问题描述
我在蓝牙RN应用程序中运行一些异步代码时遇到问题。
我正在尝试创建一个执行以下操作的侦听器功能:连接到设备(使用异步功能),记录我已连接的日志,然后从设备断开连接(使用异步功能)。
此侦听器功能作为蓝牙低功耗(ble)设备扫描功能的参数提供:
// Relevant Function Prototypes --------------------------------------------------
// startDeviceScan() -> This scans for devices and runs a listener function on each
// device it scans.
bleManager.startDeviceScan(
UUIDs: ?Array<UUID>,options: ?Scanoptions,listener: (error: ?Error,scannedDevice: ?Device) => void // <- this listener function
)
// connectToDevice() -> This connects to a device scanned by the bleManager in the
// listener function given to startDeviceScan()
bleManager.connectToDevice(
deviceidentifier: deviceid,options: ?Connectionoptions,): Promise<Device>
// My code ------------------------------------------------------------------------
// Scans nearby ble devices advertising and runs a listener function that has the
// connection error status and device id as given parameters.
// **This function triggers on a Button onPress
const handleStartScanning = async () => {
try {
bleManager.startDeviceScan(
['00001200-0000-1000-8000-00805f9b34fb'],// the service UUID I am scanning for
{ allowDuplicates: true },// I allow to duplicates to continuously reconnect to devices
async (error,device) => {
// get services
let services = device.serviceUUIDs // get list of the service UUIDs on device
// make sure services not null and out service UUID is included
if (services && services.includes('00001200-0000-1000-8000-00805f9b34fb')) {
// log the scanned device's name,RSSi,and its service UUIDs
console.log("Scanned a device with name: " + device.name + " | " + device.RSSi)
console.log("Services:",services)
await bleManager.connectToDevice(device.id) // <- *****ISSUE HERE*****
console.log("Connected to device") // <- *****THIS NEVER PRINTS*****
// run some more async code here once i'm connected to the device
await bleManager.cancelDeviceConnection(device.id)
}
}
)
} catch (error) {
console.log('Could not start scanning for devices',{ error })
}
}
我不明白为什么即使我使侦听器函数异步并等待侦听器中的2个异步函数调用,也无法打印Connected to Device
日志。这意味着侦听器永远不会在await bleManager.connectToDevice(device.id)
异步函数调用之后执行
这是我的控制台日志:
请注意,我在下面发布的代码段之后反复Possible Unhandled Promise Rejection (id: 150): BleError: Operation was cancelled
键入错误日志。有时,日志可以追溯到某些设备中的扫描,但总会回到承诺被拒绝的情况。 Connected to device
console.log从不打印。
[Thu Oct 01 2020 22:59:42.385] LOG Scanned a device with name: Android | -43
[Thu Oct 01 2020 22:59:42.387] LOG Services: ["00001200-0000-1000-8000-00805f9b34fb"]
[Thu Oct 01 2020 22:59:42.388] LOG Scanned a device with name: Android | -43
[Thu Oct 01 2020 22:59:42.388] LOG Services: ["00001200-0000-1000-8000-00805f9b34fb"]
[Thu Oct 01 2020 22:59:42.487] LOG Scanned a device with name: Android | -44
[Thu Oct 01 2020 22:59:42.491] LOG Services: ["00001200-0000-1000-8000-00805f9b34fb"]
[Thu Oct 01 2020 22:59:42.492] LOG Scanned a device with name: Android | -44
[Thu Oct 01 2020 22:59:42.492] LOG Services: ["00001200-0000-1000-8000-00805f9b34fb"]
[Thu Oct 01 2020 22:59:42.514] WARN Possible Unhandled Promise Rejection (id: 150):
BleError: Operation was cancelled
construct@[native code]
_construct@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:23889:28
Wrapper@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:23844:25
construct@[native code]
_createSuperInternal@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:117317:322
BleError@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:117330:26
parseBleError@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:117368:30
_callPromise$@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:117632:51
tryCatch@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:24712:23
invoke@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:24885:32
tryCatch@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:24712:23
invoke@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:24785:30
http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:24797:21
tryCallOne@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:26784:16
http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:26885:27
_callTimer@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:30324:17
_callImmediatesPass@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:30363:17
callImmediates@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:30580:33
__callImmediates@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:2630:35
http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:2416:34
__guard@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:2613:15
flushedQueue@http://10.0.0.122:8081/index.bundle?platform=ios&dev=true&minify=false:2415:21
flushedQueue@[native code]
invokeCallbackAndReturnFlushedQueue@[native code]
解决方案更新: 非常感谢Jaromanda X提供的解决方案!
解决方法是在尝试拒绝connectToDevice()和cancelDeviceConnection()异步函数时添加尝试捕获,并且侦听器将返回(因此,从不打印“ Connected to Device”日志)。
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")
}
}
)
解决方法
@Jaromanda X提供了解决方案:
解决方法是在尝试拒绝connectToDevice()和cancelDeviceConnection()异步函数时添加尝试捕获,并且侦听器将返回(因此,从不打印“ Connected to Device”日志)。
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")
}
}
)