我正在开发一个连接到 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/