LETransceiver.m
11.9 KB
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
//
// LETransceiver.m
// ActsBluetoothSpeed
//
// Created by inidhu on 2019/3/15.
// Copyright © 2019 Actions. All rights reserved.
//
#import "LETransceiver.h"
#import <UIKit/UIKit.h>
#define BLE_UUID_SERVICE @"e49a25f8-f69a-11e8-8eb2-f2801f1b9fd1"
#define BLE_UUID_SERVICE_WRITE @"e49a25e0-f69a-11e8-8eb2-f2801f1b9fd1"
#define BLE_UUID_SERVICE_READ @"e49a28e1-f69a-11e8-8eb2-f2801f1b9fd1"
#define KeyWriteServiceUUID @"kWriteServiceUUID"
#define KeyIndicateServiceUUID @"kIndicateServiceUUID"
#define KeyWriteCharacteristicUUID @"kWriteCharacteristicUUID"
#define KeyIndicateCharacteristicUUID @"kIndicateCharacteristicUUID"
@implementation LETransceiver {
NSString *writeServiceUUID;
NSString *writeCharacteristicUUID;
NSString *indicateServiceUUID;
NSString *indicateCharacteristicUUID;
// CBCharacteristicWriteType writeType;
CBPeripheral *connectedPeripheral;
CBCharacteristic *characteristicWrite;
CBCharacteristic *characteristicIndicate;
NSTimer *connectTimer;
BOOL isReadyToSendWrite;
}
- (id) init {
self = [super init];
if (self) {
NSDictionary *uuids = [NSDictionary dictionaryWithObjectsAndKeys:BLE_UUID_SERVICE, KeyWriteServiceUUID, BLE_UUID_SERVICE_WRITE, KeyWriteCharacteristicUUID, BLE_UUID_SERVICE, KeyIndicateServiceUUID, BLE_UUID_SERVICE_READ, KeyIndicateCharacteristicUUID, nil];
[self setUUIDs:uuids];
}
// writeType = CBCharacteristicWriteWithoutResponse;
// centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
return self;
}
- (void)setUUIDs:(NSDictionary *) uuids {
NSArray *keys = [uuids allKeys];
for (NSString *key in keys) {
if ([key isEqualToString:KeyWriteServiceUUID]) {
writeServiceUUID = [[uuids objectForKey:KeyWriteServiceUUID] uppercaseString];
} else if ([key isEqualToString:KeyIndicateServiceUUID]) {
indicateServiceUUID = [[uuids objectForKey:KeyIndicateServiceUUID] uppercaseString];
} else if ([key isEqualToString:KeyWriteCharacteristicUUID]) {
writeCharacteristicUUID = [[uuids objectForKey:KeyWriteCharacteristicUUID] uppercaseString];
} else if ([key isEqualToString:KeyIndicateCharacteristicUUID]) {
indicateCharacteristicUUID = [[uuids objectForKey:KeyIndicateCharacteristicUUID] uppercaseString];
}
}
}
//- (void)scanStart {
// NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
// /*
// NSMutableArray *servicesUUID = [[NSMutableArray alloc] init];
// if (writeServiceUUID)
// [servicesUUID addObject:writeServiceUUID];
// if (indicateServiceUUID)
// [servicesUUID addObject:indicateServiceUUID];
// */
// [centralManager scanForPeripheralsWithServices:nil options:options];
//}
//- (void)scanStop {
// [centralManager stopScan];
//}
//- (void)connect:(CBPeripheral *)peripheral {
// [self scanStop];
//
// [centralManager connectPeripheral:peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey:@YES}];
//
// if (connectTimer) {
// [connectTimer invalidate];
// }
// connectTimer = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(connectTimeout:) userInfo:peripheral repeats:NO];
//}
//- (void)disconnect:(CBPeripheral *)peripheral {
// if (![self isPeripheralConnected:peripheral]) {
// return;
// }
//
// NSLog(@"disconnect %@", peripheral.name);
// [centralManager cancelPeripheralConnection:peripheral];
//}
//- (BOOL) isPeripheralConnected:(CBPeripheral *) peripheral {
// if (IOS_VERSION >= 7.0) {
// if (peripheral.state == CBPeripheralStateConnected) {
// return YES;
// }
// } else {
//#ifndef __IPHONE_7_0
// if (peripheral.isConnected) {
// return YES;
// }
//#endif
// }
// return NO;
//}
- (NSUInteger) getMtuForType:(CBCharacteristicWriteType) type {
return [connectedPeripheral maximumWriteValueLengthForType:type];
}
- (void)write:(NSData *) data {
if (characteristicWrite) {
NSUInteger mtu = [self getMtuForType:CBCharacteristicWriteWithoutResponse];
NSLog(@"writeData mtu: %ld, %@", mtu, data);
for (NSUInteger index = 0; index < data.length; index += mtu) {
NSUInteger len = (data.length - index) > mtu ? mtu : (data.length - index);
NSData *value = [data subdataWithRange:NSMakeRange(index, len)];
[connectedPeripheral writeValue:value forCharacteristic:characteristicWrite type:CBCharacteristicWriteWithoutResponse];
NSLog(@"writeValue: %@", value);
}
}
}
- (void)write:(NSData *) data index:(int) i {
if (characteristicWrite) {
NSUInteger mtu = [self getMtuForType:CBCharacteristicWriteWithoutResponse];
NSLog(@"i writeData mtu: %ld, %@", mtu, data);
for (NSUInteger index = 0; index < data.length; index += mtu) {
NSUInteger len = (data.length - index) > mtu ? mtu : (data.length - index);
NSData *value = [data subdataWithRange:NSMakeRange(index, len)];
if(i != 0 && i % 62 == 0) {
NSData *value2 = [value subdataWithRange:NSMakeRange(0, 1)];
NSData *value3 = [value subdataWithRange:NSMakeRange(1, value.length - 1)];
[connectedPeripheral writeValue:value2 forCharacteristic:characteristicWrite type:CBCharacteristicWriteWithResponse];
[connectedPeripheral writeValue:value3 forCharacteristic:characteristicWrite type:CBCharacteristicWriteWithoutResponse];
} else {
[connectedPeripheral writeValue:value forCharacteristic:characteristicWrite type:CBCharacteristicWriteWithoutResponse];
}
NSLog(@"writeValue: %@", value);
}
}
}
//- (void)connectTimeout:(NSTimer *)timer {
// CBPeripheral *peripheral = (CBPeripheral *)[timer userInfo];
// [self disconnect:peripheral];
//}
#pragma mark -- CBCentralManagerDelegate
//- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
// NSString *messtoshow;
// switch (central.state) {
// case CBManagerStatePoweredOn:{
// messtoshow=[NSString stringWithFormat:@"CBCentralManagerStatePoweredOn"];
// [self scanStart];
// break;
// }
// case CBManagerStateUnknown:
// {
// messtoshow=[NSString stringWithFormat:@"State unknown, update imminent"];
// break;
// }
// case CBManagerStateResetting:
// {
// messtoshow=[NSString stringWithFormat:@"The connection with the system service was momentarily lost, update imminent."];
// break;
// }
// case CBManagerStateUnsupported:
// {
// messtoshow=[NSString stringWithFormat:@"The platform doesn't support Bluetooth Low Energy"];
// break;
// }
// case CBManagerStateUnauthorized:
// {
// messtoshow=[NSString stringWithFormat:@"The app is not authorized to use Bluetooth Low Energy"];
// break;
// }
// case CBManagerStatePoweredOff:
// {
// [self disconnect:connectedPeripheral];
//
// messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered off"];
// break;
// }
// default:
// break;
// }
// NSLog(@"%@",messtoshow);
//}
//- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
//// NSLog(@"didDiscoverPeripheral: %@", peripheral);
//
// if(_connectionDelegate && [_connectionDelegate respondsToSelector:@selector(onFoundPeripheral:advertisementData:)]) {
// [_connectionDelegate onFoundPeripheral:peripheral advertisementData:advertisementData];
// }
//}
//- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
// NSLog(@"didConnectPeripheral: %@", peripheral.name);
//
// peripheral.delegate = self;
// [peripheral discoverServices:nil];
//}
//- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
// NSLog(@"Fail to connect: %@", peripheral.name);
//}
//- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
// NSLog(@"didDisconnectPeripheral %@, error: %@",peripheral, error);
//
// connectedPeripheral = nil;
// characteristicWrite = nil;
// characteristicIndicate = nil;
// if(_connectionDelegate && [_connectionDelegate respondsToSelector:@selector(onDisconnectedPeripheral:)]) {
// [_connectionDelegate onDisconnectedPeripheral:peripheral];
// }
//}
#pragma mark -- CBPeripheralDelegate
//- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
// NSLog(@"didDiscoverServices: %@", peripheral.name);
// if (error) {
// NSLog(@"Error discovering service:%@", [error localizedDescription]);
// return;
// }
//
// for (CBService *service in peripheral.services) {
//
// NSLog(@"service: %@", service);
// [peripheral discoverCharacteristics:nil forService:service];
// }
//
//}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
NSLog(@"didDiscoverCharacteristicsForService: %@, %@", peripheral.name, service.UUID.UUIDString);
if (error) {
NSLog(@"Error didDiscoverCharacteristicsForService:%@",[error localizedDescription]);
return;
}
NSLog(@"UUID: %@, %@, %@", service.UUID.UUIDString, writeServiceUUID, indicateServiceUUID);
if ([service.UUID.UUIDString isEqualToString:writeServiceUUID]) {
for (CBCharacteristic *characteristic in service.characteristics) {
NSLog(@"DiscoverCharacteristics: %@", characteristic.UUID);
if ([characteristic.UUID.UUIDString isEqualToString:writeCharacteristicUUID])
characteristicWrite = characteristic;
}
}
if ([service.UUID.UUIDString isEqualToString:indicateServiceUUID]) {
for (CBCharacteristic *characteristic in service.characteristics) {
NSLog(@"DiscoverCharacteristics: %@", characteristic.UUID);
if ([characteristic.UUID.UUIDString isEqualToString:indicateCharacteristicUUID]) {
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
characteristicIndicate = characteristic;
}
}
}
if (characteristicWrite && characteristicIndicate) {
connectedPeripheral = peripheral;
[connectTimer invalidate];
// if(_connectionDelegate && [_connectionDelegate respondsToSelector:@selector(onConnectedPeripheral:)]) {
// [_connectionDelegate onConnectedPeripheral:peripheral];
// }
}
}
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error {
}
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (error) {
NSLog(@"%@,%@,Error didWriteValueForCharacteristic:%@",characteristic.UUID,[characteristic value], [error localizedDescription]);
return;
}
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (error) {
NSLog(@"Error didUpdateValueForCharacteristic:%@", [error localizedDescription]);
return;
}
NSLog(@"didUpdateValueForCharacteristic: %@ %@", characteristic.UUID, characteristic.value);
if (_dataDelegate && [_dataDelegate respondsToSelector:@selector(onDataReceive:)]) {
[_dataDelegate onDataReceive:[characteristic value]];
}
}
@end