BLE notification never received on android 4.4 - android

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.

Related

Failed to subscribe to notification characteristic in GATT from UWP to 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.

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 4.0 getting the broadcast data from device to phone

I have two devices. one Android phone with API level more than 18 and other is blue-tooth device 4.0.
Devices are successfully connected to each other.
Now flow of command is as follow:
a. Send the "hello" text to blue-tooth device.
UUID uuid = UUID.fromString("18cda784-4bd3-4370-85bb-bfed91ec86af");
BluetoothGattCharacteristic selectedChar = selectedGattService.getCharacteristic(uuid);
mBluetoothLeService.setCharacteristicNotification(selectedChar, true);
boolean flag = selectedChar.setValue("");
mBluetoothLeService.writeCharacteristic(selectedChar);
In this case I am getting hello through the GATT reciver. what is the meaning of this.
registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
private static IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
intentFilter.addAction(BluetoothLeService.EXTRA_DATA);
return intentFilter;
}
b. bluetooth device will perform some operations
auto done by bluetooth device
c. Result of operation is sent to android phone
Brodcated by device.
For this I used notification.
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
return;
}
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID
.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
System.out.println("nov7 descriptordescriptor " + descriptor);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}
I am not getting any data. Any idea please.
saw your email, I think you are somehow connected via Bluetooth classic, but are then attempting to 'chat' on BTLE protocol.
That's the problem.
There is almost no way an Android 4.0 device has BTLE.
Even if it has an BTLE chip(there was some early Motorola phones with BTLE - you had to import a .jar from Motorola Inc.), it wouldn't use the Android BTLE API you seem to use.
So to make a long story short, you should either be using Bluetooth Classic (SPP) with the normal BluetoothSocket, or be using two Android BTLE devices.
Here is how to check if the devices have BTLE:\
If you want to declare that your app is available to BLE-capable devices only, include the following in your app's manifest:
However, if you want to make your app available to devices that don't support BLE, you should still > include this element in your app's manifest, but set required="false". Then at run-time you can determine BLE availability by using PackageManager.hasSystemFeature():
// Use this check to determine whether BLE is supported on the device.
// Then you can selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
Two things I see that you should be doing.
BluetoothGatt has its own setCharacteristicNotification method. In addition to writing the characteristic descriptor to enable notifications, you need to call that method to enable notifications. Think of it as writing the descriptor enables notifications on the BLE device and setCharacteristicNotification enables it on the Android device.
So in your setCharacteristicNotification method above I would add the following:
// I'm assuming you have access to the BluetoothGatt object in your BluetoothGattService object
gatt.setCharacteristicNotification(characteristic, true);
You shouldn't be trying to write any data to the characteristic until you have received confirmation that the descriptor was written. That means you need to wait until you get the callback to onDescriptorWrite in your implementation of BluetoothGattCallback.

Categories

Resources