/// <summary> /// Initializes whole service object. Needs to be called before any use. It is called /// automatically by parent BleDevice object while its initialization. /// </summary> public void Initialize(BleDevice device) { try { DeviceService = device.LEDevice.GetGattService(UUID); } catch (Exception e) { DeviceService = null; } if (!IsAvailable) { return; } var props = this.GetType().GetProperties(); foreach (var property in props) { if (!property.PropertyType.IsAssignableFrom(typeof(BleCharacteristic))) { continue; } BleCharacteristic characteristic = (BleCharacteristic)property.GetValue(this); if (characteristic != null) { AllCharacteristics.Add(characteristic); characteristic.Initialize(this); } } }
public static async Task<string> ReadAsUnicodeString(this BleCharacteristic me) { var readStatus = await me.ReadAsync(); if (readStatus.Status == GattCommunicationStatus.Unreachable) return null; return StringParser.Convert(readStatus.Value, GattPresentationFormatTypes.Utf16); }
/// <summary> /// Disconnects parser from characteristic. /// </summary> public void Disconnect() { DisableNotifications(); DisableIndications(); if (Characteristic != null) { Characteristic.ValueChanged -= CharacteristicOnValueChanged; } Characteristic = null; }
/// <summary> /// Connects parser with particular characteristic. Characteristic can be controlled from that /// moment either directly or via this parser object. /// </summary> public void ConnectWithCharacteristic(BleCharacteristic characteristic) { Disconnect(); if (characteristic == null) { return; } Characteristic = characteristic; Characteristic.ValueChanged -= CharacteristicOnValueChanged; Characteristic.ValueChanged += CharacteristicOnValueChanged; }
public static async Task<GattCommunicationStatus> WriteAsUnicodeString(this BleCharacteristic me, string input) { var buffer = StringParser.Convert(input, GattPresentationFormatTypes.Utf16); return await me.WriteAsync(buffer); }