在 iOS 上延迟 React Native 的蓝牙权限请求

Delay bluetooth permissions request for React Native on iOS

提问人:jolo 提问时间:11/2/2023 最后编辑:jolo 更新时间:11/7/2023 访问量:44

问:

目前,我的应用程序通过自定义 Objective-C 模块在首次加载时请求蓝牙权限,该模块通过 RCT_EXPORT 公开给 React Native。下面的伪代码工作正常。

  RCT_EXPORT_MODULE();
  CBCentralManager *centralManager;
  CBManagerState bluetoothState = CBManagerStateUnknown;

  // Gets called during first load
  -(instancetype) init {
    centralManager = [[CBCentralManager alloc] initWithDelegate:self queue: dispatch_get_main_queue()];
    return self;
  }

  // Gets called correctly when permissions are set
  - (void)centralManagerDidUpdateState:(nonnull CBCentralManager *)central {
    bluetoothState = central.state;
  }

但是,我希望能够在需要蓝牙功能时请求蓝牙权限。可以在所需的时间调用该方法,但不会调用:initBluetooth()centralManagerDidUpdateState

  RCT_EXPORT_MODULE();
  CBCentralManager *centralManager;
  CBManagerState bluetoothState = CBManagerStateUnknown;

  // Get's called at the desired time, and OS settings are set correctly
  RCT_EXPORT_METHOD(initBluetooth: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    centralManager = [[CBCentralManager alloc] initWithDelegate:self queue: dispatch_get_main_queue()];
  }
  
  // No longer gets called
  - (void)centralManagerDidUpdateState:(nonnull CBCentralManager *)central {
    bluetoothState = central.state;
  }

谁能告诉我为什么通过这个自定义函数初始化蓝牙时没有调用?centralManagerDidUpdateState

Objective-C React-Native 蓝牙

评论


答: 暂无答案