React Native等待承诺永远不会执行

时间:2020-02-21 10:51:16

标签: javascript react-native asynchronous bluetooth async-await

我正在尝试构建一个本机蓝牙应用程序,但这是我第一次使用本机蓝牙。 我有一个asycn函数,我正在调用女巫正在调用UIApplication.sharedApplication,但是什么也没有发生,只是在等待。 我想念什么?

全功能代码

Info.plist

P.S。对不好的代码感到抱歉,仅研究项目的本机蓝牙。 如果有人对本地本机经典蓝牙有很好的教程,那也会有很大帮助。

编辑

将代码更改为

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyWindowScene : UIWindowScene

@end

NS_ASSUME_NONNULL_END

问题仍然存在。 我认为这只是库中的错误,因为如果调用#import "MyWindowScene.h" @implementation MyWindowScene - (UIResponder *)nextResponder{ return self.delegate; } @end 而不是@implementation SceneDelegate ... - (UIResponder *)nextResponder{ return UIApplication.sharedApplication; } 会起作用

2 个答案:

答案 0 :(得分:0)

要正确利用async / await语法,您的代码应更像这样:

   try{
      console.log("false: ", this.state.discovering)
      const unpairedDevices = await BluetoothSerial.discoverUnpairedDevices();
      this.setState({ unpairedDevices, discovering: false });
      console.log("false: ", this.state.discovering);
    catch(e){
      console.log(e)
    } 

答案 1 :(得分:0)

尝试了在问题的编辑部分中编写的更改后,我尝试使用BluetoothSerial.list()并成功了,但仅显示了已解析的设备,而BluetoothSerial.listUnpaired()BluetoothSerial.discoverUnpairedDevices()不起作用。 这一切都是在我的 Xiaomi Mi Mix 2S 手机上完成的。在使用我朋友的诺基亚手机后,BluetoothSerial.listUnpaired()BluetoothSerial.discoverUnpairedDevices()都按预期工作。