为了账号安全,请及时绑定邮箱和手机立即绑定

蓝牙管理器组件侦听器函数中的问题处理异步函数

蓝牙管理器组件侦听器函数中的问题处理异步函数

MMTTMM 2023-04-14 15:36:01
我在蓝牙 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')) {                    }                }            )        } 我不明白为什么即使我使侦听器函数异步并等待侦听器中的 2 个异步函数调用,日志Connected to Device也不会打印。await bleManager.connectToDevice(device.id)这意味着侦听器永远不会在异步函数调用之后执行
查看完整描述

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")

                    }

                }

            )


查看完整回答
反对 回复 2023-04-14
  • 1 回答
  • 0 关注
  • 116 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信