Cannot read characteristic. Android BLE

12,366

you need first enable notifications for characteristic and then try to read it's value and remember to get the appropiate format of the byte array that return characteristic.getValue() method. Regards

Share:
12,366
Magic
Author by

Magic

Updated on June 12, 2022

Comments

  • Magic
    Magic almost 2 years

    I'd like to read the data from a specific characteristic of my remote BLE device to my Android tablet Nexus 7.

    The problem is that, I can receive the data by enabling the notification of that characteristic even without calling readCharacteristic. But I cannot successfully read characteristic by calling readCharacteristicwithout enabling the notification.

    mBluetoothGatt.readCharacteristic(characteristic) returns false. Thus the function onCharacteristicRead has never been triggered. I also checked the property value BluetoothGattCharacteristic.PROPERTY_READ, it is 30.

    Does anyone have some ideas about what is going on here? I really need to read the characteristic separately. Because if I only analysis data based on notification, I can not figure out where the data begins. This is because that my device will send 12bytes each time. And it will continuously sending the byte array. However, the notification would bring me the data one byte a time. So I don't know which is the beginning byte of the byte array.

    I'm using the sample code provided by Android right now.

    Here is the snippet:

    public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
    
        boolean status = mBluetoothGatt.readCharacteristic(characteristic);
        System.out.println("Initialize reading process status:" + status);
    
    }
    
    
    
    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
    
        // This is specific to Heart Rate Measurement.
        if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                    UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 
    
            mBluetoothGatt.writeDescriptor(descriptor);
        }
    }
    

    The code in callback is:

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic,
                                         int status) {
    
            System.out.println("In onCharacteristicRead!!!!!!!");
            if (status == BluetoothGatt.GATT_SUCCESS) {
    
                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
                System.out.println("Received Data Success!!!!!!");
            }
        }
    

    I have read through the document of reading characteristic, but nothing helps. Can anyone help me? Thank you very much!

  • Magic
    Magic almost 10 years
    Hi, thanks for your respond. I have tried enable the notification first, and then read characteristic. It turns out that only "onCharacteristicChanged" get called when data is ready on the device, the function "onCharacteristicRead" still remains uncalled. Do you have any idea about this? Thank you.
  • J Sanchez
    J Sanchez almost 10 years
    you also can try to get the characteristic permissions in order to make sure if your characteristic is readable, if that doesn't work maybe it's something else, or try to read the value between try/catch block to see if throws an exception. hope to help
  • Larry Lo
    Larry Lo over 9 years
    Have you written this setNotification as Descriptor ?