0

Hi i am trying to read the data from BLE Blood glucose meter. when I am trying to read the characteristics of "00002a18-0000-1000-8000-00805f9b34fb" which is nothing but BLOOD_GLUCOSE_MEASUREMENT UUID, characteristic.getProperties method returns 16 and my onCharacteristicRead method itself is not getting called. Please help me how to read BLOOD_GLUCOSE_MEASUREMENT characteristics.

private final ExpandableListView.OnChildClickListener servicesListClickListner =
            new ExpandableListView.OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                            int childPosition, long id) {
                    if (mGattCharacteristics != null) {
                        final BluetoothGattCharacteristic characteristic =
                                mGattCharacteristics.get(groupPosition).get(childPosition);
                        final int charaProp = characteristic.getProperties();
                        if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
                            // If there is an active notification on a characteristic, clear
                            // it first so it doesn't update the data field on the user interface.
                            if (mNotifyCharacteristic != null) {
                                mBluetoothLeService.setCharacteristicNotification(
                                        mNotifyCharacteristic, false);
                                mNotifyCharacteristic = null;
                            }
                            mBluetoothLeService.readCharacteristic(characteristic);
                        }
                        if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                            mNotifyCharacteristic = characteristic;
                            mBluetoothLeService.setCharacteristicNotification(
                                    characteristic, true);
                        }
                        return true;
                    }
                    return false;
                }
    };

And my readCharacteristic method is

public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
        final byte[] data = characteristic.getValue();

    if(data != null && data.length > 0){
        final char[] out = new char[data.length * 3 - 1];
        for(int j = 0; j < data.length; j++) {
            int v = data[j] & 0xFF;
            out[j * 3] = HEX_ARRAY[v >>> 4];
            out[j * 3 + 1] = HEX_ARRAY[v & 0x0F];
            if(j != data.length - 1)
                out[j * 3 + 2] = '-';
        }
    }

        mBluetoothGatt.readCharacteristic(characteristic);
    }

And my setCharacteristicNotification method is this:

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

        for (BluetoothGattService service : mBluetoothGatt.getServices()) {
            if ((service == null) || (service.getUuid() == null)) {
                continue;
            }
            if (SampleGattAttributes.BLOOD_GLUCOSE_SERVICE.equalsIgnoreCase(service
                    .getUuid().toString())) {

                BluetoothGattCharacteristic charGM =
                        mBluetoothGatt.getService(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_SERVICE))
                                .getCharacteristic(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_MEASUREMENT));
                mBluetoothGatt.setCharacteristicNotification(charGM, enabled);
                BluetoothGattDescriptor descGM = charGM.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
                descGM.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                mBluetoothGatt.writeDescriptor(descGM);

                BluetoothGattCharacteristic charGMC =
                        mBluetoothGatt.getService(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_SERVICE))
                                .getCharacteristic(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_MEASUREMENT_context));
                mBluetoothGatt.setCharacteristicNotification(charGMC, enabled);
                BluetoothGattDescriptor descGMC = charGMC.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
                descGMC.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                mBluetoothGatt.writeDescriptor(descGMC);

                BluetoothGattCharacteristic charRACP =
                        mBluetoothGatt.getService(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_SERVICE))
                                .getCharacteristic(UUID.fromString(SampleGattAttributes.RECORD_ACCESS_CONTROL_POINT));
                mBluetoothGatt.setCharacteristicNotification(charRACP, enabled);
                BluetoothGattDescriptor descRACP = charRACP.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
                descRACP.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                mBluetoothGatt.writeDescriptor(descRACP);


                /*BluetoothGattCharacteristic charBarrery =
                        mBluetoothGatt.getService(UUID.fromString(SampleGattAttributes.SERVICE_BATTERY_SERVICE))
                                .getCharacteristic(UUID.fromString(SampleGattAttributes.CHAR_BATTERY_LEVEL));
                mBluetoothGatt.setCharacteristicNotification(charBarrery, enabled);
                BluetoothGattDescriptor descBarrery = charBarrery.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
                descBarrery.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                mBluetoothGatt.writeDescriptor(descBarrery);*/

               /* runOnUiThread(new Runnable() {
                    public void run() {
                        btnUpdateData.setEnabled(true);
                    };
                });*/
            }
        }
    }
Jayasree
  • 21
  • 4

2 Answers2

1

characteristic.getProperties() returns 16,

which is the value of BluetoothGattCharacteristic.PROPERTY_NOTIFY.

You have set the characteristic notification,

if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
    mNotifyCharacteristic = characteristic;
    mBluetoothLeService.setCharacteristicNotification(
        characteristic, true);
}

You need to call readCharacteristic(desiredCharacteristic) now.

Or/And on onDescriptorWrite() callback.

Edit:

After calling

mBluetoothGatt.readCharacteristic(characteristic);

You need to receive the data on onCharacteristicRead() callback

onCharacteristicRead(BluetoothGatt gatt, ..., ...){
    final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
    final byte[] data = characteristic.getValue();

    if(data != null && data.length > 0){
        final char[] out = new char[data.length * 3 - 1];
        for(int j = 0; j < data.length; j++) {
            int v = data[j] & 0xFF;
            out[j * 3] = HEX_ARRAY[v >>> 4];
            out[j * 3 + 1] = HEX_ARRAY[v & 0x0F];
            if(j != data.length - 1)
                out[j * 3 + 2] = '-';
        }
    }
}
Amrit kumar
  • 466
  • 6
  • 9
  • ok @Amrit kumar thanks for your time and help. But I have one question how can we pass BluetoothGattCharacteristic on onDescriptorWrite() callback? – Jayasree Aug 01 '18 at 09:44
  • hi @Amrit kumar I tried calling readCharacteristic(desiredCharacteristic) method after if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0){} but it didnt work.. – Jayasree Aug 01 '18 at 10:23
  • You also need to change the | to a &. Otherwise the if statement will always be true. – Emil Aug 01 '18 at 17:59
  • No @Amrit kumar my onCharacteristicRead callback itself is not getting called. – Jayasree Aug 03 '18 at 05:44
  • https://stackoverflow.com/questions/25865587/android-4-3-bluetooth-ble-dont-called-oncharacteristicread Please follow this – Amrit kumar Aug 03 '18 at 06:09
1

16 is the constant value for notify. You can use this function check wether or not a characteristic usage fits yours. attr should be filled with one of these constant values:

 public static final int PROPERTY_EXTENDED_PROPS = 128;
 public static final int PROPERTY_INDICATE = 32;
 public static final int PROPERTY_NOTIFY = 16;
 public static final int PROPERTY_READ = 2;
 public static final int PROPERTY_SIGNED_WRITE = 64;
 public static final int PROPERTY_WRITE = 8;
 public static final int PROPERTY_WRITE_NO_RESPONSE = 4;

 private boolean checkForCharacteristicProperty(BluetoothGattCharacteristic chara, int attr) {
     return chara != null && ((chara.getProperties() & attr) != 0);
 }
 
Symtox
  • 77
  • 5