【问题标题】:Not able to get read characteristic after write in BLE Android在 BLE Android 中写入后无法获得读取特性
【发布时间】:2018-07-15 18:11:34
【问题描述】:

我正在开发 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 天。

【问题讨论】:

    标签: android bluetooth bluetooth-lowenergy bluetooth-gatt


    【解决方案1】:

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

    onCharacteristicRead 在您调用characteristic.getValue() 时触发。 像这样的:

    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 调用才能成功启用通知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      相关资源
      最近更新 更多