Shikhar Tandon
Shikhar Tandon

Reputation: 41

WriteClientCharacteristicConfigurationDescriptorAsync not working in UWP

My selected characterstic property returns Indicate but still WriteClientCharacteristicConfigurationDescriptorAsync(cccdValue) does not return Success status

This is the piece of code: status = await selectedCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(cccdValue);//writes cccd to the ble device to enable indication or notification

status = await selectedCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(cccdValue);//writes cccd to the ble device to enable indication or notification

 if (status == GattCommunicationStatus.Success)
                    {
                        AddValueChangedHandler();
                        rootPage.NotifyUser("Successfully subscribed for value changes", NotifyType.StatusMessage);
                    }
                    else
                    {
                        rootPage.NotifyUser($"Error registering for value changes: {status}", NotifyType.ErrorMessage);
                    }

It always goes in else condition.I have put Indicate property True but still it does not work.

Please help, any suggestions? Thanks

Upvotes: 0

Views: 3432

Answers (2)

jadware
jadware

Reputation: 165

One thing to be aware of, that's not well documented, is that the peripheral needs to have a writable Gatt CCCD Characteristic Descriptor with a very specific UUID (00002902-0000-1000-8000-00805f9b34fb) to be technically compliant. Android and iOS will not fail if the peripheral is non-compliant, but UWP currently will throw this exception. There does not appear to be any way to get UWP to work with a non-compliant device.

If you are making a peripheral, be aware that no matter what UUID's you use for anything, the CCCD needs be the 2902 UUID. Otherwise you'll get this UWP exception.

Upvotes: 2

Icce
Icce

Reputation: 51

Does the characteristic support Writing?

GattCharacteristicProperties properties = characteristic.CharacteristicProperties;
            if (properties.HasFlag(GattCharacteristicProperties.Write) || properties.HasFlag(GattCharacteristicProperties.WriteWithoutResponse))
            {
               //writing is supported..
            } 

If it does:

MSDN

There are four statuses:

  • Access Denied
  • Device Unreachable
  • Success
  • Protocol error

As you have not posted the status, I can't give you a precise answer... If it's Protocol Error, you need to provide more information on the device. Try this:

  1. Access Denied: Retry, by getting the characteristic without using the Bluetooth Cache

            var characteristics = await selectedService.GetCharacteristicsForUuidAsync(characteristicUuid, BluetoothCacheMode.Uncached);
            selectedCharacteristic = characteristics[0];
            status = await selectedCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(cccdValue);
    

Then retry writing.

  1. Device Unreachable: The connection was lost. Either the device is too far away or you need to reconnect. Recreate the device object and retry.

           BluetoothLEDevice device = await BluetoothLEDevice.FromIdAsync(deviceId); 
            .....
    

Upvotes: 2

Related Questions