123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using Windows.Devices.Bluetooth.GenericAttributeProfile;
- namespace BluetoothLENet.Classes
- {
- /// <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 Chars => (CanRead ? "R" : " ") + (CanWrite ? "W" : " ") + (CanNotify ? "N" : " ");
- public bool CanRead
- {
- get
- {
- return characteristic != null ? characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Read) : false;
- }
- }
- public bool CanWrite
- {
- get
- {
- return characteristic != null ?
- characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Write) ||
- characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.WriteWithoutResponse) ||
- characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.ReliableWrites) ||
- characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.WritableAuxiliaries)
- : false;
- }
- }
- public bool CanNotify
- {
- get
- {
- return characteristic != null ? characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify) : false;
- }
- }
- public string Name
- {
- get
- {
- switch (AttributeDisplayType)
- {
- case AttributeType.Service:
- if (IsSigDefinedUuid(service.Uuid))
- {
- if (Enum.TryParse(Utilities.ConvertUuidToShortId(service.Uuid).ToString(), out GattNativeServiceUuid serviceName))
- {
- return serviceName.ToString();
- }
- }
- else
- {
- return "Custom Service: " + service.Uuid;
- }
- break;
- case AttributeType.Characteristic:
- if (IsSigDefinedUuid(characteristic.Uuid))
- {
- if (Enum.TryParse(Utilities.ConvertUuidToShortId(characteristic.Uuid).ToString(), out GattNativeCharacteristicUuid 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;
- }
- }
- }
|