繁体   English   中英

在BLE Android中写入后无法获得读取特性

[英]Not able to get read characteristic after write in BLE Android

我正在使用BLE设备。 -我能够使用BluetoothGATT将此设备与应用程序连接-与BluetoothGatt连接后,我能够首次读取所有服务,特征和广告商数据。 -我们有两个特征,一个是我们需要阅读的,另一个是写作,我能够写作。

但是写入后我无法读取特征的问题。 根据我的理解

        @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic)

要么

        @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status)

应该自动调用,这些是回调方法

我的特征写代码是

boolean status = mBluetoothGatt.setCharacteristicNotification(characteristic, state);

我也通过以下代码启用通知

        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

写作后我停留在阅读特征部分

可以提供任何帮助,谢谢您,亲爱的,我在这一点上停留了3天。

onCharacteristicRead()和onCharacteristicChanged() 不会自动调用。

调用character.getValue()时会触发onCharacteristicRead。 像这样:

BluetoothGattCharacteristic charac = ... ;
byte[] messageBytes = charac.getValue();

凡持有CHARAC的特点你想阅读并messageBytes拥有的特征读取值。

启用通知后,将调用onCharacteristicChanged。 您似乎没有完全启用通知。

public static String CLIENT_CONFIGURATION_DESCRIPTOR_STRING = "00002902-0000-1000-8000-00805f9b34fb";

...

            mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                    UUID.fromString(CLIENT_CONFIGURATION_DESCRIPTOR_STRING));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mBluetoothgatt.writeDescriptor(descriptor);

您需要添加writeDescriptor调用才能成功启用通知。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM