【问题标题】:onCharacteristicChanged is not called in Android BLE GATT servcies在 Android BLE GATT 服务中未调用 onCharacteristicChanged
【发布时间】:2017-04-02 03:53:40
【问题描述】:

我正在尝试在我的应用中接收来自 BLE 设备的数据。我可以成功地将 BLE 设备与我的应用程序连接起来,并且我能够找到 BLE 设备提供的服务。

我可以在特征中写入数据,并能够成功启用特定服务的通知及其返回TRUE。问题是在 gatt.writeCharacteristic(characteristic) 成功后它应该调用 onCharacteristicChanged 覆盖方法。但它没有调用该方法。 只有通过这种方法,我才能从 BLE 设备接收数据。

我关注了以下网址

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


    }

【问题讨论】:

    标签: android bluetooth-lowenergy android-bluetooth gatt android-service-binding


    【解决方案1】:

    没有。将值写入特征后,不会调用 onCharacteristicChanged。 onCharacteristicWrite 被调用。

    在收到通知或指示时调用 onCharacteristicChanged。

    【讨论】:

    • 但是我已经为characteristics (READING_UUID)设置了一个通知,然后只有我写到characterstics(INDEX_UUID)。
    • 您应该重写 onCharacteristicWrite 以便等到写入完成。
    猜你喜欢
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多