forked from jacobrosenthal/react-native-ble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNBLE.m
209 lines (158 loc) · 7.21 KB
/
RNBLE.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#import "RNBLE.h"
#import "RCTEventDispatcher.h"
#import "RCTLog.h"
#import "RCTConvert.h"
#import "RCTCONVERT+CBUUID.h"
@interface RNBLE () <CBCentralManagerDelegate, CBPeripheralDelegate> {
CBCentralManager *centralManager;
dispatch_queue_t eventQueue;
}
@end
@implementation RNBLE
RCT_EXPORT_MODULE()
@synthesize bridge = _bridge;
#pragma mark Initialization
- (instancetype)init
{
if (self = [super init]) {
}
return self;
}
RCT_EXPORT_METHOD(setup)
{
eventQueue = dispatch_queue_create("com.openble.mycentral", DISPATCH_QUEUE_SERIAL);
dispatch_set_target_queue(eventQueue, dispatch_get_main_queue());
centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:eventQueue options:@{}];
}
RCT_EXPORT_METHOD(startScanning:(CBUUIDArray *)uuids allowDuplicates:(BOOL)allowDuplicates)
{
// RCTLogInfo(@"startScanning %@ %d", uuids, allowDuplicates);
NSMutableDictionary *scanOptions = [NSMutableDictionary dictionaryWithObject:@NO
forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
if(allowDuplicates){
[scanOptions setObject:@YES forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
}
// RCTLogInfo(@"startScanning %@ %@", uuids, scanOptions);
[centralManager scanForPeripheralsWithServices:uuids options:scanOptions];
}
RCT_EXPORT_METHOD(stopScanning)
{
// RCTLogInfo(@"stopScanning");
[centralManager stopScan];
}
RCT_EXPORT_METHOD(getState)
{
// RCTLogInfo(@"getState");
[self.bridge.eventDispatcher sendDeviceEventWithName:@"stateChange" body:[self NSStringForCBCentralManagerState:[centralManager state]]];
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
// RCTLogInfo(@"didDiscoverPeripheral");
//, @"state" : [self nameForCBPeripheralState:peripheral.state]
NSDictionary *advertisementDictionary = [self dictionaryForAdvertisementData:advertisementData fromPeripheral:peripheral];
NSDictionary *event = @{
@"kCBMsgArgDeviceUUID": peripheral.identifier.UUIDString,
@"kCBMsgArgAdvertisementData": advertisementDictionary,
@"kCBMsgArgName": @"",
@"kCBMsgArgRssi": RSSI
};
[self.bridge.eventDispatcher sendDeviceEventWithName:@"discover" body:event];
}
-(NSDictionary *)dictionaryForPeripheral:(CBPeripheral*)peripheral
{
return @{ @"identifier" : peripheral.identifier.UUIDString, @"name" : peripheral.name ? peripheral.name : @"", @"state" : [self nameForCBPeripheralState:peripheral.state] };
}
-(NSDictionary *)dictionaryForAdvertisementData:(NSDictionary*)advertisementData fromPeripheral:(CBPeripheral*)peripheral
{
// RCTLogInfo(@"dictionaryForAdvertisementData %@ %@", advertisementData, peripheral);
NSString *localNameString = [advertisementData objectForKey:@"kCBAdvDataLocalName"];
localNameString = localNameString ? localNameString : @"";
NSData *manufacturerData = [advertisementData objectForKey:@"kCBAdvDataManufacturerData"];
NSString *manufacturerDataString = [manufacturerData base64EncodedStringWithOptions:0];
manufacturerDataString = manufacturerDataString ? manufacturerDataString : @"";
NSDictionary *serviceDataDictionary = [advertisementData objectForKey:@"kCBAdvDataServiceData"];
NSMutableDictionary *stringServiceDataDictionary = [NSMutableDictionary new];
for (CBUUID *cbuuid in serviceDataDictionary)
{
NSString *uuidString = cbuuid.UUIDString;
NSData *serviceData = [serviceDataDictionary objectForKey:cbuuid];
NSString *serviceDataString = [serviceData base64EncodedStringWithOptions:0];
// RCTLogInfo(@"stringServiceDataDictionary %@ %@", serviceDataString, uuidString);
[stringServiceDataDictionary setObject:serviceDataString forKey:uuidString];
}
// RCTLogInfo(@"stringServiceDataDictionary %@", stringServiceDataDictionary);
NSMutableArray *serviceUUIDsStringArray = [NSMutableArray new];
for (CBUUID *cbuuid in [advertisementData objectForKey:@"kCBAdvDataServiceUUIDs"])
{
[serviceUUIDsStringArray addObject:cbuuid.UUIDString];
}
NSDictionary *advertisementDataDictionary = @{ @"identifier" : @"",
@"kCBAdvDataIsConnectable" : [advertisementData objectForKey:@"kCBAdvDataIsConnectable"],
@"kCBAdvDataLocalName" : localNameString,
@"kCBAdvDataManufacturerData" : manufacturerDataString,
@"kCBAdvDataServiceData" : stringServiceDataDictionary,
@"kCBAdvDataServiceUUIDs" : serviceUUIDsStringArray,
@"kCBAdvDataTxPowerLevel" : [advertisementData objectForKey:@"kCBAdvDataTxPowerLevel"] ? [advertisementData objectForKey:@"kCBAdvDataTxPowerLevel"] : @""
};
return advertisementDataDictionary;
}
- (void) centralManagerDidUpdateState:(CBCentralManager *)central
{
[self.bridge.eventDispatcher sendDeviceEventWithName:@"stateChange" body:[self NSStringForCBCentralManagerState:[central state]]];
}
- (NSString*) NSStringForCBCentralManagerState:(CBCentralManagerState)state{
NSString *stateString = [NSString new];
switch (state) {
case CBCentralManagerStateResetting:
stateString = @"resetting";
break;
case CBCentralManagerStateUnsupported:
stateString = @"unsupported";
break;
case CBCentralManagerStateUnauthorized:
stateString = @"unauthorized";
break;
case CBCentralManagerStatePoweredOff:
stateString = @"poweredOff";
break;
case CBCentralManagerStatePoweredOn:
stateString = @"poweredOn";
break;
case CBCentralManagerStateUnknown:
default:
stateString = @"unknown";
}
return stateString;
}
-(NSString *)nameForCBPeripheralState:(CBPeripheralState)state{
switch (state) {
case CBPeripheralStateDisconnected:
return @"CBPeripheralStateDisconnected";
case CBPeripheralStateConnecting:
return @"CBPeripheralStateConnecting";
case CBPeripheralStateConnected:
return @"CBPeripheralStateConnected";
}
}
-(NSString *)nameForCBCentralManagerState:(CBCentralManagerState)state{
switch (state) {
case CBCentralManagerStateUnknown:
return @"CBCentralManagerStateUnknown";
case CBCentralManagerStateResetting:
return @"CBCentralManagerStateResetting";
case CBCentralManagerStateUnsupported:
return @"CBCentralManagerStateUnsupported";
case CBCentralManagerStateUnauthorized:
return @"CBCentralManagerStateUnauthorized";
case CBCentralManagerStatePoweredOff:
return @"CBCentralManagerStatePoweredOff";
case CBCentralManagerStatePoweredOn:
return @"CBCentralManagerStatePoweredOn";
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
case CBPeripheralStateDisconnecting:
return @"CBPeripheralStateDisconnecting";
#endif
}
}
@end