gpt4 book ai didi

java - 安卓蓝牙 : Identify the Characteristic Type?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:03:17 24 4
gpt4 key购买 nike

我正在使用 BLE 开发一个安卓应用程序。此应用程序的要求是更新具有各种输入的特定硬件中的电压变化。所以我在这个应用程序中启用了 BLE 通知 API。这将在一段时间内通知应用程序最新的硬件电压。

实现

mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor des = characteristic.getDescriptors();
des.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
//Set the value of the descriptor to enable notification
mBluetoothGatt.writeDescriptor(des);

我在 Gatt CallBack 方法的通知值中收到通知

      @Override
public void onCharacteristicChanged(BluetoothGatt Gatt, BluetoothGattCharacteristic characteristic) {
Log.w(TAG, "**ACTION_DATA_AVAILABLE**" + characteristic.getUuid());
//Indication or notification was received
broadcastUpdate(BLEConstants.ACTION_DATA_AVAILABLE, characteristic);
//Go broadcast an intent with the characteristic data
}

但我的问题是,我也在同一个 Gatt 回调方法中得到正常响应。我想将通知更新为 UI 中的特定方式。所以我需要将正常响应和通知分开。有没有办法做同样的事情?或者识别特定消息的任何选项来自通知?

最佳答案

一般来说,我们(在硬件端)创建一个同时具有 WRITE 和 NOTIFY 属性的特征,以便我们可以随时读取或完全启用通知以获取实时数据。

如果您可以访问硬件固件并且可以添加特性,那么最好将电压特性和响应特性分开。因此,您可以测试 onCharacteristicChanged 参数:

int propertiesFlags = characteristic.getUuid();

另一个好方法,也是我通常做的,是使用一个特征,但分配数据间隔。您的应用程序和硬件之间的某种约定:

@Override
public void onCharacteristicChanged(BluetoothGatt Gatt, BluetoothGattCharacteristic characteristic) {
byte[] data = characteristic.getValue();
if(data[1] < SOME_PREDIFINDED_VALUE){
//it's a real-time data update
}else{
//it's a response to some data you sent.
}

}

否则,响应将只是一个特征变化,意味着硬件电压的新值。

关于java - 安卓蓝牙 : Identify the Characteristic Type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39120157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com