0
votes

I am trying to receive the data from BLE device in my app. i could able to connect the BLE device successfully with my app and i am able to find the services provided by the BLE devices.

I can able to write data in characteristics and able to enable notification for particular service successfully and its returning TRUE. Issue is after gatt.writeCharacteristic(characteristic) successfully it should call onCharacteristicChanged override method. But its not calling that method. from that method only i can able to receive the data from BLE device.

I Followed below url

Android BLE

Note: By using service , i am invoking establishing GATT connection.

 private class BleGattCallback extends BluetoothGattCallback {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

            switch (newState) {
                case BluetoothProfile.STATE_CONNECTED:

                     gatt.discoverServices();

                    break;

                case BluetoothProfile.STATE_DISCONNECTED:
                    Log.d(TAG, "BluetoothProfile.STATE_DISCONNECTED");
                    gatt.close();
                    break;

                default:
                    break;
            }
        }


        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            Log.d(TAG, "onServicesDiscovered");

            if (status == BluetoothGatt.GATT_SUCCESS) {
                BluetoothGattService service = gatt.getService(SERVICE_UUID);
                if (service != null) {

                    BluetoothGattCharacteristic characteristic = service.getCharacteristic(READING_UUID);
                    if (characteristic != null) {
                        Log.d(TAG, " Subscribe Characteristic Notification UUID :  "+characteristic.getUuid());

                        gatt.setCharacteristicNotification(characteristic, true);

                        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UUID);
                        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

                        boolean success = gatt.writeDescriptor(descriptor);


                        Log.d(TAG, "writeDescriptor Status : " + success);
                    }
                }
            }
        }


        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);

            Toast.makeText(mContext,"onCharacteristicChanged",Toast.LENGTH_LONG).show();
            // read Value
        }



        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);


            if (status == BluetoothGatt.GATT_SUCCESS) {

                BluetoothGattService service = gatt.getService(SERVICE_UUID);
                if (service != null) {
                    BluetoothGattCharacteristic characteristic = service.getCharacteristic(INDEX_UUID);
                    Log.d(TAG, "onDescriptorWrite  success UUID for "+characteristic.getUuid());
                    if (characteristic != null) {
                        characteristic.setValue(new byte[] {0x03, 0x00});
                        gatt.writeCharacteristic(characteristic);
                    }
                }
            }
        }


    }
1

1 Answers

0
votes

No. onCharacteristicChanged is not called after you write a value to a characteristic. onCharacteristicWrite is called.

onCharacteristicChanged is called when a notification or indication has been received.