internal async Task <bool> Init() { bool success = false; bleDevice = await Windows.Devices.Bluetooth.BluetoothLEDevice.FromIdAsync(deviceId); services = bleDevice.GattServices; int ccc = services.Count; for (int i = 0; i < ccc; i++) { Windows.Devices.Bluetooth.GenericAttributeProfile.GattDeviceService service = services[i]; string s = service.Uuid.ToString(); if (s == "4f63756c-7573-2054-6872-65656d6f7465") { myService = service; //var CharResult = myService.GetAllCharacteristics(); //Console.WriteLine("elo"); break; } } if (myService != null) { try { allCharacteristics = myService.GetAllCharacteristics(); Guid notifyGuid = new Guid("{c8c51726-81bc-483b-a052-f7a14ea3d281}"); Guid writeGuid = new Guid("{c8c51726-81bc-483b-a052-f7a14ea3d282}"); 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; } catch { } } return(success); }
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!!!!!!! } } } }