Kumar Raj
Kumar Raj

Reputation: 124

onCharacteristicChanged is not called after writing on ble

I need some response data from Ble. When I am writing something on ble I need to read response data from Ble. I am able to successfully enable and disable my ble device but only missing response data from ble. I also need to convert decimal time into Integer hex format like for 60 min into 0x3c.

private BluetoothGattCallback gattCallback= new BluetoothGattCallback() {

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt,status);
            clientGatt =gatt;
            if (status == BluetoothGatt.GATT_SUCCESS) {
                BluetoothGattService service = gatt.getServices().get(2);
                List<BluetoothGattCharacteristic> gattCharacteristics = service.getCharacteristics();
                for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {

                    if (gattCharacteristic.getUuid().toString().equalsIgnoreCase(AppConstant.RECEIVE_UUID)) {
                        readCharacteristic=gattCharacteristic;
                    }
                    if (gattCharacteristic.getUuid().toString().equalsIgnoreCase(AppConstant.SEND_UUID_STR)) {
                        writeCharacteristic = gattCharacteristic;
                    }
                }
            }

        }
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt,characteristic,status);
            LogUtils.errorLog("onCharacteristicRead", "@@: "+characteristic.getValue()[0]);

        }
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic)

        }

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);

            setCharacteristicNotification(characteristic,true);
        }

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);

            clientGatt = gatt;

            switch (newState) {
                case BluetoothProfile.STATE_CONNECTED:
                    isBLE_Connected=true;
                    gatt.discoverServices();

                    break;
                case BluetoothProfile.STATE_DISCONNECTED:
                    isBLE_Connected=false;
                    if(status==133 || status==22 || status==62){
                        refreshDeviceCache();
                        clientGatt.discoverServices();
                    }else{
                        clientGatt.disconnect();
                        clientGatt.close();
                    }
                    break;
            }

        }
    };

Upvotes: 3

Views: 5164

Answers (2)

Manoj Singh Rawal
Manoj Singh Rawal

Reputation: 469

You when you write something on characteristic with the help of BluetoothGattDescriptor as

Mike's answer

You need to override the following method to listen to the changes has been completed then you can read the characteristics:

 @Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    super.onDescriptorWrite(gatt, descriptor, status);
    Log.d(TAG, "onDescriptorWrite :" + ((status == BluetoothGatt.GATT_SUCCESS) ? "Sucess" : "false"));
}

Upvotes: -1

Mike
Mike

Reputation: 4288

Before onCharacteristicChanged is called you had to enable notification. Someting like:

   //Enable local notifications
    gatt.setCharacteristicNotification(characteristic, true);
    //Enabled remote notifications
    BluetoothGattDescriptor desc =characteristic.getDescriptor(CONFIG_DESCRIPTOR);
    desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    gatt.writeDescriptor(desc);

should help.

Upvotes: 5

Related Questions