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

我在蓝牙 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)这意味着侦听器永远不会在异步函数调用之后执行


MMTTMM
浏览 117回答 1
1回答

守着星空守着你

解决方法是在 connectToDevice() 和 cancelDeviceConnection() 异步函数周围添加尝试捕获,因为它们被拒绝并且侦听器将返回(因此从未打印“连接到设备”日志的原因)。bleManager.startDeviceScan(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ['00001200-0000-1000-8000-00805f9b34fb'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { allowDuplicates: true },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; async (error, device) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get services&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let services = device.serviceUUIDs&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // check if there are services being advertised&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (services && services.includes('00001200-0000-1000-8000-00805f9b34fb')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("Scanned a device with name: " + device.name + " | " + device.rssi)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("Services:", services)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await bleManager.connectToDevice(device.id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("Could not connect")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("Connected to device: ", device.name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // run some more async code once i'm connected to the device&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await bleManager.cancelDeviceConnection(device.id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("Could not disconnect")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // await bleManager.connectToDevice(device.id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //console.log("Connected to device")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //await bleManager.cancelDeviceConnection(device.id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //console.log("Disconnected from device")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript