我仍在努力从android (使用java)的BLE设备(激光计)中读取信息,基本上,我正在尝试获取我感兴趣的字段(也称为度量)。
到目前为止,我发现了5个服务(我找到了使用这里):
我遇到的第一个问题是,根据文档,onCharacteristicRead(3)被废弃了,因为它现在添加了一个字节数组作为参数。但据安卓工作室称,这种超载并不存在。
第二个原因是,onCharacteristicRead只被称为一个,尽管它有超过10个可读的特性。
我现在只是在测试一些东西,但我所做的是:
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
for (BluetoothGattService serv: gatt.getServices()) {
System.out.println("-----------------" + serv.getUuid());
for(BluetoothGattCharacteristic chara: serv.getCharacteristics()) {
if((chara.getProperties() & PROPERTY_READ) != 0) {
System.out.println("Can read");
gatt.readCharacteristic(chara);
}
else {
System.out.println("Can't read");
}
}
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
System.out.println("Called");
if(status == GATT_SUCCESS) {
System.out.println(new String(characteristic.getValue()));
} else {
System.out.println("error during read");
}
}这是输出:
I/System.out: -----------------00001800-0000-1000-8000-00805f9b34fb
I/System.out: Can read
I/chatty: uid=10168(com.example.adici) Binder:27154_2 identical 1 line
I/System.out: Can read
I/System.out: -----------------00001801-0000-1000-8000-00805f9b34fb
I/System.out: Can't read
I/System.out: -----------------3ab10100-f831-4395-b29d-570977d5bf94
I/System.out: Can read
I/System.out: Can read
I/System.out: Can't read
I/System.out: Can read
I/System.out: Can read
I/System.out: -----------------0000180f-0000-1000-8000-00805f9b34fb
I/System.out: Can read
I/System.out: Can read
I/System.out: -----------------0000180a-0000-1000-8000-00805f9b34fb
I/System.out: Can read
I/System.out: Called
I/System.out: DISTO 80951028我还尝试添加这个while循环,以等待上次回调发生,然后再启动另一个回调,就像提到的那样
while(waitingForCallback)
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
waitingForCallback = true;
gatt.readCharacteristic(chara);其中,waitingForCallback在onCharacteristicRead中设置为false,但由于该方法未被调用,它在无限循环中结束。
我真的不明白为什么重载对我的IDE来说是一个问题,因为Sdkmin = 27和targetSdk = 31
任何帮助都是非常感谢的!
发布于 2022-05-24 05:51:22
当一个操作已经挂起时,您可能不会启动关贸总协定操作(例如,读取一个特性)。在启动下一次读取之前,必须等待回调。
您的睡眠循环将无法工作,因为蓝牙堆栈从未调用您的回调,而另一个回调已经在运行。在本例中,如果在onServicesDiscovered中运行睡眠循环,则onCharacteristicRead回调将被调度为运行,但在onServicesDiscovered返回之前不会运行。这是Android中的"Binder“机制的一个特性,它防止同一个对象的回调并发运行。
https://stackoverflow.com/questions/72350295
复制相似问题