0

I am trying to get data from a glucose meter and I am not able to find good resources regarding the implementation on internet. Here is what I have been able to implement till now:

I am scanning the devices using BluetoothAdapter.LeScanCallback Interface:

@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
    if ((device.getName() != null) && !bleDevices.contains(device)) {
        bleDevices.add(device);
    }
}

After getting the devices I am connecting to device using:

device.connectGatt(MainActivity.this, true, bleGattCallBack);

In BluetoothGattCallback class I am able to get the status connected in:

@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
                                    int newState) {
    if (newState == BluetoothProfile.STATE_CONNECTED) {
        connectionState = STATE_CONNECTED;
        gatt.discoverServices();
    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
        connectionState = STATE_DISCONNECTED;
        gatt.close();
    }
}

After that onServicesDiscovered gets called

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        List<BluetoothGattService> gattServices = gatt.getServices();
        for (BluetoothGattService gattService : gattServices) {
            String serviceUUID = gattService.getUuid().toString();

            if (serviceUUID.equals("00001808-0000-1000-8000-00805f9b34fb")) {
                List<BluetoothGattCharacteristic> characteristics = gattService.getCharacteristics();
                for (BluetoothGattCharacteristic characteristic : characteristics) {
                    if (characteristic.getUuid().equals(UUID.fromString("00002a18-0000-1000-8000-00805f9b34fb"))) {
                        BluetoothGattCharacteristic charGM =
                                gatt.getService(UUID.fromString("00001808-0000-1000-8000-00805f9b34fb"))
                                        .getCharacteristic(UUID.fromString("00002a18-0000-1000-8000-00805f9b34fb"));
                        gatt.setCharacteristicNotification(charGM, true);
                        glucoseCharacteristic = characteristic;

                        BluetoothGattDescriptor descGM = charGM.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
                        descGM.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                        gatt.writeDescriptor(descGM);
                    } else if (characteristic.getUuid().equals(UUID.fromString("00002a52-0000-1000-8000-00805f9b34fb"))) {
                        BluetoothGattCharacteristic charRACP =
                                gatt.getService(UUID.fromString("00001808-0000-1000-8000-00805f9b34fb"))
                                        .getCharacteristic(UUID.fromString("00002a52-0000-1000-8000-00805f9b34fb"));
                        gatt.setCharacteristicNotification(charRACP, true);
                        BluetoothGattDescriptor descRACP = charRACP.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
                        descRACP.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                        gatt.writeDescriptor(descRACP);
                    }
                }
            }
    } else {

    }
}

After this onDescriptorWrite gets called:

@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    super.onDescriptorWrite(gatt, descriptor, status);
    if (status == BluetoothGatt.GATT_SUCCESS) {
        Log.d(TAG, "onDescriptorWrite: GATT_SUCCESS");
        BluetoothGattCharacteristic writeRACPchar =
                gatt.getService(UUID.fromString("00001808-0000-1000-8000-00805f9b34fb"))
                        .getCharacteristic(UUID.fromString("00002a52-0000-1000-8000-00805f9b34fb"));
        byte[] data = new byte[2];
        data[0] = 0x01; // Report Stored records
        data[1] = 0x01; // All records
        writeRACPchar.setValue(data);
        gatt.writeCharacteristic(writeRACPchar);
    } else if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
        if (gatt.getDevice().getBondState() != BluetoothDevice.BOND_NONE) {
            Log.d(TAG, "onDescriptorWrite: GATT_INSUFFICIENT_AUTHENTICATION");
        }
    } else {
        Log.d(TAG, "onDescriptorWrite: GATT_INSUFFICIENT_AUTHENTICATION");
    }
}

and then onCharacteristicWrite

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    super.onCharacteristicWrite(gatt, characteristic, status);

    final byte[] data = characteristic.getValue();
    if (data != null && data.length > 0) {
        final StringBuilder stringBuilder = new StringBuilder(data.length);
        for (byte byteChar : data)
            stringBuilder.append(String.format("%02X ", byteChar));
        Log.d(TAG, "onCharacteristicWrite: " + status + "\n" + characteristic.getUuid() + "\n" + stringBuilder.toString() + "\n" + characteristic.getValue().length);
    }
}

Now, how do I proceed further? I know that I will get the data in onCharacteristicRead but that is never called.

Shubham Anand
  • 551
  • 2
  • 7
  • 19
  • Here's the process in [Not receiving data from BLE device](https://stackoverflow.com/questions/32779643/not-receiving-data-from-ble-device/32955582#32955582) – Markus Kauppinen Mar 20 '20 at 12:25
  • You can't directly read characteristic. I can't see role of descriptor or notification in your explanation. – Shubham Anand Mar 20 '20 at 13:11
  • I haven't played around with BLE for a while now, but in `onServicesDiscovered()` you (supposedly) get `BluetoothGatt gatt`. Does `gatt.getServices()` there return anything? You should get one or more `services`. And they then have `characteristics` which are available via `service.getCharacteristics()`. And then you would read the desired `characteristic`. And of course a glucose meter probaby follows standard services and characteristics defined in some BLE specification. – Markus Kauppinen Mar 20 '20 at 14:40
  • Notifications are a different thing then. Your options are to a) just read a single value or b) "subscribe to a characteristic" and you'll then get notifications whenever the value changes (and the connection still exists). – Markus Kauppinen Mar 20 '20 at 14:42
  • Hello @MarkusKauppinen , sorry I was sick so couldn't reply. Yes I am getting all the services. Using those services I am trying to get the data but data is coming null. – Shubham Anand Mar 25 '20 at 07:15

0 Answers0