亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

藍牙管理器組件偵聽器函數中的問題處理異步函數

藍牙管理器組件偵聽器函數中的問題處理異步函數

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 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號