RNPermissionHandlerBluetoothPeripheral.m 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. switch ([CBPeripheralManager authorizationStatus]) {
  27. case CBPeripheralManagerAuthorizationStatusNotDetermined:
  28. return resolve(RNPermissionStatusNotDetermined);
  29. case CBPeripheralManagerAuthorizationStatusRestricted:
  30. return resolve(RNPermissionStatusRestricted);
  31. case CBPeripheralManagerAuthorizationStatusDenied:
  32. return resolve(RNPermissionStatusDenied);
  33. case CBPeripheralManagerAuthorizationStatusAuthorized:
  34. return resolve(RNPermissionStatusAuthorized);
  35. }
  36. #endif
  37. }
  38. - (void)requestWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  39. rejecter:(void (^ _Nonnull)(NSError * _Nonnull))reject {
  40. if (![RNPermissions isBackgroundModeEnabled:@"bluetooth-peripheral"]) {
  41. return resolve(RNPermissionStatusNotAvailable);
  42. }
  43. _resolve = resolve;
  44. _reject = reject;
  45. _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:@{
  46. CBPeripheralManagerOptionShowPowerAlertKey: @false,
  47. }];
  48. [_peripheralManager startAdvertising:@{}];
  49. }
  50. - (void)peripheralManagerDidUpdateState:(nonnull CBPeripheralManager *)peripheral {
  51. int state = peripheral.state;
  52. [_peripheralManager stopAdvertising];
  53. _peripheralManager = nil;
  54. switch (state) {
  55. case CBManagerStatePoweredOff:
  56. case CBManagerStateResetting:
  57. case CBManagerStateUnsupported:
  58. return _resolve(RNPermissionStatusNotAvailable);
  59. case CBManagerStateUnknown:
  60. return _resolve(RNPermissionStatusNotDetermined);
  61. case CBManagerStateUnauthorized:
  62. return _resolve(RNPermissionStatusDenied);
  63. case CBManagerStatePoweredOn:
  64. return [self checkWithResolver:_resolve rejecter:_reject];
  65. }
  66. }
  67. @end