-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialGATT.m
executable file
·677 lines (583 loc) · 23.2 KB
/
SerialGATT.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//
// SerialGATT.m
// BLKSoft
//
// Created by BLKSofts on 7/13/12.
// Copyright (c) 2012 jnhuamao.cn. All rights reserved.
//
#import "SerialGATT.h"
@implementation SerialGATT
@synthesize delegate;
@synthesize peripherals;
@synthesize manager;
@synthesize activePeripheral;
/*!
* @method notification:
*
* @param serviceUUID Service UUID to read from (e.g. 0x2400)
* @param characteristicUUID Characteristic UUID to read from (e.g. 0x2401)
* @param p CBPeripheral to read from
*
* @discussion Main routine for enabling and disabling notification services. It converts integers
* into CBUUID's used by CoreBluetooth. It then searches through the peripherals services to find a
* suitable service, it then checks that there is a suitable characteristic on this service.
* If this is found, the notfication is set.
*
*/
-(void) notification:(int)serviceUUID characteristicUUID:(int)characteristicUUID p:(CBPeripheral *)p on:(BOOL)on {
UInt16 s = [self swap:serviceUUID];
UInt16 c = [self swap:characteristicUUID];
NSData *sd = [[NSData alloc] initWithBytes:(char *)&s length:2];
NSData *cd = [[NSData alloc] initWithBytes:(char *)&c length:2];
CBUUID *su = [CBUUID UUIDWithData:sd];
CBUUID *cu = [CBUUID UUIDWithData:cd];
CBService *service = [self findServiceFromUUIDEx:su p:p];
if (!service) {
printf("Could not find service with UUID %s on peripheral with UUID %s\r\n",[self CBUUIDToString:su],[self UUIDToString:[self getUUIDFromString:p.identifier.UUIDString]]);
return;
}
CBCharacteristic *characteristic = [self findCharacteristicFromUUIDEx:cu service:service];
if (!characteristic) {
printf("Could not find characteristic with UUID %s on service with UUID %s on peripheral with UUID %s\r\n",[self CBUUIDToString:cu],[self CBUUIDToString:su],[self UUIDToString:[self getUUIDFromString:p.identifier.UUIDString]]);
return;
}
[p setNotifyValue:on forCharacteristic:characteristic];
}
/*!
* @method swap:
*
* @param s Uint16 value to byteswap
*
* @discussion swap byteswaps a UInt16
*
* @return Byteswapped UInt16
*/
-(UInt16) swap:(UInt16)s {
UInt16 temp = s << 8;
temp |= (s >> 8);
return temp;
}
/*
* (void) setup
* enable CoreBluetooth CentralManager and set the delegate for SerialGATT
*
*/
-(void) setup
{
manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
/*
* -(int) findBTSmartPeripherals:(int)timeout
*
*/
-(int) findBLKSoftPeripherals:(int)timeout
{
if ([manager state] != CBCentralManagerStatePoweredOn) {
printf("CoreBluetooth is not correctly initialized !\n");
return -1;
}
[NSTimer scheduledTimerWithTimeInterval:(float)timeout target:self selector:@selector(scanTimer:) userInfo:nil repeats:NO];
//[manager scanForPeripheralsWithServices:[NSArray arrayWithObject:serviceUUID] options:0]; // start Scanning
[manager scanForPeripheralsWithServices:nil options:0];
return 0;
}
/*
* scanTimer
* when findBLKSoftPeripherals is timeout, this function will be called
*
*/
-(void) scanTimer:(NSTimer *)timer
{
[manager stopScan];
}
/*
* @method printPeripheralInfo:
*
* @param peripheral Peripheral to print info of
*
* @discussion printPeripheralInfo prints detailed info about peripheral
*
*/
- (void) printPeripheralInfo:(CBPeripheral*)peripheral {
// CFStringRef s = CFUUIDCreateString(NULL, (__bridge CFUUIDRef)(peripheral.identifier)); //todocw
CFStringRef s = (__bridge CFStringRef)peripheral.identifier.UUIDString;
printf("------------------------------------\r\n");
printf("Peripheral Info :\r\n");
printf("UUID : %s\r\n",CFStringGetCStringPtr(s, 0));
printf("RSSI : %d\r\n",[peripheral.RSSI intValue]);
printf("Name : %s\r\n",[peripheral.name cStringUsingEncoding:NSStringEncodingConversionAllowLossy]);
printf("isConnected : %d\r\n",peripheral.state == CBPeripheralStateConnected);
printf("-------------------------------------\r\n");
}
/*
* connect
* connect to a given peripheral
*
*/
-(void) connect:(CBPeripheral *)peripheral
{
if (peripheral.state != CBPeripheralStateConnected) {
[manager connectPeripheral:peripheral options:nil];
}
}
/*
* disconnect
* disconnect to a given peripheral
*
*/
-(void) disconnect:(CBPeripheral *)peripheral
{
[manager cancelPeripheralConnection:peripheral];
}
#pragma mark - basic operations for SerialGATT service
-(void) write:(CBPeripheral *)peripheral data:(NSData *)data
{
[self writeValue:fileService characteristicUUID:fileSub p:peripheral data:data];
//[self writeValue:mainService characteristicUUID:mainSub3 p:peripheral data:data];
}
-(void) read:(CBPeripheral *)peripheral
{
printf("begin reading\n");
printf("now can reading......\n");
//[peripheral readValueForCharacteristic:dataRecvrCharacteristic];
}
-(void) notify: (CBPeripheral *)peripheral on:(BOOL)on
{
[self notification:fileService characteristicUUID:fileSub p:peripheral on:YES];
//[peripheral setNotifyValue:on forCharacteristic:dataNotifyCharacteristic];
}
#pragma mark - Finding CBServices and CBCharacteristics
-(CBService *) findServiceFromUUID:(CBUUID *)UUID p:(CBPeripheral *)peripheral
{//查找的是主服务,找到主服务后再找char
printf("the services count is %d\n", peripheral.services.count);
for (CBService *s in peripheral.services) {
printf("<%s> is found!\n", [[s.UUID.data description] cStringUsingEncoding:NSStringEncodingConversionAllowLossy]);
// compare s with UUID
if ([[s.UUID data] isEqualToData:[UUID data]]) {
return s;
}
}
return nil;
}
-(CBCharacteristic *) findCharacteristicFromUUID:(CBUUID *)UUID p:(CBPeripheral *)peripheral service:(CBService *)service
{
for (CBCharacteristic *c in service.characteristics) {
printf("characteristic <%s> is found!\n", [[UUID.data description] cStringUsingEncoding:NSStringEncodingConversionAllowLossy]);
if ([[c.UUID data] isEqualToData:[UUID data]]) {
return c;
}
}
return nil;
}
#pragma mark - CBCentralManager Delegates
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
//TODO: to handle the state updates
}
//MARK: 发现外设
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
printf("Now we found device\n");
if (!peripherals) {
peripherals = [[NSMutableArray alloc] initWithObjects:peripheral, nil];
for (int i = 0; i < [peripherals count]; i++) {
[delegate peripheralFound: peripheral];
}
}
if (!self.peripheralDic) {
self.peripheralDic = [[NSMutableDictionary alloc] init];
}
{
if(peripheral.identifier == NULL || peripheral.name == nil) return;
// Add the new peripheral to the peripherals array
for (int i = 0; i < [peripherals count]; i++) {
CBPeripheral *p = [peripherals objectAtIndex:i];
if(p.identifier == NULL) continue;
CFUUIDRef u1 = CFUUIDCreateFromString(NULL, (__bridge CFStringRef)p.identifier.UUIDString);
CFUUIDRef u2 = CFUUIDCreateFromString(NULL, (__bridge CFStringRef)peripheral.identifier.UUIDString);
CFUUIDBytes b1 = CFUUIDGetUUIDBytes(u1); //todocw
CFUUIDBytes b2 = CFUUIDGetUUIDBytes(u2);
// CFUUIDBytes b2 = CFUUIDGetUUIDBytes((__bridge CFUUIDRef)(peripheral.identifier));
if (memcmp(&b1, &b2, 16) == 0) {
// these are the same, and replace the old peripheral information
[peripherals replaceObjectAtIndex:i withObject:peripheral];
printf("Duplicated peripheral is found...\n");
//[delegate peripheralFound: peripheral];
return;
}
}
printf("New peripheral is found...\n");
[peripherals addObject:peripheral];
[delegate peripheralFound:peripheral];
//添加扫描蓝牙Mac地址对, 蓝牙Mac地址:48872D9A0179
// if ([peripheral.name isEqualToString:@"Romoss Bluetooth test"]) { //测试用
NSData *manufacturerData = [advertisementData objectForKey:CBAdvertisementDataManufacturerDataKey];
NSString *result = [self convertDataToHexStr:manufacturerData];
// const char *valueString = [[manufacturerData description] cStringUsingEncoding: NSUTF8StringEncoding];
// NSLog(@"%02x", valueString);
if(result != nil && result.length > 4) {
NSString *addr = [result substringFromIndex:4];
NSString *macAddre = [addr uppercaseString];
NSLog(@"%@", macAddre);
[self.peripheralDic setObject:peripheral forKey:macAddre];
}
// }
return;
}
printf("%s\n", __FUNCTION__);
}
//MARK: data转十六进制
- (NSString *)convertDataToHexStr:(NSData *)data {
if (!data || [data length] == 0) {
return @"";
}
NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
[data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
unsigned char *dataBytes = (unsigned char*)bytes;
for (NSInteger i = 0; i < byteRange.length; i++) {
NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
if ([hexStr length] == 2) {
[string appendString:hexStr];
} else {
[string appendFormat:@"0%@", hexStr];
}
}
}];
return string;
}
//-(CFUUIDRef)getUUIDRef:(CBPeripheral *)peripheral {
//#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0 && __IPHONE_OS_VERSION_MAX_ALLOWED <= __IPHONE_14_0
// return (__bridge CFUUIDRef)peripheral.identifier;
//#else
// return peripheral.UUID;
//#endif
//}
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
activePeripheral = peripheral;
activePeripheral.delegate = self;
[activePeripheral discoverServices:nil];
[self printPeripheralInfo:peripheral];
printf("connected to the active peripheral\n");
}
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
activePeripheral = nil;
printf("disconnected to the active peripheral\n");
}
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"failed to connect to peripheral %@: %@\n", [peripheral name], [error localizedDescription]);
}
#pragma mark - CBPeripheral delegates
//MARK: 从设备上获取到数据会调用此方法
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
printf("in updateValueForCharacteristic function\n");
if (error) {
printf("updateValueForCharacteristic failed\n");
return;
}
NSString *str = [self convertDataToHexStr:characteristic.value];
[delegate serialGATTCharValueUpdated:@"FFE1" value:str];
}
//////////////////////////////////////////////////////////////////////////////////////////////
//MARK:
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
}
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error
{
}
- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(NSError *)error
{
}
/*
* @method getAllCharacteristicsFromKeyfob
*
* @param p Peripheral to scan
*
*
* @discussion getAllCharacteristicsFromKeyfob starts a characteristics discovery on a peripheral
* pointed to by p
*
*/
-(void) getAllCharacteristicsFromKeyfob:(CBPeripheral *)p{
for (int i=0; i < p.services.count; i++) {
CBService *s = [p.services objectAtIndex:i];
printf("Fetching characteristics for service with UUID : %s\r\n",[self CBUUIDToString:s.UUID]);
[p discoverCharacteristics:nil forService:s];
}
}
/*
* @method didDiscoverServices
*
* @param peripheral Pheripheral that got updated
* @error error Error message if something went wrong
*
* @discussion didDiscoverServices is called when CoreBluetooth has discovered services on a
* peripheral after the discoverServices routine has been called on the peripheral
*
*/
//MARK: 找到服务
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
if (!error) {
printf("Services of peripheral with UUID : %s found\r\n",[self UUIDToString:[self getUUIDFromString:peripheral.identifier.UUIDString]]);
[self getAllCharacteristicsFromKeyfob:peripheral];
}
else {
printf("Service discovery was unsuccessfull !\r\n");
}
}
/*
* @method didDiscoverCharacteristicsForService
*
* @param peripheral Pheripheral that got updated
* @param service Service that characteristics where found on
* @error error Error message if something went wrong
*
* @discussion didDiscoverCharacteristicsForService is called when CoreBluetooth has discovered
* characteristics on a service, on a peripheral after the discoverCharacteristics routine has been called on the service
*
*/
//MARK: 找到特征
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
if (!error) {
printf("Characteristics of service with UUID : %s found\r\n",[self CBUUIDToString:service.UUID]);
for(int i=0; i < service.characteristics.count; i++) {
CBCharacteristic *c = [service.characteristics objectAtIndex:i];
printf("Found characteristic %s\r\n",[ self CBUUIDToString:c.UUID]);
CBService *s = [peripheral.services objectAtIndex:(peripheral.services.count - 1)];
if([self compareCBUUID:service.UUID UUID2:s.UUID]) {
printf("Finished discovering characteristics\n");
//char data = 0x01;
//NSData *d = [[NSData alloc] initWithBytes:&data length:1];
//[self writeValue:mainService characteristicUUID:mainSub p:peripheral data:d];
//[self writeValue:fileService characteristicUUID:fileSub p:peripheral data:d];
[self notify:peripheral on:YES];
}
}
}
else {
printf("Characteristic discorvery unsuccessfull !\r\n");
}
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if (!error) {
printf("Updated notification state for characteristic with UUID %s on service with UUID %s on peripheral with UUID %s\r\n",[self CBUUIDToString:characteristic.UUID],[self CBUUIDToString:characteristic.service.UUID],[self UUIDToString:[self getUUIDFromString:peripheral.identifier.UUIDString]]);
[delegate setConnect];
}
else {
printf("Error in setting notification state for characteristic with UUID %s on service with UUID %s on peripheral with UUID %s\r\n",[self CBUUIDToString:characteristic.UUID],[self CBUUIDToString:characteristic.service.UUID],[self UUIDToString:[self getUUIDFromString:peripheral.identifier.UUIDString]]);
printf("Error code was %s\r\n",[[error description] cStringUsingEncoding:NSStringEncodingConversionAllowLossy]);
}
}
/*
* @method CBUUIDToString
*
* @param UUID UUID to convert to string
*
* @returns Pointer to a character buffer containing UUID in string representation
*
* @discussion CBUUIDToString converts the data of a CBUUID class to a character pointer for easy printout using printf()
*
*/
-(const char *) CBUUIDToString:(CBUUID *) UUID {
return [[UUID.data description] cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
}
/*
* @method UUIDToString
*
* @param UUID UUID to convert to string
*
* @returns Pointer to a character buffer containing UUID in string representation
*
* @discussion UUIDToString converts the data of a CFUUIDRef class to a character pointer for easy printout using printf()
*
*/
-(const char *) UUIDToString:(CFUUIDRef)UUID {
if (!UUID) return "NULL";
CFStringRef s = CFUUIDCreateString(NULL, UUID); //todocw
return CFStringGetCStringPtr(s, 0);
}
-(CFUUIDRef) getUUIDFromString:(NSString *)str {
return CFUUIDCreateFromString(NULL, (__bridge CFStringRef)str);
}
/*
* @method compareCBUUID
*
* @param UUID1 UUID 1 to compare
* @param UUID2 UUID 2 to compare
*
* @returns 1 (equal) 0 (not equal)
*
* @discussion compareCBUUID compares two CBUUID's to each other and returns 1 if they are equal and 0 if they are not
*
*/
-(int) compareCBUUID:(CBUUID *) UUID1 UUID2:(CBUUID *)UUID2 {
char b1[16];
char b2[16];
[UUID1.data getBytes:b1];
[UUID2.data getBytes:b2];
if (memcmp(b1, b2, UUID1.data.length) == 0)return 1;
else return 0;
}
/*
* @method compareCBUUIDToInt
*
* @param UUID1 UUID 1 to compare
* @param UUID2 UInt16 UUID 2 to compare
*
* @returns 1 (equal) 0 (not equal)
*
* @discussion compareCBUUIDToInt compares a CBUUID to a UInt16 representation of a UUID and returns 1
* if they are equal and 0 if they are not
*
*/
-(int) compareCBUUIDToInt:(CBUUID *)UUID1 UUID2:(UInt16)UUID2 {
char b1[16];
[UUID1.data getBytes:b1];
UInt16 b2 = [self swap:UUID2];
if (memcmp(b1, (char *)&b2, 2) == 0) return 1;
else return 0;
}
/*
* @method CBUUIDToInt
*
* @param UUID1 UUID 1 to convert
*
* @returns UInt16 representation of the CBUUID
*
* @discussion CBUUIDToInt converts a CBUUID to a Uint16 representation of the UUID
*
*/
-(UInt16) CBUUIDToInt:(CBUUID *) UUID {
char b1[16];
[UUID.data getBytes:b1];
return ((b1[0] << 8) | b1[1]);
}
/*
* @method IntToCBUUID
*
* @param UInt16 representation of a UUID
*
* @return The converted CBUUID
*
* @discussion IntToCBUUID converts a UInt16 UUID to a CBUUID
*
*/
-(CBUUID *) IntToCBUUID:(UInt16)UUID {
char t[16];
t[0] = ((UUID >> 8) & 0xff); t[1] = (UUID & 0xff);
NSData *data = [[NSData alloc] initWithBytes:t length:16];
return [CBUUID UUIDWithData:data];
}
/*
* @method findServiceFromUUID:
*
* @param UUID CBUUID to find in service list
* @param p Peripheral to find service on
*
* @return pointer to CBService if found, nil if not
*
* @discussion findServiceFromUUID searches through the services list of a peripheral to find a
* service with a specific UUID
*
*/
-(CBService *) findServiceFromUUIDEx:(CBUUID *)UUID p:(CBPeripheral *)p {
for(int i = 0; i < p.services.count; i++) {
CBService *s = [p.services objectAtIndex:i];
if ([self compareCBUUID:s.UUID UUID2:UUID]) return s;
}
return nil; //Service not found on this peripheral
}
/*
* @method findCharacteristicFromUUID:
*
* @param UUID CBUUID to find in Characteristic list of service
* @param service Pointer to CBService to search for charateristics on
*
* @return pointer to CBCharacteristic if found, nil if not
*
* @discussion findCharacteristicFromUUID searches through the characteristic list of a given service
* to find a characteristic with a specific UUID
*
*/
-(CBCharacteristic *) findCharacteristicFromUUIDEx:(CBUUID *)UUID service:(CBService*)service {
for(int i=0; i < service.characteristics.count; i++) {
CBCharacteristic *c = [service.characteristics objectAtIndex:i];
if ([self compareCBUUID:c.UUID UUID2:UUID]) return c;
}
return nil; //Characteristic not found on this service
}
/*!
* @method writeValue:
*
* @param serviceUUID Service UUID to write to (e.g. 0x2400)
* @param characteristicUUID Characteristic UUID to write to (e.g. 0x2401)
* @param data Data to write to peripheral
* @param p CBPeripheral to write to
*
* @discussion Main routine for writeValue request, writes without feedback. It converts integer into
* CBUUID's used by CoreBluetooth. It then searches through the peripherals services to find a
* suitable service, it then checks that there is a suitable characteristic on this service.
* If this is found, value is written. If not nothing is done.
*
*/
-(void) writeValue:(int)serviceUUID characteristicUUID:(int)characteristicUUID p:(CBPeripheral *)p data:(NSData *)data {
UInt16 s = [self swap:serviceUUID];
UInt16 c = [self swap:characteristicUUID];
NSData *sd = [[NSData alloc] initWithBytes:(char *)&s length:2];
NSData *cd = [[NSData alloc] initWithBytes:(char *)&c length:2];
CBUUID *su = [CBUUID UUIDWithData:sd];
CBUUID *cu = [CBUUID UUIDWithData:cd];
CBService *service = [self findServiceFromUUIDEx:su p:p];
if (!service) {
printf("Could not find service with UUID %s on peripheral with UUID %s\r\n",[self CBUUIDToString:su],[self UUIDToString:[self getUUIDFromString:p.identifier.UUIDString]]);
return;
}
CBCharacteristic *characteristic = [self findCharacteristicFromUUIDEx:cu service:service];
if (!characteristic) {
printf("Could not find characteristic with UUID %s on service with UUID %s on peripheral with UUID %s\r\n",[self CBUUIDToString:cu],[self CBUUIDToString:su],[self UUIDToString:[self getUUIDFromString:p.identifier.UUIDString]]);
return;
}
// [p writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
[p writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}
/*!
* @method readValue:
*
* @param serviceUUID Service UUID to read from (e.g. 0x2400)
* @param characteristicUUID Characteristic UUID to read from (e.g. 0x2401)
* @param p CBPeripheral to read from
*
* @discussion Main routine for read value request. It converts integers into
* CBUUID's used by CoreBluetooth. It then searches through the peripherals services to find a
* suitable service, it then checks that there is a suitable characteristic on this service.
* If this is found, the read value is started. When value is read the didUpdateValueForCharacteristic
* routine is called.
*
* @see didUpdateValueForCharacteristic
*/
-(void) readValue: (int)serviceUUID characteristicUUID:(int)characteristicUUID p:(CBPeripheral *)p {
printf("In read Value");
UInt16 s = [self swap:serviceUUID];
UInt16 c = [self swap:characteristicUUID];
NSData *sd = [[NSData alloc] initWithBytes:(char *)&s length:2];
NSData *cd = [[NSData alloc] initWithBytes:(char *)&c length:2];
CBUUID *su = [CBUUID UUIDWithData:sd];
CBUUID *cu = [CBUUID UUIDWithData:cd];
CBService *service = [self findServiceFromUUIDEx:su p:p];
if (!service) {
printf("Could not find service with UUID %s on peripheral with UUID %s\r\n",[self CBUUIDToString:su],[self UUIDToString:[self getUUIDFromString:p.identifier.UUIDString]]);
return;
}
CBCharacteristic *characteristic = [self findCharacteristicFromUUIDEx:cu service:service];
if (!characteristic) {
printf("Could not find characteristic with UUID %s on service with UUID %s on peripheral with UUID %s\r\n",[self CBUUIDToString:cu],[self CBUUIDToString:su],[self UUIDToString:[self getUUIDFromString:p.identifier.UUIDString]]);
return;
}
[p readValueForCharacteristic:characteristic];
}
@end