Android fails to subscribe to multiple characteristics within service - android

I am trying to subscribe to multiple characteristics within the service and obtain notifications for both of the characteristics.
Here is my version with 1 char:
#Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
List<BluetoothGattCharacteristic> characteristics = gatt.getServices().get(2).getCharacteristics();
BluetoothGattCharacteristic characteristic = characteristics.get(0);
gatt.setCharacteristicNotification(characteristic, true);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
BluetoothGattDescriptor descriptor = characteristic.getDescriptors().get(0);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
#Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
if (characteristic.getUuid().equals(characteristicIDs[0])) {
Log.i(TAG,"Char for Beacons");
int threshValue = characteristic.getValue()[0];
sendBeaconToCloud(threshValue);
} else if (characteristic.getUuid().equals(characteristicIDs[1])) {
Log.d(TAG,"Char for Sensors");
convertSensorValues(characteristic.getValue());
} else {
Toast.makeText(BluetoothConnActivity.this,"Unknown char", Toast.LENGTH_SHORT).show();
}
}
So then I decided to copy the block of code within onServicesDiscovered and also
characterics.get(1)
This didn't work, so I followed the method specified in here:
How to subscribe to multiple BluetoothLE Characteristics with Android
So this is the produced code:
public List<BluetoothGattCharacteristic> chars;
public BluetoothGatt myGatt;
#Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
chars = gatt.getServices().get(2).getCharacteristics();
this.myGatt = gatt;
subscribeToMultiple();
}
private void subscribeToMultiple() {
if (chars.size() == 0) return;
BluetoothGattCharacteristic characteristic = chars.get(0);
myGatt.setCharacteristicNotification(characteristic, true);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
BluetoothGattDescriptor descriptor = characteristic.getDescriptors().get(0);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
myGatt.writeDescriptor(descriptor);
}
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt,descriptor,status);
chars.remove(0);
subscribeToMultiple();
}
However, I just get output:
D/BluetoothGatt: setCharacteristicNotification() - uuid: 0000a001-0000-1000-8000-00805f9b34fb enable: true
D/BluetoothGatt: setCharacteristicNotification() - uuid: 0000a002-0000-1000-8000-00805f9b34fb enable: true
but onCharacteristicChanged is never called.
I would really appreciate any help.

First you can set for 0.When you receive onCharacteristicChanged for characteristics 0 then unsubscribe from it and enable for the characteristic 1.when you receive for 1 unsubscribe for 1 and re enable 0. It worked for me once.

Related

How to get Heart Rate from Mi Band using Bluetooth Le

