RNPermissionHandlerBluetoothPeripheral.m 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 *> * _Nullable)usageDescriptionKeys {
  10. return [RNPermissionsManager hasBackgroundModeEnabled:@"bluetooth-peripheral"] ? @[@"NSBluetoothPeripheralUsageDescription"] : nil;
  11. }
  12. - (void)checkWithResolver:(void (^)(RNPermissionStatus status))resolve
  13. withRejecter:(void (__unused ^)(NSError *error))reject {
  14. switch ([CBPeripheralManager authorizationStatus]) {
  15. case CBPeripheralManagerAuthorizationStatusNotDetermined:
  16. return resolve(RNPermissionStatusNotDetermined);
  17. case CBPeripheralManagerAuthorizationStatusRestricted:
  18. return resolve(RNPermissionStatusRestricted);
  19. case CBPeripheralManagerAuthorizationStatusDenied:
  20. return resolve(RNPermissionStatusDenied);
  21. case CBPeripheralManagerAuthorizationStatusAuthorized:
  22. return resolve(RNPermissionStatusAuthorized);
  23. }
  24. }
  25. - (void)requestWithOptions:(__unused NSDictionary * _Nullable)options
  26. withResolver:(void (^)(RNPermissionStatus status))resolve
  27. withRejecter:(void (^)(NSError *error))reject {
  28. _resolve = resolve;
  29. _reject = reject;
  30. _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:@{
  31. CBPeripheralManagerOptionShowPowerAlertKey: @false,
  32. }];
  33. [_peripheralManager startAdvertising:@{}];
  34. }
  35. - (void)peripheralManagerDidUpdateState:(nonnull CBPeripheralManager *)peripheral {
  36. CBManagerState state = peripheral.state;
  37. [_peripheralManager stopAdvertising];
  38. _peripheralManager = nil;
  39. switch (state) {
  40. case CBManagerStatePoweredOn:
  41. return [self checkWithResolver:_resolve withRejecter:_reject];
  42. case CBManagerStatePoweredOff:
  43. case CBManagerStateResetting:
  44. case CBManagerStateUnsupported:
  45. return _resolve(RNPermissionStatusNotAvailable);
  46. case CBManagerStateUnknown:
  47. return _resolve(RNPermissionStatusNotDetermined);
  48. case CBManagerStateUnauthorized:
  49. return _resolve(RNPermissionStatusDenied);
  50. }
  51. }
  52. @end