-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDisplayHelpers.cs
398 lines (367 loc) · 14.5 KB
/
DisplayHelpers.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media.Imaging;
namespace SDKTemplate
{
/// <summary>
/// Represents the display of an attribute - both characteristics and services.
/// </summary>
public class BluetoothLEAttributeDisplay
{
public GattCharacteristic characteristic;
public GattDescriptor descriptor;
public GattDeviceService service;
public BluetoothLEAttributeDisplay(GattDeviceService service)
{
this.service = service;
AttributeDisplayType = AttributeType.Service;
}
public BluetoothLEAttributeDisplay(GattCharacteristic characteristic)
{
this.characteristic = characteristic;
AttributeDisplayType = AttributeType.Characteristic;
}
public string Name
{
get
{
switch (AttributeDisplayType)
{
case AttributeType.Service:
if (IsSigDefinedUuid(service.Uuid))
{
GattNativeServiceUuid serviceName;
if (Enum.TryParse(Utilities.ConvertUuidToShortId(service.Uuid).ToString(), out serviceName))
{
return serviceName.ToString();
}
}
else
{
return "Custom Service: " + service.Uuid;
}
break;
case AttributeType.Characteristic:
if (IsSigDefinedUuid(characteristic.Uuid))
{
GattNativeCharacteristicUuid characteristicName;
if (Enum.TryParse(Utilities.ConvertUuidToShortId(characteristic.Uuid).ToString(),
out characteristicName))
{
return characteristicName.ToString();
}
}
else
{
if (!string.IsNullOrEmpty(characteristic.UserDescription))
{
return characteristic.UserDescription;
}
else
{
return "Custom Characteristic: " + characteristic.Uuid;
}
}
break;
default:
break;
}
return "Invalid";
}
}
public AttributeType AttributeDisplayType { get; }
/// <summary>
/// The SIG has a standard base value for Assigned UUIDs. In order to determine if a UUID is SIG defined,
/// zero out the unique section and compare the base sections.
/// </summary>
/// <param name="uuid">The UUID to determine if SIG assigned</param>
/// <returns></returns>
private static bool IsSigDefinedUuid(Guid uuid)
{
var bluetoothBaseUuid = new Guid("00000000-0000-1000-8000-00805F9B34FB");
var bytes = uuid.ToByteArray();
// Zero out the first and second bytes
// Note how each byte gets flipped in a section - 1234 becomes 34 12
// Example Guid: 35918bc9-1234-40ea-9779-889d79b753f0
// ^^^^
// bytes output = C9 8B 91 35 34 12 EA 40 97 79 88 9D 79 B7 53 F0
// ^^ ^^
bytes[0] = 0;
bytes[1] = 0;
var baseUuid = new Guid(bytes);
return baseUuid == bluetoothBaseUuid;
}
}
public enum AttributeType
{
Service = 0,
Characteristic = 1,
Descriptor = 2
}
/// <summary>
/// Display class used to represent a BluetoothLEDevice in the Device list
/// </summary>
public class BluetoothLEDeviceDisplay : INotifyPropertyChanged
{
public BluetoothLEDeviceDisplay(DeviceInformation deviceInfoIn)
{
DeviceInformation = deviceInfoIn;
//UpdateGlyphBitmapImage();
}
public DeviceInformation DeviceInformation { get; private set; }
public string Id => DeviceInformation.Id;
public string Name => DeviceInformation.Name;
public bool IsPaired => DeviceInformation.Pairing.IsPaired;
public bool IsConnected => (bool?)DeviceInformation.Properties["System.Devices.Aep.IsConnected"] == true;
public bool IsConnectable => (bool?)DeviceInformation.Properties["System.Devices.Aep.Bluetooth.Le.IsConnectable"] == true;
public IReadOnlyDictionary<string, object> Properties => DeviceInformation.Properties;
public BitmapImage GlyphBitmapImage { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
public void Update(DeviceInformationUpdate deviceInfoUpdate)
{
DeviceInformation.Update(deviceInfoUpdate);
OnPropertyChanged("Id");
OnPropertyChanged("Name");
OnPropertyChanged("DeviceInformation");
OnPropertyChanged("IsPaired");
OnPropertyChanged("IsConnected");
OnPropertyChanged("Properties");
OnPropertyChanged("IsConnectable");
//UpdateGlyphBitmapImage();
}
//private async void UpdateGlyphBitmapImage()
//{
// DeviceThumbnail deviceThumbnail = await DeviceInformation.GetGlyphThumbnailAsync();
// var glyphBitmapImage = new BitmapImage();
// await glyphBitmapImage.SetSourceAsync(deviceThumbnail);
// GlyphBitmapImage = glyphBitmapImage;
// OnPropertyChanged("GlyphBitmapImage");
//}
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
#if x
public class GeneralPropertyValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
object property = null;
var properties = value as IReadOnlyDictionary<string, object>;
var propertyName = parameter as string;
if (properties != null && !string.IsNullOrEmpty(propertyName))
{
property = properties[propertyName];
}
return property;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
#endif
// This inverts the sense of a boolean.
public class InvertConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return !(bool)value;
}
}
/// <summary>
/// This enum assists in finding a string representation of a BT SIG assigned value for Service UUIDS
/// Reference: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
/// </summary>
public enum GattNativeServiceUuid : ushort
{
None = 0,
AlertNotification = 0x1811,
Battery = 0x180F,
BloodPressure = 0x1810,
CurrentTimeService = 0x1805,
CyclingSpeedandCadence = 0x1816,
DeviceInformation = 0x180A,
GenericAccess = 0x1800,
GenericAttribute = 0x1801,
Glucose = 0x1808,
HealthThermometer = 0x1809,
HeartRate = 0x180D,
HumanInterfaceDevice = 0x1812,
ImmediateAlert = 0x1802,
LinkLoss = 0x1803,
NextDSTChange = 0x1807,
PhoneAlertStatus = 0x180E,
ReferenceTimeUpdateService = 0x1806,
RunningSpeedandCadence = 0x1814,
ScanParameters = 0x1813,
TxPower = 0x1804,
SimpleKeyService = 0xFFE0
}
/// <summary>
/// This enum is nice for finding a string representation of a BT SIG assigned value for Characteristic UUIDs
/// Reference: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicsHome.aspx
/// </summary>
public enum GattNativeCharacteristicUuid : ushort
{
None = 0,
AlertCategoryID = 0x2A43,
AlertCategoryIDBitMask = 0x2A42,
AlertLevel = 0x2A06,
AlertNotificationControlPoint = 0x2A44,
AlertStatus = 0x2A3F,
Appearance = 0x2A01,
BatteryLevel = 0x2A19,
BloodPressureFeature = 0x2A49,
BloodPressureMeasurement = 0x2A35,
BodySensorLocation = 0x2A38,
BootKeyboardInputReport = 0x2A22,
BootKeyboardOutputReport = 0x2A32,
BootMouseInputReport = 0x2A33,
CSCFeature = 0x2A5C,
CSCMeasurement = 0x2A5B,
CurrentTime = 0x2A2B,
DateTime = 0x2A08,
DayDateTime = 0x2A0A,
DayofWeek = 0x2A09,
DeviceName = 0x2A00,
DSTOffset = 0x2A0D,
ExactTime256 = 0x2A0C,
FirmwareRevisionString = 0x2A26,
GlucoseFeature = 0x2A51,
GlucoseMeasurement = 0x2A18,
GlucoseMeasurementContext = 0x2A34,
HardwareRevisionString = 0x2A27,
HeartRateControlPoint = 0x2A39,
HeartRateMeasurement = 0x2A37,
HIDControlPoint = 0x2A4C,
HIDInformation = 0x2A4A,
IEEE11073_20601RegulatoryCertificationDataList = 0x2A2A,
IntermediateCuffPressure = 0x2A36,
IntermediateTemperature = 0x2A1E,
LocalTimeInformation = 0x2A0F,
ManufacturerNameString = 0x2A29,
MeasurementInterval = 0x2A21,
ModelNumberString = 0x2A24,
NewAlert = 0x2A46,
PeripheralPreferredConnectionParameters = 0x2A04,
PeripheralPrivacyFlag = 0x2A02,
PnPID = 0x2A50,
ProtocolMode = 0x2A4E,
ReconnectionAddress = 0x2A03,
RecordAccessControlPoint = 0x2A52,
ReferenceTimeInformation = 0x2A14,
Report = 0x2A4D,
ReportMap = 0x2A4B,
RingerControlPoint = 0x2A40,
RingerSetting = 0x2A41,
RSCFeature = 0x2A54,
RSCMeasurement = 0x2A53,
SCControlPoint = 0x2A55,
ScanIntervalWindow = 0x2A4F, //240
ScanRefresh = 0x2A31, //0,
SensorLocation = 0x2A5D,
SerialNumberString = 0x2A25,
ServiceChanged = 0x2A05,
SoftwareRevisionString = 0x2A28,
SupportedNewAlertCategory = 0x2A47,
SupportedUnreadAlertCategory = 0x2A48,
SystemID = 0x2A23,
TemperatureMeasurement = 0x2A1C,
TemperatureType = 0x2A1D,
TimeAccuracy = 0x2A12,
TimeSource = 0x2A13,
TimeUpdateControlPoint = 0x2A16,
TimeUpdateState = 0x2A17,
TimewithDST = 0x2A11,
TimeZone = 0x2A0E,
TxPowerLevel = 0x2A07,
UnreadAlertStatus = 0x2A45,
AggregateInput = 0x2A5A,
AnalogInput = 0x2A58,
AnalogOutput = 0x2A59,
CyclingPowerControlPoint = 0x2A66,
CyclingPowerFeature = 0x2A65,
CyclingPowerMeasurement = 0x2A63,
CyclingPowerVector = 0x2A64,
DigitalInput = 0x2A56,
DigitalOutput = 0x2A57,
ExactTime100 = 0x2A0B,
LNControlPoint = 0x2A6B,
LNFeature = 0x2A6A,
LocationandSpeed = 0x2A67,
Navigation = 0x2A68,
NetworkAvailability = 0x2A3E,
PositionQuality = 0x2A69,
ScientificTemperatureinCelsius = 0x2A3C,
SecondaryTimeZone = 0x2A10,
String = 0x2A3D,
TemperatureinCelsius = 0x2A1F,
TemperatureinFahrenheit = 0x2A20,
TimeBroadcast = 0x2A15,
BatteryLevelState = 0x2A1B,
BatteryPowerState = 0x2A1A,
PulseOximetryContinuousMeasurement = 0x2A5F,
PulseOximetryControlPoint = 0x2A62,
PulseOximetryFeatures = 0x2A61,
PulseOximetryPulsatileEvent = 0x2A60,
SimpleKeyState = 0xFFE1
}
/// <summary>
/// This enum assists in finding a string representation of a BT SIG assigned value for Descriptor UUIDs
/// Reference: https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorsHomePage.aspx
/// </summary>
public enum GattNativeDescriptorUuid : ushort
{
CharacteristicExtendedProperties = 0x2900,
CharacteristicUserDescription = 0x2901,
ClientCharacteristicConfiguration = 0x2902,
ServerCharacteristicConfiguration = 0x2903,
CharacteristicPresentationFormat = 0x2904,
CharacteristicAggregateFormat = 0x2905,
ValidRange = 0x2906,
ExternalReportReference = 0x2907,
ReportReference = 0x2908
}
public static class Utilities
{
/// <summary>
/// Converts from standard 128bit UUID to the assigned 32bit UUIDs. Makes it easy to compare services
/// that devices expose to the standard list.
/// </summary>
/// <param name="uuid">UUID to convert to 32 bit</param>
/// <returns></returns>
public static ushort ConvertUuidToShortId(Guid uuid)
{
// Get the short Uuid
var bytes = uuid.ToByteArray();
var shortUuid = (ushort) (bytes[0] | (bytes[1] << 8));
return shortUuid;
}
/// <summary>
/// Converts from a buffer to a properly sized byte array
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] ReadBufferToBytes(IBuffer buffer)
{
var dataLength = buffer.Length;
var data = new byte[dataLength];
using (var reader = DataReader.FromBuffer(buffer))
{
reader.ReadBytes(data);
}
return data;
}
}
}