protected async Task StopUpdatesNativeAsync() { _gattCallback.CharacteristicValueUpdated -= OnCharacteristicValueChanged; await TaskBuilder.EnqueueOnMainThreadAsync(() => { if (!_gatt.SetCharacteristicNotification(NativeCharacteristic, false)) { throw new CharacteristicReadException("GATT: SetCharacteristicNotification to false, FAILED."); } }); if (NativeCharacteristic.Descriptors.Count > 0) { var descriptors = await GetDescriptorsAsync(); var descriptor = descriptors.FirstOrDefault(d => d.Id.Equals(ClientCharacteristicConfigurationDescriptorId)) ?? descriptors.FirstOrDefault(); // fallback just in case manufacturer forgot if (descriptor != null && (Properties.HasFlag(CharacteristicPropertyType.Notify) || Properties.HasFlag(CharacteristicPropertyType.Indicate))) { await descriptor.WriteAsync(BluetoothGattDescriptor.DisableNotificationValue.ToArray()); Trace.Message("Descriptor set value: DISABLE_NOTIFY"); } } else { Trace.Message("StopUpdatesNativeAsync descriptor set value FAILED: _nativeCharacteristic.Descriptors was empty"); } }
protected async Task StartUpdatesNativeAsync() { // wire up the characteristic value updating on the gattcallback for event forwarding _gattCallback.CharacteristicValueUpdated -= OnCharacteristicValueChanged; _gattCallback.CharacteristicValueUpdated += OnCharacteristicValueChanged; await TaskBuilder.EnqueueOnMainThreadAsync(() => { if (!_gatt.SetCharacteristicNotification(NativeCharacteristic, true)) { throw new CharacteristicReadException("Gatt SetCharacteristicNotification FAILED."); } }); // In order to subscribe to notifications on a given characteristic, you must first set the Notifications Enabled bit // in its Client Characteristic Configuration Descriptor. See https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorsHomePage.aspx and // https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorViewer.aspx?u=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml // for details. if (NativeCharacteristic.Descriptors.Count > 0) { var descriptors = await GetDescriptorsAsync(); var descriptor = descriptors.FirstOrDefault(d => d.Id.Equals(ClientCharacteristicConfigurationDescriptorId)) ?? descriptors.FirstOrDefault(); // fallback just in case manufacturer forgot // has to have one of these (either indicate or notify) if (descriptor != null && Properties.HasFlag(CharacteristicPropertyType.Indicate)) { await descriptor.WriteAsync(BluetoothGattDescriptor.EnableIndicationValue.ToArray()); Trace.Message("Descriptor set value: INDICATE"); } if (descriptor != null && Properties.HasFlag(CharacteristicPropertyType.Notify)) { await descriptor.WriteAsync(BluetoothGattDescriptor.EnableNotificationValue.ToArray()); Trace.Message("Descriptor set value: NOTIFY"); } } else { Trace.Message("Descriptor set value FAILED: _nativeCharacteristic.Descriptors was empty"); } Trace.Message("Characteristic.StartUpdates, successful!"); }