Failed to subscribe to notification characteristic in GATT from UWP to Android - android

In my UWP application, I want to connect to a Bluetooth low energy peripheral device and subscribe to its notification characteristic. this is the application Connect method:
status = await NotifyCharacter.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
The problem is when I try to write to the notification characteristic, the WriteClientCharacteristicConfigurationDescriptorAsync method raise an exception:
The attribute cannot be written. (Exception from HRESULT: 0x80650003)
What can I do? I want to notify for android.

Related

How to write characteristics for bluetooth gatt service for two channels without getting disconnected(state 133)?

I've a bluetooth gatt service and two channels to write
bluetoothGattService = gatt?.getService(UUID.fromString(ble_gatt_service_uuid))
mTxCharacteristic1 = bluetoothGattService.getCharacteristic(UUID.fromString(txCharacteristics1))
mTxCharacteristic2 = bluetoothGattService.getCharacteristic(UUID.fromString(txCharacteristics2))
Now I have onCharacteristicsRead(), onCharacteristicsWrite(), and onCharacteristicsChange()
I connected with the ble device
When I write on mTxCharacteristic1 characteristic then ble works well
But whenever I write on mTxCharacteristic2 characteristic then ble gets DISCONNECTED
// function1
mTxCharacteristic1.writeType = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
mTxCharacteristic1.value = chunk
gatt.writeCharacteristic(mTxCharacteristic1)
//function 2
mTxCharacteristic2.writeType = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
mTxCharacteristic2.value = chunk
gatt.writeCharacteristic(mTxCharacteristic2)
How to resolve this?
Everytime it is getting disconnected whenever I send data using 2nd characteristics, I'm only able to connect with ble again only when I restart my android device.
I tried:
gatt.beginReliableWrite()
gatt.setCharacteristicNotification(characteristic1,true)
gatt.writeCharacteristic(characteristic1)
gatt.executeReliableWrite()
gatt.beginReliableWrite()
gatt.setCharacteristicNotification(characteristic2,true)
gatt.writeCharacteristic(characteristic2)
gatt.executeReliableWrite()
nrf connect app showing (TX(1) and unknown_characteristic(2))
still getting the same disconnected with gatt server issue with state 133.
Just made this simple writeCharacteristics instead of reliableWrite on doing this disconnection and no repairing issue resolved
gatt.writeCharacteristic(characteristic1)
gatt.writeCharacteristic(characteristic2)
I was have 8 byte of packet data where I sent null which I found on debugging
expected was 19 byte array and I was sending only 11 packets (8 missed)
due to this it was giving me error and getting disconnected with state code 133

Android BLE (Bluetooth Gatt Descriptor) always null

Can any one please help.
I am developing a Bluetooth Low Energy App
Works are done:
Devices Scan
Device Connection
Write to remote Device Raspberry Pi
The remote device has service id:
6e400001-b5a3-f393-e0a9-e50e24dcca9e
Write Strings is:
6e400002-b5a3-f393-e0a9-e50e24dcca9e
Notify String is:
6e400003-b5a3-f393-e0a9-e50e24dcca9e
All above custom services working well on IOS and on Android all done instead of listening to remote device notification.
I can write with:
gatt.writeCharacteristic(characteristicWrite);
I can enable notification with:
gatt.setCharacteristicNotification(characteristicNotify, true);
But when I try to fetch descriptor the descriptor is always null.
I tried:
UUID uuid = UUID.fromString("00000001-0000-1000-8000-00805f9b34fb");
BluetoothGattDescriptor descriptor =
characteristic.getDescriptor(uuid);
UUID uuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
BluetoothGattDescriptor descriptor =
characteristic.getDescriptor(uuid);
UUID uuid = convertFromInteger(0x2902); BluetoothGattDescriptor
descriptor = characteristic.getDescriptor(uuid);
UUID.fromString("6e400003-b5a3-f393-e0a9-e50e24dcca9e");
UUID.fromString("6e400002-b5a3-f393-e0a9-e50e24dcca9e");
UUID.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
in all cases the descriptor is return null and I cannot set descriptor notification value.
The server guy told the descriptor value not configured on server device, same device working fine on IOS but on Android notification not receiving.
I tested same device with this app:
https://play.google.com/store/apps/details?id=no.nordicsemi.android.nrftoolbox
I can recieve notification with the help of this app.
Above app listening on:00002902-0000-1000-8000-00805f9b34fb for descriptor
But with same UUID I can not getting notification.
Sorry for my English.
I have found my answer after two days research.
Please see reference app for testing any BLE device.
The app change request MTU and then enable notification.
My steps to write and listen notification given below:
First I tired to request MTU and then write with notification value. After first try with the help of notification token the descriptor is written successfully.
After that step I can write and listen with given device.
My be any one facing same problem so please consider above given application and check application logs. Every step will be clear by given app.

