RNPermissionHandlerBluetoothPeripheral.m 2.6KB

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