-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTAHble.m
executable file
·1740 lines (1201 loc) · 46.4 KB
/
TAHble.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// TAHble.m
// Created by Dhiraj Jadhao on 9/06/2014.
// Copyright (c) 2014 www.tah.io
// All rights reserved.
#import "TAHble.h"
@implementation TAHble
@synthesize delegate;
@synthesize peripherals;
@synthesize manager;
@synthesize activePeripheral;
/*!
* @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 TAHble
*
*/
-(void) setup
{
manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
/*
* -(int) findTAHPeripherals:(int)timeout
*
*/
-(int) findTAHPeripherals:(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 findTAHPeripherals 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);
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];
}
}
-(void) stopScan
{
[manager stopScan];
}
/*
* disconnect
* disconnect to a given peripheral
*
*/
-(void) disconnect:(CBPeripheral *)peripheral
{
[manager cancelPeripheralConnection:peripheral];
}
#pragma mark - basic operations for TAHble service
-(void) write:(CBPeripheral *)peripheral data:(NSData *)data
{
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
-(void) read:(CBPeripheral *)peripheral
{
printf("begin reading\n");
//[peripheral readValueForCharacteristic:dataRecvrCharacteristic];
printf("now reading......\n");
}
-(void) notify: (CBPeripheral *)peripheral on:(BOOL)on
{
[self notification:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral on:YES];
}
#pragma mark - CBCentralManager Delegates
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
//TODO: to handle the state updates
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
printf("New Device Found\n");
if (!peripherals)
{
peripherals = [[NSMutableArray alloc] initWithObjects:peripheral, nil];
for (int i = 0; i < [peripherals count]; i++) {
[delegate peripheralFound: peripheral];
}
}
else
{
if((__bridge CFUUIDRef )peripheral.identifier == NULL) return;
//if(peripheral.name == NULL) return;
//if(peripheral.name == nil) return;
if(peripheral.name.length < 1) return;
// Add the new peripheral to the peripherals array
for (int i = 0; i < [peripherals count]; i++) {
CBPeripheral *p = [peripherals objectAtIndex:i];
if((__bridge CFUUIDRef )p.identifier == NULL) continue;
CFUUIDBytes b1 = CFUUIDGetUUIDBytes((__bridge CFUUIDRef )p.identifier);
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("Known Device Found...\n");
//[delegate peripheralFound: peripheral];
return;
}
}
printf("New Device Found\n");
[peripherals addObject:peripheral];
[delegate peripheralFound:peripheral];
return;
}
}
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
activePeripheral = peripheral;
activePeripheral.delegate = self;
[activePeripheral discoverServices:nil];
//[self notify:peripheral on:YES];
[self printPeripheralInfo:peripheral];
printf("Connected to active peripheral Device\n");
}
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
printf("Disconnected from the active peripheral Device\n");
if(activePeripheral != nil)
[delegate setDisconnect];
activePeripheral = nil;
}
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"Failed to connect active peripheral %@: %@\n", [peripheral name], [error localizedDescription]);
}
#pragma mark - CBPeripheral delegates
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
printf("Updating Value For Characteristic function\n");
if (error) {
printf("Updating Value For Characteristic Failed\n");
return;
}
[delegate TAHbleCharValueUpdated:@"FFE1" value:characteristic.value];
}
//////////////////////////////////////////////////////////////////////////////////////////////
- (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
*
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
if (!error) {
printf("Services of peripheral with UUID : %s found\r\n",[self UUIDToString:(__bridge CFUUIDRef )peripheral.identifier]);
[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
*
*/
- (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++) { //Show every one
CBCharacteristic *c = [service.characteristics objectAtIndex:i];
printf("Found characteristic %s\r\n",[ self CBUUIDToString:c.UUID]);
}
char t[16];
t[0] = (SERVICE_UUID >> 8) & 0xFF;
t[1] = SERVICE_UUID & 0xFF;
NSData *data = [[NSData alloc] initWithBytes:t length:16];
CBUUID *uuid = [CBUUID UUIDWithData:data];
//CBService *s = [peripheral.services objectAtIndex:(peripheral.services.count - 1)];
if([self compareCBUUID:service.UUID UUID2:uuid]) {
printf("Try to open notify\n");
[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:(__bridge CFUUIDRef )peripheral.identifier]);
[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:(__bridge CFUUIDRef )peripheral.identifier]);
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);
return CFStringGetCStringPtr(s, 0);
}
/*
* @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 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 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:(__bridge CFUUIDRef )p.identifier]);
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:(__bridge CFUUIDRef )p.identifier]);
return;
}
[p setNotifyValue:on forCharacteristic:characteristic];
}
/*!
* @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:(__bridge CFUUIDRef )p.identifier]);
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:(__bridge CFUUIDRef )p.identifier]);
return;
}
if(characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse)
{
[p writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
}else
{
[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("Reading 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:(__bridge CFUUIDRef )p.identifier]);
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:(__bridge CFUUIDRef )p.identifier]);
return;
}
[p readValueForCharacteristic:characteristic];
}
//////////////////////////////////// TAH Pin Controls /////////////////////////////////
////////////////// TAH Pins Digital Value control ///////////////////
-(void) TAHdigitalWrite:(CBPeripheral *)peripheral PinNumber:(int)Pin Value:(int)Value
{
NSString *str1 = @"0,";
NSString *str2 = [NSString stringWithFormat:@"%d",Pin];
NSString *str3 = [NSString stringWithFormat:@"%@%d%@",@",",Value,@"R"];
NSString *dataString = [NSString stringWithFormat:@"%@%@%@",str1,str2,str3];
NSData *data = [dataString dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////// TAH Pins Analog Value control ////////////////////////////
-(void) TAHanalogWrite:(CBPeripheral *)peripheral PinNumber:(int)Pin Value:(int)Value
{
NSString *str1 = @"1,";
NSString *str2 = [NSString stringWithFormat:@"%d",Pin];
NSString *str3 = [NSString stringWithFormat:@"%@%d%@",@",",Value,@"R"];
NSString *dataString = [NSString stringWithFormat:@"%@%@%@",str1,str2,str3];
NSData *data = [dataString dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
////////////////////// TAH PWM Servo control pins //////////////////////
-(void) TAHservoWrite:(CBPeripheral *)peripheral PinNumber:(int)Pin Angle:(int)Angle
{
NSString *str1 = @"2,";
NSString *str2 = [NSString stringWithFormat:@"%d",Pin];
NSString *str3 = [NSString stringWithFormat:@"%@%d%@",@",",Angle,@"R"];
NSString *dataString = [NSString stringWithFormat:@"%@%@%@",str1,str2,str3];
NSData *data = [dataString dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
//////////////////////////////////////////////////////////////////////////////////////
/////////// TAH Keyboard and Mouse Control //////////
-(void) TAHkeyPress:(CBPeripheral *)peripheral Press:(int)key
{
NSString *string1 = @"0,0,0,";
NSString *end = @"M";
NSString *command = [NSString stringWithFormat:@"%@%d%@",string1,key,end];
NSData *data = [command dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
-(void) TAHMouseMove:(CBPeripheral *)peripheral Xaxis:(float)Xaxis Yaxis:(float)Yaxis Scroll:(float)Scroll
{
NSString *command,*mouseX,*mouseY,*scroll,*keypress,*end,*seperator;
mouseX = [NSString stringWithFormat:@"%.0f",Xaxis];
mouseY = [NSString stringWithFormat:@"%.0f",Yaxis];
scroll = [NSString stringWithFormat:@"%.0f",Scroll];
keypress = @"0";
seperator = @",";
end = @"M";
command = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@",mouseX,seperator,mouseY,seperator,scroll,seperator,keypress,end];
NSData *data = [command dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
-(void) TAHTrackPad:(CBPeripheral *)peripheral Swipe:(int)Swipe
{
if (Swipe == Up)
{
NSData *data = [@"0,0,0,256M" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
else if (Swipe == Down)
{
NSData *data = [@"0,0,0,257M" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
else if (Swipe == Right)
{
NSData *data = [@"0,0,0,258M" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
else if (Swipe == Left)
{
NSData *data = [@"0,0,0,259M" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
}
//////////////////////////////////////////////////////
////////////////// TAH AT Command Set //////////////////
// Parameters which are altered takes effect only after Reboot
// Resets TAH
-(void)updateSettings:(CBPeripheral *)peripheral
{
NSData *data = [@"AT+RESET" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
// Get TAH Mac Address
-(void)getTAHMacAddress:(CBPeripheral *)peripheral
{
NSData *data = [@"AT+ADDR?" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
// Get TAH Advertising Interval
-(void)getTAHadvertisinginterval:(CBPeripheral *)peripheral
{
NSData *data = [@"AT+ADVI?" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
// Get TAH Battery Level
-(void)getTAHbatterylevel:(CBPeripheral *)peripheral
{
NSData *data = [@"AT+BATT?" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
// Get TAH Baud Rate
-(void)getTAHBaudRate:(CBPeripheral *)peripheral
{
NSData *data = [@"AT+BAUD?" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
// Get TAH Characteristics Value
-(void)getTAHcharacteristicsValue:(CBPeripheral *)peripheral
{
NSData *data = [@"AT+CHAR?" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
// Get TAH Beacon Mode
-(void)getTAHBeaconMode:(CBPeripheral *)peripheral
{
NSData *data = [@"AT+IBEA?" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
// Get TAH Beacon UUID 0
-(void)getTAHBeaconUUID0:(CBPeripheral *)peripheral
{
NSData *data = [@"AT+IBE0?" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
// Get TAH Beacon UUID 1
-(void)getTAHBeaconUUID1:(CBPeripheral *)peripheral
{
NSData *data = [@"AT+IBE1?" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
// Get TAH Beacon UUID 2
-(void)getTAHBeaconUUID2:(CBPeripheral *)peripheral
{
NSData *data = [@"AT+IBE2?" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
// Get TAH Beacon UUID 3
-(void)getTAHBeaconUUID3:(CBPeripheral *)peripheral
{
NSData *data = [@"AT+IBE3?" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
-(void)setTAHBeaconUUID0:(CBPeripheral *)peripheral UUID0:(char)UUID0
{
if (UUID0 >=7)
{
NSString *str1 = @"AT+IBE0";
NSString *str2 = [NSString stringWithFormat:@"%hhd", UUID0];
NSString *str3 = [NSString stringWithFormat:@"%@%@", str1,str2];
NSData *data = [str3 dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
}
-(void)setTAHBeaconUUID1:(CBPeripheral *)peripheral UUID1:(char)UUID1
{
if (UUID1 >=7)
{
NSString *str1 = @"AT+IBE1";
NSString *str2 = [NSString stringWithFormat:@"%hhd", UUID1];
NSString *str3 = [NSString stringWithFormat:@"%@%@", str1,str2];
NSData *data = [str3 dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
}
-(void)setTAHBeaconUUID2:(CBPeripheral *)peripheral UUID2:(char)UUID2
{
if (UUID2 >=7)
{
NSString *str1 = @"AT+IBE2";
NSString *str2 = [NSString stringWithFormat:@"%hhd", UUID2];
NSString *str3 = [NSString stringWithFormat:@"%@%@", str1,str2];
NSData *data = [str3 dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
}
-(void)setTAHBeaconUUID3:(CBPeripheral *)peripheral UUID3:(char)UUID3
{
if (UUID3 >=7)
{
NSString *str1 = @"AT+IBE3";
NSString *str2 = [NSString stringWithFormat:@"%hhd", UUID3];
NSString *str3 = [NSString stringWithFormat:@"%@%@", str1,str2];
NSData *data = [str3 dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
}
// Get TAH Beacon Major Value
-(void)getTAHBeaconMajor:(CBPeripheral *)peripheral
{
NSData *data = [@"AT+MARJ?" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
// Get TAH Beacon Minor Value
-(void)getTAHBeaconMinor:(CBPeripheral *)peripheral
{
NSData *data = [@"AT+MINO?" dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
-(void)setTAHBeaconMajor:(CBPeripheral *)peripheral Major:(char)Major
{
if (Major >=3)
{
NSString *str1 = @"AT+MARJ0x";
NSString *str2 = [NSString stringWithFormat:@"%hhd", Major];
NSString *str3 = [NSString stringWithFormat:@"%@%@", str1,str2];
NSData *data = [str3 dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
}
-(void)setTAHBeaconMinor:(CBPeripheral *)peripheral Minor:(char)Minor
{
if (Minor >=3)
{
NSString *str1 = @"AT+MINO0x";
NSString *str2 = [NSString stringWithFormat:@"%hhd", Minor];
NSString *str3 = [NSString stringWithFormat:@"%@%@", str1,str2];
NSData *data = [str3 dataUsingEncoding:[NSString defaultCStringEncoding]];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:peripheral data:data];
}
}
// Get TAH Working Mode