如何知道BLE是否表示已在Android中得到确认

问题描述 投票:2回答:1

我们正在开发两个Android应用之间的蓝牙低功耗通信。一个是外围/服务器,一个是中央/客户端。如果数据已更改,服务器将向客户端发送指示。但是,我们没有找到确保数据在客户端实际确认的方法。如果客户收到并确认数据以便在服务器端做出相应的反应,我们如何判断?

根据Android文档,BleutoothGattServer有针对NoificationSent的回调。 https://developer.android.com/reference/android/bluetooth/BluetoothGattServerCallback#onNotificationSent(android.bluetooth.BluetoothDevice,%20int)

但是,通过调试和进行一些测试,似乎只有在发送通知时才会调用此方法。无法保证实际收到或确认消息。

这就是我们如何设置GattServer的特性

BluetoothGattService service = new BluetoothGattService(SERVICE_LOGIN_UUID,
                BluetoothGattService.SERVICE_TYPE_PRIMARY);

        // Write characteristic
        BluetoothGattCharacteristic writeCharacteristic = new BluetoothGattCharacteristic(CHARACTERISTIC_LOGIN_UUID,
                BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_READ| BluetoothGattCharacteristic.PROPERTY_INDICATE,
                // Somehow this is not necessary, the client can still enable notifications
//                        | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
                BluetoothGattCharacteristic.PERMISSION_WRITE | BluetoothGattCharacteristic.PERMISSION_READ);

        service.addCharacteristic(writeCharacteristic);

        mGattServer.addService(service);

然后我们通过调用此通知客户端

mHandler.post(() -> {
            BluetoothGattService service = mGattServer.getService(SERVICE_LOGIN_UUID);
            BluetoothGattCharacteristic characteristic = service.getCharacteristic(uuid);
            log("Notifying characteristic " + characteristic.getUuid().toString()
                    + ", new value: " + StringUtils.byteArrayInHexFormat(value));

            characteristic.setValue(value);
            boolean confirm = BluetoothUtils.requiresConfirmation(characteristic);
            for(BluetoothDevice device : mDevices) {
                mGattServer.notifyCharacteristicChanged(device, characteristic, confirm);
            }
        });

*请不要忘记这里的循环

这将导致onNotificationSent被调用,但无法知道它是否被确认。

如果您需要其他代码部分,请告诉我。

谢谢大家,很多问候

android bluetooth bluetooth-lowenergy gatt bluetooth-gatt
1个回答
2
投票

不幸的是,在Android上,确认是在幕后发送/接收的,即没有机制可以在您的应用中使用它。请看看以下链接: -

Handling indications instead of notifications in Android BLE

如果您真的想要一些应用程序层机制来证明收到了数据,那么您可以在服务器端实现另一个(附加)特性。当在中心侧接收数据时,它可以简单地写入该特征以证明它具有。这样做的缺点是使您的应用程序更复杂,但可以说更可靠。这样做的原因是您手动从应用层发送确认,确保一切都按预期工作,而自动从基带层发送ACK并不能证明一切都是100%成功(例如数据没有'从基带到应用程序,或您的应用程序已崩溃,等)。

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.