如何修复 Web 蓝牙 API 中的 "DOMException" 错误?

How to fix "DOMException" error in Web Bluetooth API?

我正在 Android 中设置蓝牙 LE GATT 服务器,并希望在 Chrome 中使用网络蓝牙 API 作为 GATT 客户端来检查它是否正常工作。实际上,这是我稍后在 Android 中构建 FIDO Authenticator 的第一步。

首先,我尝试在我的 Android 应用程序中将我的服务的 uuid 设置为 0000fffd-0000-1000-8000-00805f9b34fb。像这样:

// start gatt server

bluetoothGattServer = bluetoothManager.openGattServer(this, callback);
bluetoothGattServer.addService(
new BluetoothGattService(UUID.fromString("0000fffd-0000-1000-8000-00805f9b34fb"),
BluetoothGattService.SERVICE_TYPE_PRIMARY));

// start advertising
// ...

然后,我在Chrome中执行了下面的Javascript代码,检查是否可以建立连接。

navigator.bluetooth.requestDevice({ 
filters: [{ services: ['0000fffd-0000-1000-8000-00805f9b34fb'] }] })
.then(device => { console.log(device); })
.catch(error => { console.log(error); });

但是,我只得到如下异常

DOMException

当我在 Android 代码中将 uuid 更改为另一个如 62893031-5e68-4a71-94e4-01fb81f16818 时,它就起作用了! (我的意思是它能够连接到我的 phone 并且我可以在 Chrome 的控制台中看到我的设备信息)

我不知道到底是什么问题。我以前的uuid有什么问题?我怎样才能调试它以了解根本原因?

感谢您的查看!

你可以试试

console.log(error.messsage);

代替

console.log(error);

你会看到

requestDevice() called with a filter containing a blocklisted UUID. https://webbluetoothcg.github.io/web-bluetooth/#attacks-on-devices

查看安全原因。