I want to get the Heart Rate through BluetoothLe from my Mi Band 2. I tried to follow the example from Getting Started with Bluetooth Low Energy but I didn't succeed to write the value from Heart Rate. I think I miss something but I cannot realize what. It's the first time I am working with bluetooth and smart band. I wish you could help me. I do not know if for starting the Heart Rate sensor should I use as {0x01} byte.
So following the example I downloaded the BluetoothLeDemo app and I got from there BleWrapper BleDefinedUUIDs and BleNamesResolver. I have 2 buttons Scan and stop. I created a BleWrapper, called mBleWrapper to use it for starting scanning and stop scanning.
mBleWrapper = new BleWrapper(this, new BleWrapperUiCallbacks.Null(){
#Override
public void uiDeviceConnected(BluetoothGatt gatt, BluetoothDevice device) {
super.uiDeviceConnected(gatt, device);
}
#Override
public void uiDeviceDisconnected(BluetoothGatt gatt, BluetoothDevice device) {
super.uiDeviceDisconnected(gatt, device);
}
#Override
public void uiAvailableServices(BluetoothGatt gatt, BluetoothDevice device, List<BluetoothGattService> services) {
super.uiAvailableServices(gatt, device, services);
BluetoothGattCharacteristic c=null;
for(BluetoothGattService service : services) {
String serviceName = BleNamesResolver.resolveUuid(service.getUuid().toString());
Log.i("SERVIDE", serviceName);
}
}
c=gatt.getService(BleDefinedUUIDs.Service.HEART_RATE).getCharacteristic(BleDefinedUUIDs.Characteristic.HEART_RATE_MEASUREMENT);
mBleWrapper.writeDataToCharacteristic(c, new byte[]{0x01});
mState = mSensorState.ACC_ENABLE;
}
#Override
public void uiCharacteristicForService(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattService service, List<BluetoothGattCharacteristic> chars) {
super.uiCharacteristicForService(gatt, device, service, chars);
}
#Override
public void uiCharacteristicsDetails(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattService service, BluetoothGattCharacteristic characteristic) {
super.uiCharacteristicsDetails(gatt, device, service, characteristic);
}
#Override
public void uiNewValueForCharacteristic(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattService service, BluetoothGattCharacteristic ch, String strValue, int intValue, byte[] rawValue, String timestamp) {
super.uiNewValueForCharacteristic(gatt, device, service, ch, strValue, intValue, rawValue, timestamp);
switch (mState) {
case ACC_READ:
Log.i("READ", "heart rate dta");
}
Log.i("Value", "Val" + intValue);
}
#Override
public void uiGotNotification(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattService service, BluetoothGattCharacteristic characteristic) {
super.uiGotNotification(gatt, device, service, characteristic);
String ch = BleNamesResolver.resolveCharacteristicName(characteristic.getUuid().toString());
Log.d("AAA", "uiGotNotification: " + ch);
}
#Override
public void uiSuccessfulWrite(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattService service, BluetoothGattCharacteristic ch, String description) {
super.uiSuccessfulWrite(gatt, device, service, ch, description);
BluetoothGattCharacteristic c;
switch (mState) {
case ACC_ENABLE:
Log.i("ENABLED", "Heart Rate enabled");
c = gatt.getService(BleDefinedUUIDs.Service.HEART_RATE).getCharacteristic(BleDefinedUUIDs.Characteristic.HEART_RATE_MEASUREMENT);
mBleWrapper.requestCharacteristicValue(c);
mState = mSensorState.ACC_READ;
break;
case ACC_READ:
Log.i("WRITE", "SUCCESSfule write");
break;
}
}
#Override
public void uiFailedWrite(BluetoothGatt gatt, BluetoothDevice device, BluetoothGattService service, BluetoothGattCharacteristic ch, String description) {
super.uiFailedWrite(gatt, device, service, ch, description);
switch (mState) {
case ACC_ENABLE:
Log.i("FAILED", "Feailed to enbale Heart Rate");
}
}
#Override
public void uiNewRssiAvailable(BluetoothGatt gatt, BluetoothDevice device, int rssi) {
super.uiNewRssiAvailable(gatt, device, rssi);
}
#Override
public void uiDeviceFound(BluetoothDevice device, int rssi, byte[] record) {
super.uiDeviceFound(device, rssi, record);
String msg = "uiDeviceFound: "+device.getAddress()+","+device.getName();
if(devicesList.contains(device)==false) {
Log.i("Devicce", "deviceFound: " + msg);
devicesList.add(device);
genericListAdapter.notifyDataSetChanged();
}
}
});
I display all devices found with a ListView and onClick I want to connect to that item and write the Heart Rate in the Log.
deviceListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
BluetoothGatt gatt;
BluetoothGattCharacteristic c;
connectDevice((BluetoothDevice) parent.getItemAtPosition(position));
Log.i("Read", "Get Heart Rate");
if(mBleWrapper.isConnected()==false){
Log.i("not connected", "NOT CONNECTE");
return;
}
gatt = mBleWrapper.getGatt();
c = gatt.getService(BleDefinedUUIDs.Service.HEART_RATE).getCharacteristic(BleDefinedUUIDs.Characteristic.HEART_RATE_MEASUREMENT);
mBleWrapper.requestCharacteristicValue(c);
mState=mSensorState.ACC_READ;
}
});
I also added at BleWrapper class onDescpritorWriter
#Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
String deviceName = gatt.getDevice().getName();
String serviceName = BleNamesResolver.resolveServiceName(descriptor.getCharacteristic().getService().getUuid().toString().toLowerCase(Locale.getDefault()));
String charName = BleNamesResolver.resolveCharacteristicName(descriptor.getCharacteristic().getUuid().toString().toLowerCase(Locale.getDefault()));
String description = "Device: " + deviceName + " Service: " + serviceName + " Characteristic: " + charName;
if(status == BluetoothGatt.GATT_SUCCESS) {
mUiCallback.uiSuccessfulWrite(mBluetoothGatt, mBluetoothDevice, mBluetoothSelectedService, descriptor.getCharacteristic(), description);
}
else {
mUiCallback.uiFailedWrite(mBluetoothGatt, mBluetoothDevice, mBluetoothSelectedService, descriptor.getCharacteristic(), description + " STATUS = " + status);
}
}
And for UUIDS for service I used
UUID HEART_RATE = UUID.fromString("0000180d-0000-1000-8000-00805f9b34fb");
and for Charachteristics
UUID HEART_RATE_MEASUREMENT = UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb");
In other examples I saw that they also used the UUIDS for descriptor, but me not. I know that is a long post and question, but please I really need some help.
There are something you can check around.
Obviously, you have successfully discovered devices around. According to this(Bluetooth official document) heart rate service (UUID:180d) need set notify to TRUE first. Something like:
Use UUID:00002902-0000-1000-8000-00805f9b34fb to get the descriptor of the characteristic.
BluetoothGattDescriptor descriptor = gattCharacteristic.getDescriptor(Client_Characteristic_Configuration);
Set the feature (Notify) of descriptor to TRUE:
descriptor.setValue((BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE));
Write the value to the BLE device (using Gatt object from the callback function)
gatt.writeDescriptor(descriptor);
After above, you may get the data in override function:
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)
By this way:
BluetoothGattCharacteristic Char = gatt.getService(FORA_SERVICE_UUID).getCharacteristic(The uuid you want to connect);
byte[] data = Char.getValue();
And maybe you can show the example link. It can be more clear what you had used.

