gpt4 book ai didi

多次成功连接后,Android GATT 服务返回 Null

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:08:51 26 4
gpt4 key购买 nike

我正在开发一个连接到 BLE 设备的 Android 应用程序,设备在大多数时间确实连接并且我能够捕获读数。但在多次连接断开后,蓝牙开/关,我的 BluetoothGattCallback 类方法

onServicesDiscovered(BluetoothGatt gatt, int status)

状态为 0 [这意味着 GATT_SUCCESS]。

现在当我尝试获取 BluetoothGattService 时:

BluetoothGattService service = gatt.getService(getServiceUUID());

它返回 null,因此我无法执行后续步骤。请帮我找出问题所在。

最佳答案

您必须首先发现给定设备的所有服务,否则当您运行 BluetoothGattService service = gatt.getService(getServiceUUID()); 时,它将返回 null。

我建议您添加 onServicesDiscovered 函数并改用 gatt.discoverServices();

@Override
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
for (BluetoothGattService gattService : gattServices) {
Log.i(TAG, "Service UUID Found: " + gattService.getUuid().toString());
}
}
}

关于多次成功连接后,Android GATT 服务返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23411573/

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