What does BluetoothGatt.setCharacteristicNotification() do?

Reading the documentation, one would think that setCharacteristicNotification enables notifications for a BLE characteristic:
Enable or disable notifications/indications for a given
characteristic.
But this method doesn't seem to do this? Reading the BLE documentation on receiving BLE notifications, it turns out to be a multi-step process where you have to call this method and then write a file into a descriptor.
If this is the case, then what does setCharacteristicNotification by itself do?
The descriptor write is needed in order to tell the remote device to send notifications. setCharactersticNotification only tells the Bluetooth stack that it should forward any received notification to the app.
Interesting read at Why does setCharacteristicNotification() not actually enable notifications? . The answerers there dig through the source code and docs to find that:
"setCharacteristicNotification only prepares the local service to receive notifications."
I would suggest a wrapper function for set notifications, because, in addition to the documentation being not-so-clear, it is a confusing concept to enable notification reception locally as well as enable notification sending on the peripheral. I would suggest something like what the kind answerers at Enabling Bluetooth characteristic Notification in Android (Bluetooth Low Energy ) Not Working use:
public boolean setCharacteristicNotification(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic characteristic,boolean enable) {
Logger.d("setCharacteristicNotification");
bluetoothGatt.setCharacteristicNotification(characteristic, enable);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID);
descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[]{0x00, 0x00});
return bluetoothGatt.writeDescriptor(descriptor); //descriptor write operation successfully started?
}

Android as BLE peripheral

I'm trying to create an Android 5 (lollipop) app that acts as a Bluetooth Low Energy (BLE) peripheral. The app runs on a Nexus 9 which supports BLE peripheral mode. So far I've managed to advertise a service and allow another BLE device connect to it successfully. However, it is fails when trying to read the characteristic value.
I've checked that the characteristic has the read property and that the read permission is set.
When using the LightBlue iOS app (https://itunes.apple.com/gb/app/lightblue-bluetooth-low-energy/id557428110?mt=8) I manage to discover and connect to my Nexus and see the characteristic uuid but the value doesn't show.
Any help would be highly appreciated.
First check the characteristic data you are advertising in the peripheral mode usually there are three modes
BluetoothGattCharacteristic.PROPERTY_WRITE, BluetoothGattCharacteristic.PROPERTY_READ,
BluetoothGattCharacteristic.PROPERTY_NOTIFY;
and you can build characteristic with all modes using
BluetoothGattCharacteristic.PROPERTY_WRITE |BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY;
Once done look for onCharacteristicWriteRequest() in BluetoothGattServerCallback() that you gave while building characteristic. When central want to send data it can write data to the characteristic with WRITE mode and you will have onCharacteristicWriteRequest() callback method triggered at peripheral side and you will have data in byte[] and make sure send response using btGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, null);
by checking the responseNeeded bool value in callback method. In this way the data is transferred from central to peripheral.
And to send data from peripheral to central use notification charatertertistc
BluetoothGattCharacteristic bgc = bluetoothGattService
.getCharacteristic(chartersticUUID);
bgc.setValue(bnd.data);
btGattServer.notifyCharacteristicChanged(centralbluetoothdevice, bgc, false);
.

BLE notification never received on android 4.4

I am developing an application in android for communicating with Blugiga BLE 112. Here my BLE device is designed to provide data to android as Indications, I can successfully write characteristic but i am not getting any indications on android. Following is what i tried for setting notification.
public void setCharectresticNotification(
BluetoothGattCharacteristic characteristic,
BluetoothGattDescriptor descriptor) {
mBluetoothGatt.setCharacteristicNotification(characteristic, true);
descriptor
.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
I am calling this method once i get the characteristic and descriptor successfully.
Also please provide the flow i should be following to set notification once connection has been established.

Categories

Resources