BLE setCharacteristicNotification() never call

I have a problem with CharacteristicNotification, I follow docs from android and search in many stackoverflow post.
All my code works until I try to get data from my device, when I subscribe to notification I get this log : BluetoothGatt: setCharacteristicNotification()
But my methods was never call
My UUID are good and my service send data I already check with other application.
I enable location for my app.
I have this permission in Manifest :
uses-permission android:name="android.permission.BLUETOOTH" android:required="true"
uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
I have try with 3 mobiles all are samsung but different Android version
I really don't understand what happen
Here my callback :
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
#Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
Log.i("gattCallback", "STATE_CONNECTED");
gatt.discoverServices();
break;
case BluetoothProfile.STATE_DISCONNECTED:
Log.e("gattCallback", "STATE_DISCONNECTED");
break;
default:
Log.e("gattCallback", "STATE_OTHER");
}
}
#Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.i("onServicesDiscovered", "connection to service");
BluetoothGattCharacteristic characteristic =
gatt.getService(UUID.fromString("a389d578-6285-4b3d-a9dc-b83a2ba4c095"))
.getCharacteristic(UUID.fromString("9ecbe31b-f77e-4886-a0a8-c416906387e7"));
gatt.readCharacteristic(characteristic);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805F9B34FB"));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
gatt.writeDescriptor(descriptor);
gatt.setCharacteristicNotification(characteristic, true);
}
#Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic
characteristic, int status) {
Log.i("onCharacteristicRead", "read");
}
#Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.i("onCharacteristicChanged", "changement");
}
};
Two errors:
1.
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
The second call overwrites the first value.
You must wait for the read callback before you start the descriptor write operation. Generally, you must always wait for the GATT callback before you can start the next operation.

Android BLE readCharacteristic returns always false

I am developing and application to read and write in a custom device through Bluetooth BLE. There is no problem for writing. I am able to establish a connections, discover the service and the characteristics.
Note the call to enableRXNotification
public void startReceiving() {
boolean readOk = gatt.readCharacteristic(rx);
if (!readOk) {
Log.d("ANUBEBT", "ERROR INITIATING READ CHARACTERISTIC");
}
}
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
// Notify connection failure if service discovery failed.
if (status == BluetoothGatt.GATT_FAILURE) {
connectFailure();
return;
}
// Save reference to each UART characteristic.
tx = gatt.getService(SERIAL_SERVICE_UUID).getCharacteristic(TX_CHAR_UUID);
rx = gatt.getService(SERIAL_SERVICE_UUID).getCharacteristic(RX_CHAR_UUID);
enableRXNotification();
// Notify of connection completion.
notifyOnConnected(context);
}
public boolean enableRXNotification() {
if (gatt == null) return false;
BluetoothGattService SerialService = gatt.getService(SERIAL_SERVICE_UUID);
if (SerialService == null) return false;
BluetoothGattCharacteristic RxChar = SerialService.getCharacteristic(RX_CHAR_UUID);
if (RxChar == null) {
Log.d("ANUBEBT", "Error getting rx characteristic");
connectFailure();
return false;
}
if (!setCharacteristicNotification(RxChar, true)) {
Log.d("ANUBEBT", "Error setting rx notification");
connectFailure();
return false;
}
return true;
}
After that, I have tried to call readCharacteristic but always returns false. I have tried several alternatives:
Press a button and call to readCharacteristic
call to readCharacteristic in the onCharacteristicWrite callback (just to make sure there is no pending operations)
Do you know what's going on here?
#Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.d("ANUBEBT", "onCharacteristicWrite " + status);
if (status == BluetoothGatt.GATT_SUCCESS) {
notifyOnCommunicationError(characteristic.getStringValue(0).length(), characteristic.getStringValue(0));
}
writeInProgress = false;
startReceiving();
}

Can't write to BLE device's characteristic anymore after reconnect

