This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Android BLE : the callback function 'onCharacteristicRead' never called

I write a code for read the temperature via BLE. The code is modified from the example "android-BluetoothLeGatt-master". But the CallBack function onCharacteristicRead for the function readCharacteristic is not be fired and the app can not read any value. In order to make sure the remote device is available, I use the APP "nRF Connect" to read the data send from remote device, The data received by the "nRF Connect" is correct, hence the remote device is available. Following is the part of the code of : MainActivity.java

......................
private BLEService mBLEService;
......................
else if (BLEService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
    List<BluetoothGattService> gattServices = 
    mBLEService.getSupportedGattServices();
    searchTargetChara(gattServices);
    if (isMeasTempCharReadable ==true){
        BluetoothGattCharacteristic notifyTargetChar
            =meas_temperature_char;
            //meas_temperature_char is found via searchTargetChara
            if (notifyTargetChar != null) {
                mBLEService.setCharacteristicNotification(
                        notifyTargetChar, false);
                notifyTargetChar = null;
                Log.i("LOG_DCA","target Char Notify ="+String.valueOf(notifyTargetChar));
            }
            mBLEService.readCharacteristic(meas_temperature_char);                    
            prop_TempChar=meas_temperature_char.getProperties();
            if ((prop_TempChar | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0){                        
                mBLEService.setCharacteristicNotification(meas_temperature_char,true);
            }
        }
    }

The following the the part of the code of BLEService.java

............
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
                                 BluetoothGattCharacteristic characteristic,
                                 int status) {
	Log.i("LOG_BLEServ"," onCharacteristicRead status = "+String.valueOf(status));
	if (status == BluetoothGatt.GATT_SUCCESS) {
		broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);    
	}
}
......................
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
	Log.i("LOG_BLEServ","BLEServ readCharacteristic");
	if (mBluetoothAdapter == null || mBluetoothGatt == null) {      
	    //Log.w(TAG, "BluetoothAdapter not initialized");
	    return;
	}
	mBluetoothGatt.readCharacteristic(characteristic);
	String stg_read_uuid = characteristic.getUuid().toString();
	Log.i("LOG_BLEServ","read Characteristic UUID ="+stg_read_uuid);
	Log.i("LOG_BLEServ ","readCharacteristic finish");
}	

Accroding the information of Log, the function readCharacteristic is called correctly and get the correct characteristic UUID. BTW, before I update the android studio to version2.3, my code can run correctly. But now, even the example "android-BluetoothLeGatt-master" can not fire the callback onCharacteristicRead also.But I am not sure that the problem is caused by the update.

Before ask this question, I have search the similar issue. But it seems not to solve the problem. Thanks for your help and suggestion.

Best Wishes, thank you very much.

Related