蓝牙管理器组件侦听器函数中的问题处理异步函数
Issue processing async function in a Bluetooth manager component listener function
我遇到问题 运行 蓝牙 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() 异步函数周围添加尝试捕获,因为它们被拒绝并且侦听器会 return(因此 '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() 异步函数周围添加尝试捕获,因为它们被拒绝并且侦听器会 return(因此 '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")
}
}
)
我遇到问题 运行 蓝牙 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() 异步函数周围添加尝试捕获,因为它们被拒绝并且侦听器会 return(因此 '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() 异步函数周围添加尝试捕获,因为它们被拒绝并且侦听器会 return(因此 '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")
}
}
)