I'm writing printing data to the BluetoothGattCharacteristic of a Zebra ZD410 printer. I do this by chunking the data into 20 byte chunks and writing a chunk at a time with the following code:
mCharacteristic.setValue(bytes);
boolean status = mGatt.writeCharacteristic(mCharacteristic);
and then waiting until I receive BluetoothGattCallback.onCharacteristicWrite() before initiating the writing of the next chunk. This works fine.
If I disconnect() and close() the BluetoothGatt and later connect to the same device again with BluetoothDevice.connectGatt() and then try to write to the Characteristic after onServicesDiscovered() has been called is done and I have my Characteristic again, writing will fail. What I mean by this is that when I write to the Characteristic now, onCharacteristicWrite() will be called with a Characteristic who's getValue() returns the value of the last write on the old Gatt.
After trying to solve this for two days and reading tons of SO posts I haven't found a solution.
How can I fix this?
EDIT
Here is the code for the BluetoothGattCallback
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback()
{
#Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
{
FALog.i(TAG, "onConnectionStateChange Status: " + status);
switch (newState)
{
case BluetoothProfile.STATE_CONNECTED:
FALog.i(TAG, "gattCallback STATE_CONNECTED");
gatt.discoverServices();
break;
case BluetoothProfile.STATE_DISCONNECTED:
disconnectAndCloseGatt();
mCharacteristic = null;
connectionFailed();
FALog.e(TAG, "gattCallback STATE_DISCONNECTED");
break;
default:
FALog.e(TAG, "gattCallback STATE_OTHER");
}
}
#Override
public void onServicesDiscovered(BluetoothGatt gatt, int status)
{
BluetoothGattService service = gatt.getService(PRINTER_SERVICE_UUID);
if (service != null)
{
BluetoothGattCharacteristic characteristic = service.getCharacteristic
(PRINTER_SERVICE_CHARACTERISTIC_UUID);
if (characteristic != null)
{
mCharacteristic = characteristic;
mInternalState = STATE_CONNECTED;
mState = State.CONNECTED;
notifyStateChanged();
print("~JA");
FALog.d(TAG, "Printer connected");
mBluetoothActivity.runOnUiThread(new Runnable()
{
#Override
public void run()
{
mListener.onPrinterConnected();
}
});
}
}
}
#Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
{
FALog.d(TAG, "received onCharacteristicWrite " + new String(characteristic.getValue()) + "; success: " +
(status == BluetoothGatt.GATT_SUCCESS));
if (status == BluetoothGatt.GATT_SUCCESS)
{
handler.removeCallbacks(writeRunnable);
popQueueAndReleaseLock();
}
}
};
Try writeCharacteristic after the onDescriptorWrite() callback instead of onServicesDiscovered() callback. writeDescriptor holds the mDeviceBusy.

BluetoothGattDescriptor is always NULL

My CLIENT_CHARACTERISTIC_CONFIG is
public final static UUID CLIENT_CHARACTERISTIC_CONFIG = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
I do this to change CHARACTERISTIC:
BluetoothGatt mBluetoothGatt = device.connectGatt(this.context, false, mGattCallback);
mBluetoothGatt.connect();
BluetoothGattService mCustomService = bleScanner.getBluetoothGatt().getService(UUID.fromString(bleServiceUUID));
if(mCustomService == null){
return;
}
BluetoothGattCharacteristic mCharacteristic = mCustomService.getCharacteristic(UUID.fromString(bleCharacteristicUUID));
if(mReadCharacteristic == null){
return;
}
String message = "myMessage";
mCharacteristic.setValue(message.getBytes());
if (!mBluetoothGatt.writeCharacteristic(characteristic)) {
Log.e(TAG, "Failed to write characteristic");
} else {
Log.e(TAG, "Writed characteristic " + message);
}
After that characteristic is changed on sensor, onCharacteristicWrite is called in BluetoothGattCallback
#Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
if (!mBluetoothGatt.setCharacteristicNotification(characteristic, true)) {
DLog.e(TAG, "Notification Setup failed: "+characteristic.getUuid());
}
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
if (descriptor!=null){
descriptor.setValue( BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor1);
}
}
}
BUT descriptor is always NULL. And onCharacteristicChanged is never called.
Why? and is my CLIENT_CHARACTERISTIC_CONFIG right?
it was because some of devices (mine is Samsung E5 Android 4.3->updated to 5.1.1) are support BLE but doesn't support BluetoothGattDescriptor 0x2902 (see link)
to see all Descriptors which are supporter in device call this:
for (BluetoothGattDescriptor descriptor:characteristic.getDescriptors()){
Log.e(TAG, "BluetoothGattDescriptor: "+descriptor.getUuid().toString());
}
[UPDATE]
Some BLE devices doesn't support Writed characteristics, such as iBeacon

Categories

Resources