RNPermissionHandlerBluetoothPeripheral.m 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 @[
  11. @"NSBluetoothPeripheralUsageDescription",
  12. @"NSBluetoothAlwaysUsageDescription",
  13. ];
  14. }
  15. + (NSString * _Nonnull)handlerUniqueId {
  16. return @"ios.permission.BLUETOOTH_PERIPHERAL";
  17. }
  18. - (void)checkWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  19. rejecter:(void (__unused ^ _Nonnull)(NSError * _Nonnull))reject {
  20. #if TARGET_OS_SIMULATOR
  21. return resolve(RNPermissionStatusNotAvailable);
  22. #else
  23. if (![RNPermissions isBackgroundModeEnabled:@"bluetooth-peripheral"]) {
  24. return resolve(RNPermissionStatusNotAvailable);
  25. }
  26. if (@available(iOS 13.0, *)) {
  27. switch ([[CBManager new] authorization]) {
  28. case CBManagerAuthorizationNotDetermined:
  29. return resolve(RNPermissionStatusNotDetermined);
  30. case CBManagerAuthorizationRestricted:
  31. return resolve(RNPermissionStatusRestricted);
  32. case CBManagerAuthorizationDenied:
  33. return resolve(RNPermissionStatusDenied);
  34. case CBManagerAuthorizationAllowedAlways:
  35. return resolve(RNPermissionStatusAuthorized);
  36. }
  37. } else {
  38. switch ([CBPeripheralManager authorizationStatus]) {
  39. case CBPeripheralManagerAuthorizationStatusNotDetermined:
  40. return resolve(RNPermissionStatusNotDetermined);
  41. case CBPeripheralManagerAuthorizationStatusRestricted:
  42. return resolve(RNPermissionStatusRestricted);
  43. case CBPeripheralManagerAuthorizationStatusDenied:
  44. return resolve(RNPermissionStatusDenied);
  45. case CBPeripheralManagerAuthorizationStatusAuthorized:
  46. return resolve(RNPermissionStatusAuthorized);
  47. }
  48. }
  49. #endif
  50. }
  51. - (void)requestWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  52. rejecter:(void (^ _Nonnull)(NSError * _Nonnull))reject {
  53. if (![RNPermissions isBackgroundModeEnabled:@"bluetooth-peripheral"]) {
  54. return resolve(RNPermissionStatusNotAvailable);
  55. }
  56. _resolve = resolve;
  57. _reject = reject;
  58. _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:@{
  59. CBPeripheralManagerOptionShowPowerAlertKey: @false,
  60. }];
  61. [_peripheralManager startAdvertising:@{}];
  62. }
  63. - (void)peripheralManagerDidUpdateState:(nonnull CBPeripheralManager *)peripheral {
  64. [_peripheralManager stopAdvertising];
  65. _peripheralManager = nil;
  66. switch (peripheral.state) {
  67. case CBManagerStatePoweredOff:
  68. case CBManagerStateResetting:
  69. case CBManagerStateUnsupported:
  70. return _resolve(RNPermissionStatusNotAvailable);
  71. case CBManagerStateUnknown:
  72. return _resolve(RNPermissionStatusNotDetermined);
  73. case CBManagerStateUnauthorized:
  74. return _resolve(RNPermissionStatusDenied);
  75. case CBManagerStatePoweredOn:
  76. return [self checkWithResolver:_resolve rejecter:_reject];
  77. }
  78. }
  79. @end