1 回答
TA贡献1799条经验 获得超8个赞
解决方法是在 connectToDevice() 和 cancelDeviceConnection() 异步函数周围添加尝试捕获,因为它们被拒绝并且侦听器将返回(因此从未打印“连接到设备”日志的原因)。
bleManager.startDeviceScan(
['00001200-0000-1000-8000-00805f9b34fb'],
{ allowDuplicates: true },
async (error, device) => {
// get services
let services = device.serviceUUIDs
// check if there are services being advertised
if (services && services.includes('00001200-0000-1000-8000-00805f9b34fb')) {
console.log("Scanned a device with name: " + device.name + " | " + device.rssi)
console.log("Services:", services)
try {
await bleManager.connectToDevice(device.id)
} catch {
console.log("Could not connect")
}
console.log("Connected to device: ", device.name)
// run some more async code once i'm connected to the device
try {
await bleManager.cancelDeviceConnection(device.id)
} catch {
console.log("Could not disconnect")
}
// await bleManager.connectToDevice(device.id)
//console.log("Connected to device")
//await bleManager.cancelDeviceConnection(device.id)
//console.log("Disconnected from device")
}
}
)
添加回答
举报