BLEInfo.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Text;
  3. using Windows.Devices.Bluetooth.GenericAttributeProfile;
  4. using Windows.Security.Cryptography;
  5. using Windows.Storage.Streams;
  6. namespace BLEComm
  7. {
  8. class BLEInfo
  9. {
  10. /// <summary>
  11. /// This enum assists in finding a string representation of a BT SIG assigned value for Service UUIDS
  12. /// Reference: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
  13. /// </summary>
  14. public enum GattNativeServiceUuid : ushort
  15. {
  16. None = 0,
  17. AlertNotification = 0x1811,
  18. Battery = 0x180F,
  19. BloodPressure = 0x1810,
  20. CurrentTimeService = 0x1805,
  21. CyclingSpeedandCadence = 0x1816,
  22. DeviceInformation = 0x180A,
  23. GenericAccess = 0x1800,
  24. GenericAttribute = 0x1801,
  25. Glucose = 0x1808,
  26. HealthThermometer = 0x1809,
  27. HeartRate = 0x180D,
  28. HumanInterfaceDevice = 0x1812,
  29. ImmediateAlert = 0x1802,
  30. LinkLoss = 0x1803,
  31. NextDSTChange = 0x1807,
  32. PhoneAlertStatus = 0x180E,
  33. ReferenceTimeUpdateService = 0x1806,
  34. RunningSpeedandCadence = 0x1814,
  35. ScanParameters = 0x1813,
  36. TxPower = 0x1804,
  37. SimpleKeyService = 0xFFE0
  38. }
  39. /// <summary>
  40. /// This enum is nice for finding a string representation of a BT SIG assigned value for Characteristic UUIDs
  41. /// Reference: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicsHome.aspx
  42. /// </summary>
  43. public enum GattNativeCharacteristicUuid : ushort
  44. {
  45. None = 0,
  46. AlertCategoryID = 0x2A43,
  47. AlertCategoryIDBitMask = 0x2A42,
  48. AlertLevel = 0x2A06,
  49. AlertNotificationControlPoint = 0x2A44,
  50. AlertStatus = 0x2A3F,
  51. Appearance = 0x2A01,
  52. BatteryLevel = 0x2A19,
  53. BloodPressureFeature = 0x2A49,
  54. BloodPressureMeasurement = 0x2A35,
  55. BodySensorLocation = 0x2A38,
  56. BootKeyboardInputReport = 0x2A22,
  57. BootKeyboardOutputReport = 0x2A32,
  58. BootMouseInputReport = 0x2A33,
  59. CSCFeature = 0x2A5C,
  60. CSCMeasurement = 0x2A5B,
  61. CurrentTime = 0x2A2B,
  62. DateTime = 0x2A08,
  63. DayDateTime = 0x2A0A,
  64. DayofWeek = 0x2A09,
  65. DeviceName = 0x2A00,
  66. DSTOffset = 0x2A0D,
  67. ExactTime256 = 0x2A0C,
  68. FirmwareRevisionString = 0x2A26,
  69. GlucoseFeature = 0x2A51,
  70. GlucoseMeasurement = 0x2A18,
  71. GlucoseMeasurementContext = 0x2A34,
  72. HardwareRevisionString = 0x2A27,
  73. HeartRateControlPoint = 0x2A39,
  74. HeartRateMeasurement = 0x2A37,
  75. HIDControlPoint = 0x2A4C,
  76. HIDInformation = 0x2A4A,
  77. IEEE11073_20601RegulatoryCertificationDataList = 0x2A2A,
  78. IntermediateCuffPressure = 0x2A36,
  79. IntermediateTemperature = 0x2A1E,
  80. LocalTimeInformation = 0x2A0F,
  81. ManufacturerNameString = 0x2A29,
  82. MeasurementInterval = 0x2A21,
  83. ModelNumberString = 0x2A24,
  84. NewAlert = 0x2A46,
  85. PeripheralPreferredConnectionParameters = 0x2A04,
  86. PeripheralPrivacyFlag = 0x2A02,
  87. PnPID = 0x2A50,
  88. ProtocolMode = 0x2A4E,
  89. ReconnectionAddress = 0x2A03,
  90. RecordAccessControlPoint = 0x2A52,
  91. ReferenceTimeInformation = 0x2A14,
  92. Report = 0x2A4D,
  93. ReportMap = 0x2A4B,
  94. RingerControlPoint = 0x2A40,
  95. RingerSetting = 0x2A41,
  96. RSCFeature = 0x2A54,
  97. RSCMeasurement = 0x2A53,
  98. SCControlPoint = 0x2A55,
  99. ScanIntervalWindow = 0x2A4F,
  100. ScanRefresh = 0x2A31,
  101. SensorLocation = 0x2A5D,
  102. SerialNumberString = 0x2A25,
  103. ServiceChanged = 0x2A05,
  104. SoftwareRevisionString = 0x2A28,
  105. SupportedNewAlertCategory = 0x2A47,
  106. SupportedUnreadAlertCategory = 0x2A48,
  107. SystemID = 0x2A23,
  108. TemperatureMeasurement = 0x2A1C,
  109. TemperatureType = 0x2A1D,
  110. TimeAccuracy = 0x2A12,
  111. TimeSource = 0x2A13,
  112. TimeUpdateControlPoint = 0x2A16,
  113. TimeUpdateState = 0x2A17,
  114. TimewithDST = 0x2A11,
  115. TimeZone = 0x2A0E,
  116. TxPowerLevel = 0x2A07,
  117. UnreadAlertStatus = 0x2A45,
  118. AggregateInput = 0x2A5A,
  119. AnalogInput = 0x2A58,
  120. AnalogOutput = 0x2A59,
  121. CyclingPowerControlPoint = 0x2A66,
  122. CyclingPowerFeature = 0x2A65,
  123. CyclingPowerMeasurement = 0x2A63,
  124. CyclingPowerVector = 0x2A64,
  125. DigitalInput = 0x2A56,
  126. DigitalOutput = 0x2A57,
  127. ExactTime100 = 0x2A0B,
  128. LNControlPoint = 0x2A6B,
  129. LNFeature = 0x2A6A,
  130. LocationandSpeed = 0x2A67,
  131. Navigation = 0x2A68,
  132. NetworkAvailability = 0x2A3E,
  133. PositionQuality = 0x2A69,
  134. ScientificTemperatureinCelsius = 0x2A3C,
  135. SecondaryTimeZone = 0x2A10,
  136. String = 0x2A3D,
  137. TemperatureinCelsius = 0x2A1F,
  138. TemperatureinFahrenheit = 0x2A20,
  139. TimeBroadcast = 0x2A15,
  140. BatteryLevelState = 0x2A1B,
  141. BatteryPowerState = 0x2A1A,
  142. PulseOximetryContinuousMeasurement = 0x2A5F,
  143. PulseOximetryControlPoint = 0x2A62,
  144. PulseOximetryFeatures = 0x2A61,
  145. PulseOximetryPulsatileEvent = 0x2A60,
  146. SimpleKeyState = 0xFFE1
  147. }
  148. /// <summary>
  149. /// This enum assists in finding a string representation of a BT SIG assigned value for Descriptor UUIDs
  150. /// Reference: https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorsHomePage.aspx
  151. /// </summary>
  152. public enum GattNativeDescriptorUuid : ushort
  153. {
  154. CharacteristicExtendedProperties = 0x2900,
  155. CharacteristicUserDescription = 0x2901,
  156. ClientCharacteristicConfiguration = 0x2902,
  157. ServerCharacteristicConfiguration = 0x2903,
  158. CharacteristicPresentationFormat = 0x2904,
  159. CharacteristicAggregateFormat = 0x2905,
  160. ValidRange = 0x2906,
  161. ExternalReportReference = 0x2907,
  162. ReportReference = 0x2908
  163. }
  164. public static ushort ConvertUuidToShortId(Guid uuid)
  165. {
  166. // Get the short Uuid
  167. var bytes = uuid.ToByteArray();
  168. var shortUuid = (ushort)(bytes[0] | (bytes[1] << 8));
  169. return shortUuid;
  170. }
  171. public static string GetServiceName(GattDeviceService service)
  172. {
  173. BLEInfo.GattNativeServiceUuid serviceName;
  174. if (Enum.TryParse(ConvertUuidToShortId(service.Uuid).ToString(), out serviceName))
  175. {
  176. return serviceName.ToString();
  177. }
  178. return "Custom Service: " + service.Uuid;
  179. }
  180. public static string GetCharacteristicName(GattCharacteristic characteristic)
  181. {
  182. GattNativeCharacteristicUuid characteristicName;
  183. if (Enum.TryParse(ConvertUuidToShortId(characteristic.Uuid).ToString(),
  184. out characteristicName))
  185. {
  186. return characteristicName.ToString();
  187. }
  188. if (!string.IsNullOrEmpty(characteristic.UserDescription))
  189. {
  190. return characteristic.UserDescription;
  191. }
  192. else
  193. {
  194. return "Custom Characteristic: " + characteristic.Uuid;
  195. }
  196. }
  197. public static string FormatValueByPresentation(IBuffer buffer, GattPresentationFormat format, out string datatype)
  198. {
  199. // BT_Code: For the purpose of this sample, this function converts only UInt32 and
  200. // UTF-8 buffers to readable text. It can be extended to support other formats if your app needs them.
  201. byte[] data;
  202. CryptographicBuffer.CopyToByteArray(buffer, out data);
  203. if (format != null)
  204. {
  205. if (format.FormatType == GattPresentationFormatTypes.UInt32 && data.Length >= 4)
  206. {
  207. datatype = "Decimal";
  208. return BitConverter.ToInt32(data, 0).ToString();
  209. }
  210. else if (format.FormatType == GattPresentationFormatTypes.UInt16 && data.Length >= 2)
  211. {
  212. datatype = "Decimal";
  213. return BitConverter.ToInt16(data, 0).ToString();
  214. }
  215. else if (format.FormatType == GattPresentationFormatTypes.Utf8)
  216. {
  217. try
  218. {
  219. datatype = "UTF8";
  220. return Encoding.UTF8.GetString(data);
  221. }
  222. catch (ArgumentException)
  223. {
  224. datatype = "known";
  225. return "(Invalid UTF-8 string)";
  226. }
  227. }
  228. else
  229. {
  230. datatype = "HEX";
  231. // Add support for other format types as needed.
  232. return CryptographicBuffer.EncodeToHexString(buffer);
  233. }
  234. }
  235. else if (data != null)
  236. {
  237. datatype = "HEX";
  238. return CryptographicBuffer.EncodeToHexString(buffer);
  239. }
  240. else
  241. {
  242. datatype = "HEX";
  243. return "Empty data received";
  244. }
  245. }
  246. }
  247. }