react-native-ble-manager 10.1.4 → 11.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,309 +0,0 @@
1
- #import "CBPeripheral+Extensions.h"
2
- #import "NSData+Conversion.h"
3
-
4
- static char ADVERTISING_IDENTIFER;
5
- static char ADVERTISEMENT_RSSI_IDENTIFER;
6
-
7
- @implementation CBPeripheral(com_megster_ble_extension)
8
-
9
- -(NSString *)uuidAsString {
10
- if (self.identifier.UUIDString) {
11
- return [self.identifier.UUIDString lowercaseString];
12
- } else {
13
- return @"";
14
- }
15
- }
16
-
17
-
18
- -(NSDictionary *)asDictionary {
19
- NSString *uuidString = NULL;
20
- if (self.identifier.UUIDString) {
21
- uuidString = [self.identifier.UUIDString lowercaseString];
22
- } else {
23
- uuidString = @"";
24
- }
25
-
26
- NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
27
- [dictionary setObject: uuidString forKey: @"id"];
28
-
29
- if ([self name]) {
30
- [dictionary setObject: [self name] forKey: @"name"];
31
- }
32
-
33
- if ([self advertisementRSSI]) {
34
- [dictionary setObject: [self advertisementRSSI] forKey: @"rssi"];
35
- }
36
-
37
- if ([self advertising]) {
38
- [dictionary setObject: [self advertising] forKey: @"advertising"];
39
- }
40
-
41
- if([[self services] count] > 0) {
42
- [self serviceAndCharacteristicInfo: dictionary];
43
- }
44
-
45
- return dictionary;
46
-
47
- }
48
-
49
- // AdvertisementData is from didDiscoverPeripheral. RFduino advertises a service name in the Mfg Data Field.
50
- -(void)setAdvertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)rssi{
51
-
52
- [self setAdvertising:[self serializableAdvertisementData: advertisementData]];
53
- [self setAdvertisementRSSI: rssi];
54
-
55
- }
56
-
57
- // Translates the Advertisement Data from didDiscoverPeripheral into a structure that can be serialized as JSON
58
- //
59
- // This version keeps the iOS constants for keys, future versions could create more friendly keys
60
- //
61
- // Advertisement Data from a Peripheral could look something like
62
- //
63
- // advertising = {
64
- // kCBAdvDataChannel = 39;
65
- // kCBAdvDataIsConnectable = 1;
66
- // kCBAdvDataLocalName = foo;
67
- // kCBAdvDataManufacturerData = {
68
- // CDVType = ArrayBuffer;
69
- // data = "AABoZWxsbw==";
70
- // };
71
- // kCBAdvDataServiceData = {
72
- // FED8 = {
73
- // CDVType = ArrayBuffer;
74
- // data = "ACAAYWJjBw==";
75
- // };
76
- // };
77
- // kCBAdvDataServiceUUIDs = (
78
- // FED8
79
- // );
80
- // kCBAdvDataTxPowerLevel = 32;
81
- //};
82
- - (NSDictionary *) serializableAdvertisementData: (NSDictionary *) advertisementData {
83
-
84
- NSMutableDictionary *dict = [advertisementData mutableCopy];
85
-
86
- // Rename 'local name' key
87
- NSObject *localName = [dict objectForKey:CBAdvertisementDataLocalNameKey];
88
- if (localName) {
89
- [dict setObject:[dict objectForKey:CBAdvertisementDataLocalNameKey] forKey:@"localName"];
90
- [dict removeObjectForKey:CBAdvertisementDataLocalNameKey];
91
- }
92
-
93
- // Rename 'isConnectable' key
94
- NSObject *isConnectable = [dict objectForKey:CBAdvertisementDataIsConnectable];
95
- if (isConnectable) {
96
- [dict setObject:[dict objectForKey:CBAdvertisementDataIsConnectable] forKey:@"isConnectable"];
97
- [dict removeObjectForKey:CBAdvertisementDataIsConnectable];
98
- }
99
-
100
- // Rename 'power level' key
101
- NSObject *powerLevel = [dict objectForKey:CBAdvertisementDataTxPowerLevelKey];
102
- if (powerLevel) {
103
- [dict setObject:[dict objectForKey:CBAdvertisementDataTxPowerLevelKey] forKey:@"txPowerLevel"];
104
- [dict removeObjectForKey:CBAdvertisementDataTxPowerLevelKey];
105
- }
106
-
107
- // Service Data is a dictionary of CBUUID and NSData
108
- // Convert to String keys with Array Buffer values
109
- NSMutableDictionary *serviceData = [dict objectForKey:CBAdvertisementDataServiceDataKey];
110
- if (serviceData) {
111
- NSLog(@"%@", serviceData);
112
-
113
- for (CBUUID *key in [serviceData allKeys]) {
114
- if ([serviceData objectForKey:key]) {
115
- [serviceData setObject:dataToArrayBuffer([serviceData objectForKey:key]) forKey:[[key UUIDString] lowercaseString]];
116
- [serviceData removeObjectForKey:key];
117
- }
118
- }
119
-
120
- // replace the Service Data object
121
- [dict setObject:[dict objectForKey:CBAdvertisementDataServiceDataKey] forKey:@"serviceData"];
122
- [dict removeObjectForKey:CBAdvertisementDataServiceDataKey];
123
- }
124
-
125
- // Create a new list of Service UUIDs as Strings instead of CBUUIDs
126
- NSMutableArray *serviceUUIDs = [dict objectForKey: CBAdvertisementDataServiceUUIDsKey];
127
- NSMutableArray *serviceUUIDStrings;
128
- if (serviceUUIDs) {
129
- serviceUUIDStrings = [[NSMutableArray alloc] initWithCapacity:serviceUUIDs.count];
130
-
131
- for (CBUUID *uuid in serviceUUIDs) {
132
- [serviceUUIDStrings addObject:[[uuid UUIDString] lowercaseString]];
133
- }
134
-
135
- // replace the UUID list with list of strings
136
- [dict removeObjectForKey:CBAdvertisementDataServiceUUIDsKey];
137
- [dict setObject:serviceUUIDStrings forKey:@"serviceUUIDs"];
138
- }
139
-
140
- // Solicited Services UUIDs is an array of CBUUIDs, convert into Strings
141
- NSMutableArray *solicitiedServiceUUIDs = [dict objectForKey:CBAdvertisementDataSolicitedServiceUUIDsKey];
142
- NSMutableArray *solicitiedServiceUUIDStrings;
143
- if (solicitiedServiceUUIDs) {
144
- // NSLog(@"%@", solicitiedServiceUUIDs);
145
- solicitiedServiceUUIDStrings = [[NSMutableArray alloc] initWithCapacity:solicitiedServiceUUIDs.count];
146
-
147
- for (CBUUID *uuid in solicitiedServiceUUIDs) {
148
- [solicitiedServiceUUIDStrings addObject:[[uuid UUIDString] lowercaseString]];
149
- }
150
-
151
- // replace the UUID list with list of strings
152
- [dict removeObjectForKey:CBAdvertisementDataSolicitedServiceUUIDsKey];
153
- [dict setObject:solicitiedServiceUUIDStrings forKey:@"solicitedServiceUUIDs"];
154
- }
155
-
156
- // Overflow Services UUIDs is an array of CBUUIDs, convert into Strings
157
- NSMutableArray *overflowServiceUUIDs = [dict objectForKey:CBAdvertisementDataOverflowServiceUUIDsKey];
158
- NSMutableArray *overflowServiceUUIDStrings;
159
- if (overflowServiceUUIDs) {
160
- // NSLog(@"%@", overflowServiceUUIDs);
161
- overflowServiceUUIDStrings = [[NSMutableArray alloc] initWithCapacity:overflowServiceUUIDs.count];
162
-
163
- for (CBUUID *uuid in overflowServiceUUIDs) {
164
- [overflowServiceUUIDStrings addObject:[[uuid UUIDString] lowercaseString]];
165
- }
166
-
167
- // replace the UUID list with list of strings
168
- [dict removeObjectForKey:CBAdvertisementDataOverflowServiceUUIDsKey];
169
- [dict setObject:overflowServiceUUIDStrings forKey:@"overflowServiceUUIDs"];
170
- }
171
-
172
- // Convert the manufacturer data
173
- NSData *mfgData = [dict objectForKey:CBAdvertisementDataManufacturerDataKey];
174
- if (mfgData) {
175
- [dict setObject:dataToArrayBuffer([dict objectForKey:CBAdvertisementDataManufacturerDataKey]) forKey:@"manufacturerData"];
176
- [dict removeObjectForKey:CBAdvertisementDataManufacturerDataKey];
177
- }
178
-
179
- return dict;
180
- }
181
-
182
- // Put the service, characteristic, and descriptor data in a format that will serialize through JSON
183
- // sending a list of services and a list of characteristics
184
- - (void) serviceAndCharacteristicInfo: (NSMutableDictionary *) info {
185
-
186
- NSMutableArray *serviceList = [NSMutableArray new];
187
- NSMutableArray *characteristicList = [NSMutableArray new];
188
-
189
- // This can move into the CBPeripherial Extension
190
- for (CBService *service in [self services]) {
191
- NSMutableDictionary *serviceDictionary = [NSMutableDictionary new];
192
- [serviceDictionary setObject:[[[service UUID] UUIDString] lowercaseString] forKey:@"uuid"];
193
- [serviceList addObject:serviceDictionary];
194
- for (CBCharacteristic *characteristic in service.characteristics) {
195
- NSMutableDictionary *characteristicDictionary = [NSMutableDictionary new];
196
- [characteristicDictionary setObject:[[[service UUID] UUIDString] lowercaseString] forKey:@"service"];
197
- [characteristicDictionary setObject:[[[characteristic UUID] UUIDString] lowercaseString] forKey:@"characteristic"];
198
-
199
- if ([characteristic value] && [[characteristic value] length] > 0) {
200
- [characteristicDictionary setObject:dataToArrayBuffer([characteristic value]) forKey:@"value"];
201
- }
202
- if ([characteristic properties]) {
203
- //[characteristicDictionary setObject:[NSNumber numberWithInt:[characteristic properties]] forKey:@"propertiesValue"];
204
- [characteristicDictionary setObject:[self decodeCharacteristicProperties:characteristic] forKey:@"properties"];
205
- }
206
- // permissions only exist on CBMutableCharacteristics
207
- [characteristicDictionary setObject:[NSNumber numberWithBool:[characteristic isNotifying]] forKey:@"isNotifying"];
208
- [characteristicList addObject:characteristicDictionary];
209
-
210
- // descriptors always seem to be nil, probably a bug here
211
- NSMutableArray *descriptorList = [NSMutableArray new];
212
- for (CBDescriptor *descriptor in characteristic.descriptors) {
213
- NSMutableDictionary *descriptorDictionary = [NSMutableDictionary new];
214
- [descriptorDictionary setObject:[[[descriptor UUID] UUIDString] lowercaseString] forKey:@"uuid"];
215
- if ([descriptor value]) { // should always have a value?
216
- [descriptorDictionary setObject:[descriptor value] forKey:@"value"];
217
- }
218
- [descriptorList addObject:descriptorDictionary];
219
- }
220
- if ([descriptorList count] > 0) {
221
- [characteristicDictionary setObject:descriptorList forKey:@"descriptors"];
222
- }
223
-
224
- }
225
- }
226
-
227
- [info setObject:serviceList forKey:@"services"];
228
- [info setObject:characteristicList forKey:@"characteristics"];
229
-
230
- }
231
-
232
- -(NSDictionary *) decodeCharacteristicProperties: (CBCharacteristic *) characteristic {
233
- NSMutableDictionary *props = [NSMutableDictionary new];
234
-
235
- CBCharacteristicProperties p = [characteristic properties];
236
-
237
- // NOTE: props strings need to be consistent across iOS and Android
238
- if ((p & CBCharacteristicPropertyBroadcast) != 0x0) {
239
- [props setValue:@"Broadcast" forKey:@"Broadcast"];
240
- }
241
-
242
- if ((p & CBCharacteristicPropertyRead) != 0x0) {
243
- [props setValue:@"Read" forKey:@"Read"];
244
- }
245
-
246
- if ((p & CBCharacteristicPropertyWriteWithoutResponse) != 0x0) {
247
- [props setValue:@"WriteWithoutResponse" forKey:@"WriteWithoutResponse"];
248
- }
249
-
250
- if ((p & CBCharacteristicPropertyWrite) != 0x0) {
251
- [props setValue:@"Write" forKey:@"Write"];
252
- }
253
-
254
- if ((p & CBCharacteristicPropertyNotify) != 0x0) {
255
- [props setValue:@"Notify" forKey:@"Notify"];
256
- }
257
-
258
- if ((p & CBCharacteristicPropertyIndicate) != 0x0) {
259
- [props setValue:@"Indicate" forKey:@"Indicate"];
260
- }
261
-
262
- if ((p & CBCharacteristicPropertyAuthenticatedSignedWrites) != 0x0) {
263
- [props setValue:@"AuthenticateSignedWrites" forKey:@"AuthenticateSignedWrites"];
264
- }
265
-
266
- if ((p & CBCharacteristicPropertyExtendedProperties) != 0x0) {
267
- [props setValue:@"ExtendedProperties" forKey:@"ExtendedProperties"];
268
- }
269
-
270
- if ((p & CBCharacteristicPropertyNotifyEncryptionRequired) != 0x0) {
271
- [props setValue:@"NotifyEncryptionRequired" forKey:@"NotifyEncryptionRequired"];
272
- }
273
-
274
- if ((p & CBCharacteristicPropertyIndicateEncryptionRequired) != 0x0) {
275
- [props setValue:@"IndicateEncryptionRequired" forKey:@"IndicateEncryptionRequired"];
276
- }
277
-
278
- return props;
279
- }
280
-
281
- // Borrowed from Cordova messageFromArrayBuffer since Cordova doesn't handle NSData in NSDictionary
282
- id dataToArrayBuffer(NSData* data)
283
- {
284
- return @{
285
- @"CDVType" : @"ArrayBuffer",
286
- @"data" :[data base64EncodedStringWithOptions:0],
287
- @"bytes" :[data toArray]
288
- };
289
- }
290
-
291
-
292
- -(void)setAdvertising:(NSDictionary *)newAdvertisingValue{
293
- objc_setAssociatedObject(self, &ADVERTISING_IDENTIFER, newAdvertisingValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
294
- }
295
-
296
- -(NSString*)advertising{
297
- return objc_getAssociatedObject(self, &ADVERTISING_IDENTIFER);
298
- }
299
-
300
-
301
- -(void)setAdvertisementRSSI:(NSNumber *)newAdvertisementRSSIValue {
302
- objc_setAssociatedObject(self, &ADVERTISEMENT_RSSI_IDENTIFER, newAdvertisementRSSIValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
303
- }
304
-
305
- -(NSString*)advertisementRSSI{
306
- return objc_getAssociatedObject(self, &ADVERTISEMENT_RSSI_IDENTIFER);
307
- }
308
-
309
- @end
@@ -1,11 +0,0 @@
1
- #import <Foundation/Foundation.h>
2
-
3
- @interface NSData (NSData_Conversion)
4
-
5
- #pragma mark - String Conversion
6
- - (NSString *)hexadecimalString;
7
-
8
- #pragma mark - NSArray Conversion
9
- - (NSArray *)toArray;
10
-
11
- @end
@@ -1,41 +0,0 @@
1
- #import "NSData+Conversion.h"
2
-
3
- @implementation NSData (NSData_Conversion)
4
-
5
- #pragma mark - String Conversion
6
- - (NSString *)hexadecimalString {
7
- /* Returns hexadecimal string of NSData. Empty string if data is empty. */
8
-
9
- const unsigned char *dataBuffer = (const unsigned char *)[self bytes];
10
-
11
- if (!dataBuffer)
12
- return [NSString string];
13
-
14
- NSUInteger dataLength = [self length];
15
- NSMutableString *hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
16
-
17
- for (int i = 0; i < dataLength; ++i)
18
- [hexString appendString:[NSString stringWithFormat:@"%02x", (unsigned int)dataBuffer[i]]];
19
-
20
- return [NSString stringWithString:hexString];
21
- }
22
-
23
- #pragma mark - String Conversion
24
- - (NSArray *)toArray {
25
- /* Returns hexadecimal string of NSData. Empty string if data is empty. */
26
-
27
- const unsigned char *dataBuffer = (const unsigned char *)[self bytes];
28
-
29
- if (!dataBuffer)
30
- return [NSMutableArray new];
31
-
32
- NSUInteger dataLength = [self length];
33
- NSMutableArray *array = [NSMutableArray arrayWithCapacity:dataLength];
34
-
35
- for (int i = 0; i < dataLength; ++i)
36
- [array addObject:[NSNumber numberWithInteger:dataBuffer[i]]];
37
-
38
- return array;
39
- }
40
-
41
- @end