private async Task PlatformStartNotifications() { Uap.GattClientCharacteristicConfigurationDescriptorValue value = Uap.GattClientCharacteristicConfigurationDescriptorValue.None; if (_characteristic.CharacteristicProperties.HasFlag(Uap.GattCharacteristicProperties.Notify)) { value = Uap.GattClientCharacteristicConfigurationDescriptorValue.Notify; } else if (_characteristic.CharacteristicProperties.HasFlag(Uap.GattCharacteristicProperties.Indicate)) { value = Uap.GattClientCharacteristicConfigurationDescriptorValue.Indicate; } else { return; } try { var result = await _characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(value); } catch (UnauthorizedAccessException) { // not supported } return; }
private async void CharacteristicIndicateButton_Click() { try { // BT_Code: Must write the CCCD in order for server to send indications. // We receive them in the ValueChanged event handler. // Note that this sample configures either Indicate or Notify, but not both. var result = await selectedCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync( GattClientCharacteristicConfigurationDescriptorValue.Indicate); if (result == GattCommunicationStatus.Success) { AddValueChangedHandler(); rootPage.NotifyUser("Successfully registered for indications", NotifyType.StatusMessage); } else { rootPage.NotifyUser($"Error registering for indications: {result}", NotifyType.ErrorMessage); } } catch (UnauthorizedAccessException ex) { // This usually happens when a device reports that it support indicate, but it actually doesn't. rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage); } }
protected override async void OnNavigatedTo(NavigationEventArgs e) { selectedBtleDevice = (BluetoothLEDevice)e.Parameter; mwGattService = selectedBtleDevice.GetGattService(GUID_METAWEAR_SERVICE); foreach (var characteristic in selectedBtleDevice.GetGattService(GUID_DEVICE_INFO_SERVICE).GetAllCharacteristics()) { var result = await characteristic.ReadValueAsync(); string value = result.Status == GattCommunicationStatus.Success ? System.Text.Encoding.UTF8.GetString(result.Value.ToArray(), 0, (int)result.Value.Length) : "N/A"; mwDeviceInfoChars.Add(characteristic.Uuid, value); outputListView.Items.Add(new ConsoleLine(ConsoleEntryType.INFO, DEVICE_INFO_NAMES[characteristic.Uuid] + ": " + value)); } mwNotifyChar = mwGattService.GetCharacteristics(METAWEAR_NOTIFY_CHARACTERISTIC).First(); await mwNotifyChar.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); mwNotifyChar.ValueChanged += new TypedEventHandler <Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristic, GattValueChangedEventArgs>((Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristic sender, GattValueChangedEventArgs obj) => { byte[] response = obj.CharacteristicValue.ToArray(); mbl_mw_connection_notify_char_changed(mwBoard, response, (byte)response.Length); }); }
private async Task InitializeEvents() { Guid notifyGuid = new Guid("{c8c51726-81bc-483b-a052-f7a14ea3d281}"); Guid writeGuid = new Guid("{c8c51726-81bc-483b-a052-f7a14ea3d282}"); bool success = false; foreach (Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristic c in allCharacteristics) { if ( c.CharacteristicProperties.HasFlag(Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristicProperties.Notify) && c.Uuid == notifyGuid) { notifyCharacteristic = c; } else if ( c.CharacteristicProperties.HasFlag(Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristicProperties.Write) && c.Uuid == writeGuid) { writeCharacteristic = c; } if (notifyCharacteristic != null && writeCharacteristic != null) { break; } } success = notifyCharacteristic != null && writeCharacteristic != null; if (success) { try { // Write the ClientCharacteristicConfigurationDescriptor in order for server to send notifications. var result = await notifyCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync( Windows.Devices.Bluetooth.GenericAttributeProfile.GattClientCharacteristicConfigurationDescriptorValue.Notify); if (result == Windows.Devices.Bluetooth.GenericAttributeProfile.GattCommunicationStatus.Success) { notifyCharacteristic.ValueChanged += SelectedCharacteristic_ValueChanged; success = true; } if (success) { success = await InitialKickEvents(); } } catch (Exception ex) { // This usually happens when not all characteristics are found // or selected characteristic has no Notify. } } }
private async Task DoStartNotifications() { Uap.GattClientCharacteristicConfigurationDescriptorValue value = Uap.GattClientCharacteristicConfigurationDescriptorValue.None; if (_characteristic.CharacteristicProperties.HasFlag(Uap.GattCharacteristicProperties.Notify)) { value = Uap.GattClientCharacteristicConfigurationDescriptorValue.Notify; } else if (_characteristic.CharacteristicProperties.HasFlag(Uap.GattCharacteristicProperties.Indicate)) { value = Uap.GattClientCharacteristicConfigurationDescriptorValue.Indicate; } else { return; } var result = await _characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(value); return; }
async Task _StartNotifyAsync() { var result = await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync( Win.GattClientCharacteristicConfigurationDescriptorValue.Notify); switch (result) { case Win.GattCommunicationStatus.Success: break; case Win.GattCommunicationStatus.AccessDenied: throw new AccessViolationException(); case Win.GattCommunicationStatus.ProtocolError: case Win.GattCommunicationStatus.Unreachable: throw new Exception(); default: throw new InvalidOperationException(); } }
/// <summary> /// Initialize the API /// </summary> /// <param name="initDelegate">C# Delegate wrapping the callback for <see cref="mbl_mw_metawearboard_initialize(IntPtr, FnVoid)"/></param> public async void Initialize(FnVoid initDelegate) { if (notifyChar == null) { notifyChar = btleDevice.GetGattService(GattCharGuid.METAWEAR_NOTIFY_CHAR.serviceGuid).GetCharacteristics(GattCharGuid.METAWEAR_NOTIFY_CHAR.guid).FirstOrDefault(); notifyChar.ValueChanged += notifyHandler; await notifyChar.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); } this.initDelegate = initDelegate; mbl_mw_metawearboard_initialize(cppBoard, this.initDelegate); }
protected override async void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); selectedDevice = e.Parameter as BluetoothLEDevice; notifyChar = selectedDevice.GetGattService(GattCharGuid.METAWEAR_NOTIFY_CHAR.serviceGuid).GetCharacteristics(GattCharGuid.METAWEAR_NOTIFY_CHAR.guid).FirstOrDefault(); await notifyChar.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); notifyChar.ValueChanged += new TypedEventHandler <Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristic, GattValueChangedEventArgs>( (Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristic sender, GattValueChangedEventArgs obj) => { byte[] response = obj.CharacteristicValue.ToArray(); mbl_mw_connection_notify_char_changed(board, response, (byte)response.Length); }); board = mbl_mw_metawearboard_create(ref btleConn); mbl_mw_metawearboard_initialize(board, initDelegate); }
/// <summary> /// Initialize the API /// </summary> /// <returns>Status value from calling <see cref="mbl_mw_metawearboard_initialize(IntPtr, Fn_IntPtr_Int)"/></returns> public async Task <int> Initialize() { if (notifyChar == null) { notifyChar = btleDevice.GetGattService(GattCharGuid.METAWEAR_NOTIFY_CHAR.serviceGuid).GetCharacteristics(GattCharGuid.METAWEAR_NOTIFY_CHAR.guid).FirstOrDefault(); notifyChar.ValueChanged += notifyHandler; await notifyChar.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); } initTaskSource = new TaskCompletionSource <int>(); initDelegate = new Fn_IntPtr_Int((caller, status) => { initTaskSource.SetResult(status); }); mbl_mw_metawearboard_initialize(cppBoard, initDelegate); return(await initTaskSource.Task); }
private async void Get_Characteriisics() { Guid notifyGuid = new Guid("{c8c51726-81bc-483b-a052-f7a14ea3d281}"); Guid writeGuid = new Guid("{c8c51726-81bc-483b-a052-f7a14ea3d282}"); bool success = true; var charResult = myService.GetAllCharacteristics(); //if (CharResult.Status == GattCommunicationStatus.Success) { foreach (Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristic c in charResult) { if ( c.CharacteristicProperties.HasFlag(Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristicProperties.Notify) && c.Uuid == notifyGuid) { notifyCharacteristic = c; } else if ( c.CharacteristicProperties.HasFlag(Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristicProperties.Write) && c.Uuid == writeGuid) { writeCharacteristic = c; } if (notifyCharacteristic != null && writeCharacteristic != null) { break; } } success = notifyCharacteristic != null && writeCharacteristic != null; if (success) { try { // Write the ClientCharacteristicConfigurationDescriptor in order for server to send notifications. var result = await notifyCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync( Windows.Devices.Bluetooth.GenericAttributeProfile.GattClientCharacteristicConfigurationDescriptorValue.Notify); if (result == Windows.Devices.Bluetooth.GenericAttributeProfile.GattCommunicationStatus.Success) { // var dialogNotifications = new MessageDialog("Successfully registered for notifications"); // await dialogNotifications.ShowAsync(); notifyCharacteristic.ValueChanged += SelectedCharacteristic_ValueChanged; success = true; } if (success) { success = await InitialKickEvents(); } } catch (Exception ex) { // This usually happens when not all characteristics are found // or selected characteristic has no Notify. //var dialogNotifications = new MessageDialog(ex.Message); //await dialogNotifications.ShowAsync(); //await Task.Delay(100); //Get_Characteriisics(myService); //try again //!!! Add a max try counter to prevent infinite loop!!!!!!! } } } }