0
votes

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);
                    };
                });*/
            }
        }
    }
1
try adding onCharacteristicChanged() callbackAmrit kumar
@Amrit kumar I already onCharacteristicChanged() callback but still its not working.Jayasree
did you enable notifications for this characteristics, please post your codeAmrit kumar
I posted the code by editing my question. Please help me out @Amrit kumar.Jayasree

1 Answers

1
votes

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] = '-';
        }
    }
}