RNPermissionHandlerBluetoothPeripheral.m 2.9KB

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