第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

藍(lán)牙管理器組件偵聽器函數(shù)中的問題處理異步函數(shù)

藍(lán)牙管理器組件偵聽器函數(shù)中的問題處理異步函數(shù)

MMTTMM 2023-04-14 15:36:01
我在藍(lán)牙 RN 應(yīng)用程序中運(yùn)行一些異步代碼時(shí)遇到問題。我正在嘗試創(chuàng)建一個(gè)執(zhí)行以下操作的偵聽器函數(shù):連接到設(shè)備(使用異步函數(shù)),記錄我已連接,然后斷開與設(shè)備的連接(使用異步函數(shù))。此偵聽器功能作為藍(lán)牙低功耗 (ble) 設(shè)備掃描功能的參數(shù)提供:// 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')) {                    }                }            )        } 我不明白為什么即使我使偵聽器函數(shù)異步并等待偵聽器中的 2 個(gè)異步函數(shù)調(diào)用,日志Connected to Device也不會(huì)打印。await bleManager.connectToDevice(device.id)這意味著偵聽器永遠(yuǎn)不會(huì)在異步函數(shù)調(diào)用之后執(zhí)行
查看完整描述

1 回答

?
守著星空守著你

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊

解決方法是在 connectToDevice() 和 cancelDeviceConnection() 異步函數(shù)周圍添加嘗試捕獲,因?yàn)樗鼈儽痪芙^并且偵聽器將返回(因此從未打印“連接到設(shè)備”日志的原因)。


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

                    }

                }

            )


查看完整回答
反對 回復(fù) 2023-04-14
  • 1 回答
  • 0 關(guān)注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號