BluetoothLEAttributeDisplay.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Windows.Devices.Bluetooth.GenericAttributeProfile;
  2. namespace BluetoothLENet.Classes
  3. {
  4. /// <summary>
  5. /// Represents the display of an attribute - both characteristics and services.
  6. /// </summary>
  7. public class BluetoothLEAttributeDisplay
  8. {
  9. public GattCharacteristic characteristic;
  10. public GattDescriptor descriptor;
  11. public GattDeviceService service;
  12. public BluetoothLEAttributeDisplay(GattDeviceService service)
  13. {
  14. this.service = service;
  15. AttributeDisplayType = AttributeType.Service;
  16. }
  17. public BluetoothLEAttributeDisplay(GattCharacteristic characteristic)
  18. {
  19. this.characteristic = characteristic;
  20. AttributeDisplayType = AttributeType.Characteristic;
  21. }
  22. public string Chars => (CanRead ? "R" : " ") + (CanWrite ? "W" : " ") + (CanNotify ? "N" : " ");
  23. public bool CanRead
  24. {
  25. get
  26. {
  27. return characteristic != null ? characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Read) : false;
  28. }
  29. }
  30. public bool CanWrite
  31. {
  32. get
  33. {
  34. return characteristic != null ?
  35. characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Write) ||
  36. characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.WriteWithoutResponse) ||
  37. characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.ReliableWrites) ||
  38. characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.WritableAuxiliaries)
  39. : false;
  40. }
  41. }
  42. public bool CanNotify
  43. {
  44. get
  45. {
  46. return characteristic != null ? characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify) : false;
  47. }
  48. }
  49. public string Name
  50. {
  51. get
  52. {
  53. switch (AttributeDisplayType)
  54. {
  55. case AttributeType.Service:
  56. if (IsSigDefinedUuid(service.Uuid))
  57. {
  58. if (Enum.TryParse(Utilities.ConvertUuidToShortId(service.Uuid).ToString(), out GattNativeServiceUuid serviceName))
  59. {
  60. return serviceName.ToString();
  61. }
  62. }
  63. else
  64. {
  65. return "Custom Service: " + service.Uuid;
  66. }
  67. break;
  68. case AttributeType.Characteristic:
  69. if (IsSigDefinedUuid(characteristic.Uuid))
  70. {
  71. if (Enum.TryParse(Utilities.ConvertUuidToShortId(characteristic.Uuid).ToString(), out GattNativeCharacteristicUuid characteristicName))
  72. {
  73. return characteristicName.ToString();
  74. }
  75. }
  76. else
  77. {
  78. if (!string.IsNullOrEmpty(characteristic.UserDescription))
  79. {
  80. return characteristic.UserDescription;
  81. }
  82. else
  83. {
  84. return "Custom Characteristic: " + characteristic.Uuid;
  85. }
  86. }
  87. break;
  88. default:
  89. break;
  90. }
  91. return "Invalid";
  92. }
  93. }
  94. public AttributeType AttributeDisplayType { get; }
  95. /// <summary>
  96. /// The SIG has a standard base value for Assigned UUIDs. In order to determine if a UUID is SIG defined,
  97. /// zero out the unique section and compare the base sections.
  98. /// </summary>
  99. /// <param name="uuid">The UUID to determine if SIG assigned</param>
  100. /// <returns></returns>
  101. private static bool IsSigDefinedUuid(Guid uuid)
  102. {
  103. var bluetoothBaseUuid = new Guid("00000000-0000-1000-8000-00805F9B34FB");
  104. var bytes = uuid.ToByteArray();
  105. // Zero out the first and second bytes
  106. // Note how each byte gets flipped in a section - 1234 becomes 34 12
  107. // Example Guid: 35918bc9-1234-40ea-9779-889d79b753f0
  108. // ^^^^
  109. // bytes output = C9 8B 91 35 34 12 EA 40 97 79 88 9D 79 B7 53 F0
  110. // ^^ ^^
  111. bytes[0] = 0;
  112. bytes[1] = 0;
  113. var baseUuid = new Guid(bytes);
  114. return baseUuid == bluetoothBaseUuid;
  115. }
  116. }
  117. }