-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathRNBluetoothState.m
61 lines (46 loc) · 1.6 KB
/
RNBluetoothState.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#import <CoreBluetooth/CoreBluetooth.h>
#import "RNBluetoothState.h"
#import "RCTEventDispatcher.h"
@interface RNBluetoothState() <CBCentralManagerDelegate>
@property(strong, nonatomic) CBCentralManager *centralManager;
@end
@implementation RNBluetoothState
RCT_EXPORT_MODULE()
@synthesize bridge = _bridge;
#pragma mark Initialization
- (instancetype)init
{
if (self = [super init]) {
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue() options:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:CBCentralManagerOptionShowPowerAlertKey]];
}
return self;
}
RCT_EXPORT_METHOD(initialize) {
[self centralManagerDidUpdateState:self.centralManager];
}
- (NSString *) centralManagerStateToString: (int)state
{
switch (state) {
case CBCentralManagerStateUnknown:
return @"unknown";
case CBCentralManagerStateResetting:
return @"resetting";
case CBCentralManagerStateUnsupported:
return @"unsupported";
case CBCentralManagerStateUnauthorized:
return @"unauthorized";
case CBCentralManagerStatePoweredOff:
return @"off";
case CBCentralManagerStatePoweredOn:
return @"on";
default:
return @"unknown";
}
return @"unknown";
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString *stateName = [self centralManagerStateToString:central.state];
[self.bridge.eventDispatcher sendDeviceEventWithName:@"centralManagerDidUpdateState" body:stateName];
}
@end