RNPermissionHandlerBluetoothPeripheral.m 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #import "RNPermissionHandlerBluetoothPeripheral.h"
  2. @import CoreBluetooth;
  3. @interface RNPermissionHandlerBluetoothPeripheral() <CBPeripheralManagerDelegate>
  4. @property (nonatomic, strong) CBPeripheralManager* peripheralManager;
  5. @property (nonatomic, strong) void (^resolve)(RNPermissionStatus status);
  6. @property (nonatomic, strong) void (^reject)(NSError *error);
  7. @end
  8. @implementation RNPermissionHandlerBluetoothPeripheral
  9. + (NSArray<NSString *> * _Nonnull)usageDescriptionKeys {
  10. return @[@"NSBluetoothPeripheralUsageDescription"];
  11. }
  12. + (NSString * _Nonnull)handlerUniqueId {
  13. return @"ios.permission.BLUETOOTH_PERIPHERAL";
  14. }
  15. - (void)checkWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  16. rejecter:(void (__unused ^ _Nonnull)(NSError * _Nonnull))reject {
  17. #if TARGET_OS_SIMULATOR
  18. return resolve(RNPermissionStatusNotAvailable);
  19. #else
  20. if (![RNPermissions isBackgroundModeEnabled:@"bluetooth-peripheral"]) {
  21. return resolve(RNPermissionStatusNotAvailable);
  22. }
  23. switch ([CBPeripheralManager authorizationStatus]) {
  24. case CBPeripheralManagerAuthorizationStatusNotDetermined:
  25. return resolve(RNPermissionStatusNotDetermined);
  26. case CBPeripheralManagerAuthorizationStatusRestricted:
  27. return resolve(RNPermissionStatusRestricted);
  28. case CBPeripheralManagerAuthorizationStatusDenied:
  29. return resolve(RNPermissionStatusDenied);
  30. case CBPeripheralManagerAuthorizationStatusAuthorized:
  31. return resolve(RNPermissionStatusAuthorized);
  32. }
  33. #endif
  34. }
  35. - (void)requestWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  36. rejecter:(void (^ _Nonnull)(NSError * _Nonnull))reject {
  37. if (![RNPermissions isBackgroundModeEnabled:@"bluetooth-peripheral"]) {
  38. return resolve(RNPermissionStatusNotAvailable);
  39. }
  40. _resolve = resolve;
  41. _reject = reject;
  42. _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:@{
  43. CBPeripheralManagerOptionShowPowerAlertKey: @false,
  44. }];
  45. [_peripheralManager startAdvertising:@{}];
  46. }
  47. - (void)peripheralManagerDidUpdateState:(nonnull CBPeripheralManager *)peripheral {
  48. int state = peripheral.state;
  49. [_peripheralManager stopAdvertising];
  50. _peripheralManager = nil;
  51. switch (state) {
  52. case CBManagerStatePoweredOff:
  53. case CBManagerStateResetting:
  54. case CBManagerStateUnsupported:
  55. return _resolve(RNPermissionStatusNotAvailable);
  56. case CBManagerStateUnknown:
  57. return _resolve(RNPermissionStatusNotDetermined);
  58. case CBManagerStateUnauthorized:
  59. return _resolve(RNPermissionStatusDenied);
  60. case CBManagerStatePoweredOn:
  61. return [self checkWithResolver:_resolve rejecter:_reject];
  62. }
  63. }
  64. @end