onCharacteristicChanged не вызывается в службах Android BLE GATT

Я пытаюсь получить данные с устройства BLE в своем приложении. я смог успешно подключить устройство BLE к своему приложению, и я могу найти услуги, предоставляемые устройствами BLE.

Я могу записывать данные в характеристики и успешно включать уведомление для конкретной службы и возвращать TRUE. Проблема заключается в том, что после успешного выполнения gatt.writeCharacteristic(characteristic) должен вызываться метод переопределения onCharacteristicChanged. Но это не вызов этого метода. только с помощью этого метода я могу получать данные с устройства BLE.

Я подписался ниже URL

Android BLE

Примечание. Используя службу, я вызываю установление соединения GATT.

 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);
                    }
                }
            }
        }


    }

person karthik selvaraj    schedule 18.11.2016    source источник


Ответы (1)


Нет. onCharacteristicChanged не вызывается после того, как вы записываете значение в характеристику. onCharacteristicWrite вызывается.

onCharacteristicChanged вызывается при получении уведомления или индикации.

person Emil    schedule 19.11.2016
comment
но я установил уведомление для символов (READING_UUID), и только потом я пишу в символы (INDEX_UUID). - person karthik selvaraj; 19.11.2016
comment
Вы должны переопределить onCharacteristicWrite, чтобы дождаться завершения записи. - person Emil; 19.11.2016