-
Notifications
You must be signed in to change notification settings - Fork 1
/
BTdevice.cs
360 lines (342 loc) · 12.9 KB
/
BTdevice.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#undef USE_SSAPI
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Intermec.Communication.Bluetooth;
using System.IO;
namespace setBTscanner
{
class BTdevice : IDisposable
{
private Intermec.Communication.Bluetooth.DeviceUtility _deviceUtility;
/*
<PSWDM0C FormatVersion="1.0">
<Logging level="none"/>
<UserInterface>
<Configuration name="PSWDM0C">
<FullScreen enabled="N"/>
</Configuration>
</UserInterface>
<Logging level="none"/>
<Devices>
<Security enabled="Y" strict="N"/>
<Device>
<DeviceName>RS507 12088000500400 (0023686e70bc)</DeviceName>
<DeviceAddress>0023686e70bc</DeviceAddress>
<ClassOfDevice>080620</ClassOfDevice>
</Device>
<DefaultDevice name="RS507 12088000500400 (0023686e70bc)"/>
<BtPort>COM6:</BtPort>
</Devices>
</PSWDM0C>
*/
private string xmlFileName = @"\Windows\pswdm0c.xml";
const string pswdmxml = "pswdm0c.xml";
logging log;
/// <summary>
/// a list of possible passkeys
/// </summary>
string[] _sPasskeys = { "12345", "0000", "1234" };
public BTdevice(ref logging lg)
{
log = lg;
try
{
preparePSWMD0C();
#if USE_SSAPI
BTpower btPower = new BTpower();
if (btPower.IsBluetoothOn() == false)
{
log.WriteLog("BT power is OFF, trying to switch ON...");
btPower.TurnOnBluetooth();
if (btPower.IsBluetoothOn() == false)
log.WriteLog("BT power switch ON failed");
else
log.WriteLog("BT power is now ON");
}
else
log.WriteLog("BT power was ON, no change");
//btPower.Dispose();
#endif
_deviceUtility = new DeviceUtility();
_deviceUtility.Initialize(xmlFileName);
}
catch (DeviceUtilityException ex)
{
log.WriteLog("DeviceUtilityException in BTdevice(): " + ex.Message);
_deviceUtility = null;
}
catch (Exception ex)
{
log.WriteLog("Exception in BTdevice(): " + ex.Message);
_deviceUtility = null;
}
log.WriteLog("BTdevice() OK");
}
public void Dispose()
{
if (_deviceUtility != null)
{
_deviceUtility.Close();
_deviceUtility.Dispose();
_deviceUtility = null;
}
}
public bool DoAssociation(string btAddr)
{
if (btAddr == "")
{
log.WriteLog("Missing BT Address!");
Win32.doBadBeeps();
return false;
}
if (_deviceUtility == null)
{
log.WriteLog("DeviceUtility unavailable!");
Win32.doBadBeeps();
return false;
}
string strDeviceName;
log.WriteLog("FindDevice '" + btAddr + "'...");
if (!FindDevice(btAddr, out strDeviceName))
{
log.WriteLog("Device not found!");
Win32.doBadBeeps();
return false;
}
log.WriteLog("... Device was found");
log.WriteLog("Association of '" + strDeviceName + "'...");
foreach (string s in _sPasskeys)
{
try
{
log.WriteLog("Bonding...");
if (_deviceUtility.IsBondWithDeviceNeeded(strDeviceName))
{
string strPasskey;
log.WriteLog("Authentication: trying '" + s + "'...");
//deviceUtility.GetDeviceProperty(strDeviceName, DeviceUtility.PropertyName.Passkey);
//if (string.Compare(strPasskey, "") == 0)
//{
strPasskey = s;
//strPasskey = "1111";
_deviceUtility.SetDeviceProperty(strDeviceName, DeviceUtility.PropertyName.Passkey, strPasskey);
//}
// Bond the computer and the device.
// TODO: Because radio connections can sometimes fail,
// you should build in retry logic when BondWithDevice
// fails. This sample just tries once.
_deviceUtility.BondWithDevice(strDeviceName);
log.WriteLog("... Association and bonding finished");
break;
}
else
{
log.WriteLog("... no bonding needed");
break;
}
}
catch (DeviceUtilityException ex)
{
log.WriteLog("Error in Association: " + ex.Message);
return false;
}
catch (Exception ex)
{
log.WriteLog("Error in Association: " + ex.Message);
return false;
}
}//foreach
try
{
log.WriteLog("Registration...");
_deviceUtility.SetDefaultDevice(null, null, strDeviceName);
// Set up a port to use the selected device. Refer to
// the BtPort element in the XML file.
//_deviceUtility.AddDevice(null, null, "BtPort");
//_deviceUtility.SetDeviceProperty("BtPort", DeviceUtility.PropertyName.DeviceAddress, "COM6:");
_deviceUtility.SetActiveDevice(null, null, strDeviceName);
log.WriteLog("...Registration OK");
Win32.doGoodBeeps();
return true;
}
catch (DeviceUtilityException ex)
{
log.WriteLog("Error in Registration: " + ex.Message);
Win32.doBadBeeps();
return false;
}
}
private bool FindDevice(string BDAAddress, out string strOutDeviceName)
{
bool bFound = false;
strOutDeviceName = "";
try
{
log.WriteLog("Discovering devices ...");
_deviceUtility.DiscoverDevices(null, null);
log.WriteLog("...Discover finished");
}
catch (DeviceUtilityException ex)
{
log.WriteLog("DiscoveredDevices exception: " + ex.Message);
}
try
{
string[] temp = _deviceUtility.GetDiscoveredDevices();
if (temp.Length > 0)
{
// Add found devices to the list of known devices.
// When calling AddDevice with a device name returned by
// GetDiscoveredDevices, make sure you call AddDevice and
// GetDiscoveredDevices within the same Initialize/Close
// block. Otherwise,the device is added to the XML file
// as a non-Bluetooth device because Bluetooth device
// information retrieved by DiscoverDevices is lost when
// Close is called.
foreach (string device in temp)
{
_deviceUtility.AddDevice(null, null, device);
log.WriteLog("Added '" + device + "'");
}
}
else
{
log.WriteLog("No devices nearby");
}
}
catch (DeviceUtilityException ex)
{
log.WriteLog("GetDiscoveredDevices exception: " + ex.Message);
}
try
{
string deviceAddress;
string[] temp = _deviceUtility.GetDevices(null, null);
if (temp.Length > 0)
{
foreach (string device in temp)
{
deviceAddress = _deviceUtility.GetDeviceProperty(device, DeviceUtility.PropertyName.DeviceAddress);
if (string.Compare(deviceAddress, BDAAddress, true) == 0)
{
bFound = true;
strOutDeviceName = device;
//string passkey = _deviceUtility.GetDeviceProperty(device, DeviceUtility.PropertyName.Passkey); //throws exception see docu
//string authent = _deviceUtility.GetDeviceProperty(device, DeviceUtility.PropertyName.Authentication);
break;
}
}
}
else
{
log.WriteLog("No devices by GetDevices()");
}
}
catch (DeviceUtilityException ex)
{
log.WriteLog("GetDiscoveredDevices exception: " + ex.Message);
}
if (bFound)
{
log.WriteLog("Found device with matching '" + BDAAddress + "'");
return true;
}
else
{
log.WriteLog("No matching devices for '" + BDAAddress + "'");
return false;
}
}
void preparePSWMD0C()
{
string sXML = getResourceOrFile(pswdmxml);
//save to windows
StreamWriter s = new StreamWriter(xmlFileName);
s.WriteLine(sXML);
s.Flush();
s.Close();
log.WriteLog("Starting with pswdm0c.xml");
}
private string getResourceOrFile(string sFile)
{
string sReturn = "";
if (existAppFile(sFile))
{
System.IO.TextReader tr = new System.IO.StreamReader(Helpers.AppPath() + sFile);
sReturn = tr.ReadToEnd();
tr.Close();
log.WriteLog("using external resource '" + sFile + "'");
}
else
{
try
{
using (Stream stream = this.GetType().Assembly.GetManifestResourceStream(Helpers.AssemblyName() + "." + sFile))
{
using (StreamReader reader = new StreamReader(stream))
{
sReturn = reader.ReadToEnd();
log.WriteLog("using embedded resource '" + sFile + "'");
}
}
}
catch (Exception ex)
{
log.WriteLog("Exception in getResourceOrFile() for '" + sFile + "'" + ex.Message);
}
}
return sReturn;
}
private static bool existAppFile(string sFile)
{
return System.IO.File.Exists(Helpers.AppPath() + sFile);
}
}
class Helpers
{
static bool logOnceApp = false;
static bool logOnceAssembly = false;
static string _AppPath = null;
public static string AppPath()
{
if (_AppPath == null)
{
_AppPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
if (!_AppPath.EndsWith(@"\"))
_AppPath += @"\";
}
if (!logOnceApp)
{
System.Diagnostics.Debug.WriteLine("_AppPath='" + _AppPath + "'");
logOnceApp = true;
}
return _AppPath;
}
static string _AssemblyName = null;
public static string AssemblyName()
{
if (_AssemblyName == null)
{
_AssemblyName = System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}
if (!logOnceAssembly)
{
System.Diagnostics.Debug.WriteLine("_AssemblyName='" + _AssemblyName + "'");
logOnceAssembly = true;
}
return _AssemblyName;
}
public static void logError(string s)
{
System.Diagnostics.Debug.WriteLine("Error: " + s);
//logToFile(s);
}
public static void logInfo(string s)
{
System.Diagnostics.Debug.WriteLine("Info: " + s);
//logToFile(s);
}
}
}