Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Bluetooth manager组件侦听器函数中的问题处理异步函数_Javascript_React Native_Promise_Bluetooth Lowenergy_React Native Ble Plx - Fatal编程技术网

Javascript Bluetooth manager组件侦听器函数中的问题处理异步函数

Javascript Bluetooth manager组件侦听器函数中的问题处理异步函数,javascript,react-native,promise,bluetooth-lowenergy,react-native-ble-plx,Javascript,React Native,Promise,Bluetooth Lowenergy,React Native Ble Plx,我在蓝牙RN应用程序中运行一些异步代码时遇到问题 我正在尝试创建一个侦听器函数,该函数执行以下操作:连接到设备(使用异步函数),记录我已连接的设备,然后断开与设备的连接(使用异步函数) 此侦听器功能作为蓝牙低能(ble)设备扫描功能的参数提供: // Relevant Function Prototypes -------------------------------------------------- // startDeviceScan() -> This scans for d

我在蓝牙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 })
        }
    }
解决方案更新: 非常感谢Jaromanda X提供的解决方案

修复方法是在connectToDevice()和cancelDeviceConnection()异步函数被拒绝且侦听器将返回时,在其周围添加try捕获(因此,为什么从未打印“连接到设备”日志)


@Jaromanda X提供了解决方案:

修复方法是在connectToDevice()和cancelDeviceConnection()异步函数被拒绝且侦听器将返回时,在其周围添加try捕获(因此,为什么从未打印“连接到设备”日志)


如果console.log从未发生过,那么
bleManager.connectToDevice(device.id)
将永远不会解决(它可能会拒绝,您不会在那里处理错误)-可能会在
async(error,device)=>{
callback中添加try/catch。您完全正确,我在这两种方法上都添加了try-catch,现在就可以了!非常感谢您!
[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]
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")
                    }
                }
            )
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")
                    }
                }
            )