我尝试 了用于 Web 蓝牙的Google/chrome 示例。看起来 device.gatt.connect() 只能在 navigator.bluetooth.requestDevice() 的承诺范围内工作。我按照示例保存设备并稍后调用 device.gatt.connect() ,它永远等待并且没有返回。我正在使用 Mac OSX 10.13.3 和 chrome64 以及 Android BLE 外设模拟器(来自 Google playstore)
代码段...
var bluetoothDevice;
navigator.bluetooth.requestDevice({
acceptAllDevices: true})
.then(device => {
bluetoothDevice = device;
bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);
connect();
....
});
....
});
function connect() {
exponentialBackoff(3 /* max retries */, 10 /* seconds delay */,
function toTry() {
time('Connecting to Bluetooth Device... ');
/***** Issue here *****
bluetoothDevice.gatt.connect() called by neither return nor catch error
***********************/
return bluetoothDevice.gatt.connect().catch(error => {
log('Argh! ' + error);
});
},
function success() {
log('> Bluetooth Device connected. Try disconnect it now.');
},
function fail() {
time('Failed to reconnect.');
});
}
任何人都可以解释如何重新连接绑定设备?