gpt4 book ai didi

Android BLE 读取 Gatt 特性

转载 作者:行者123 更新时间:2023-11-29 19:32:18 25 4
gpt4 key购买 nike

我正在尝试读取我的应用程序中的一些蓝牙特性。现在我对从我的 Gatt 服务器更改特性后该怎么做有疑问。起初我尝试使用线程一次又一次地重新触发特征的读取,如下所示:

new Thread(new Runnable() {
@Override
public void run() {
int[] newData = new int[30];
while(true){
try{
for(int i=0;i<newData.length;i++){
newData[i] = 0;
}
BluetoothGatt tmpGatt = refExtDataClass.getRefBluetoothGatt();
tmpGatt.readCharacteristic(characteristic);

byte[] value = characteristic.getValue();

for(int i=0;i<newData.length;i++){
newData[i] = value[i];
}
refExtDataClass.setNmData(newData);
}catch(Exception e){
break;
}
}
}
}).start();

但问题是数据似乎在某一时刻损坏了(就像我总是从我的 MCU 端将相同的数据写入特性)。

可以这样读BLE数据吗?有什么建议的方法可以一直读取BLE数据吗?还是在我的App端更新?

如果您需要任何其他代码,请告诉我。

最佳答案

读取 GATT 特性是一个异步操作。在您收到 onCharacteristicRead 之前,结果不可用回调。

无论如何,您应该将 GATT 服务器配置为发送 notifications当它有新数据要发送而不是一直轮询时。

关于Android BLE 读取 Gatt 特性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39741741/

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