forked from coolnameismy/BabyBluetooth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBabysister.m
352 lines (292 loc) · 11.9 KB
/
Babysister.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
/*
BabyBluetooth
简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮
https://github.com/coolnameismy/BabyBluetooth
*/
// Created by 刘彦玮 on 15/7/30.
// Copyright (c) 2015年 刘彦玮. All rights reserved.
//
/*
后续更新:
1:补全所有委托转block
2: 每个事件增加对NSNotificationCenter 的支持
*/
#import "Babysister.h"
#import "BabyCallback.h"
@implementation Babysister
#define currChannel [babySpeaker callbackOnCurrChannel]
-(instancetype)init{
self = [super init];
if(self){
//pocket
pocket = [[NSMutableDictionary alloc]init];
connectedPeripherals = [[NSMutableArray alloc]init];
bleManager = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()];
//监听通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(scanForPeripheralNotifyReceived:) name:@"scanForPeripherals" object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didDiscoverPeripheralNotifyReceived:) name:@"didDiscoverPeripheral" object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(connectToPeripheralNotifyReceived:) name:@"connectToPeripheral" object:nil];
}
return self;
}
#pragma mark -接收到通知
//开始扫描
-(void)scanForPeripheralNotifyReceived:(NSNotification *)notify{
// NSLog(@">>>scanForPeripheralsNotifyReceived");
}
//扫描到设备
-(void)didDiscoverPeripheralNotifyReceived:(NSNotification *)notify{
// CBPeripheral *peripheral =[notify.userInfo objectForKey:@"peripheral"];
// NSLog(@">>>didDiscoverPeripheralNotifyReceived:%@",peripheral.name);
}
//开始连接设备
-(void)connectToPeripheralNotifyReceived:(NSNotification *)notify{
// NSLog(@">>>connectToPeripheralNotifyReceived");
}
//扫描Peripherals
-(void)scanPeripherals{
[bleManager scanForPeripheralsWithServices:nil options:nil];
}
//连接Peripherals
-(void)connectToPeripheral:(CBPeripheral *)peripheral{
[bleManager connectPeripheral:peripheral options:nil];
//
}
//断开所以已连接的设备
-(void)stopConnectAllPerihperals{
for (int i=0;i<connectedPeripherals.count;i++) {
[bleManager cancelPeripheralConnection:connectedPeripherals[i]];
}
connectedPeripherals = [[NSMutableArray alloc]init];
// NSLog(@">>> stopConnectAllPerihperals");
}
//停止扫描
-(void)stopScan{
[bleManager stopScan];
}
#pragma mark -CBCentralManagerDelegate委托方法
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
case CBCentralManagerStateUnknown:
NSLog(@">>>CBCentralManagerStateUnknown");
break;
case CBCentralManagerStateResetting:
NSLog(@">>>CBCentralManagerStateResetting");
break;
case CBCentralManagerStateUnsupported:
NSLog(@">>>CBCentralManagerStateUnsupported");
break;
case CBCentralManagerStateUnauthorized:
NSLog(@">>>CBCentralManagerStateUnauthorized");
break;
case CBCentralManagerStatePoweredOff:
NSLog(@">>>CBCentralManagerStatePoweredOff");
break;
case CBCentralManagerStatePoweredOn:
NSLog(@">>>CBCentralManagerStatePoweredOn");
//发送centralManagerDidUpdateState通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"CBCentralManagerStatePoweredOn" object:nil];
break;
default:
break;
}
//状态改变callback
if([currChannel blockOnCentralManagerDidUpdateState]){
[currChannel blockOnCentralManagerDidUpdateState](central);
}
}
//扫描到Peripherals
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
//日志
//NSLog(@"当扫描到设备:%@",peripheral.name);
//设备添加到q列表
[self addPeripheral:peripheral];
//发出通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"didDiscoverPeripheral"
object:nil
userInfo:@{@"central":central,@"peripheral":peripheral,@"advertisementData":advertisementData,@"RSSI":RSSI}];
//扫描到设备callback
if([currChannel blockOnDiscoverPeripherals]){
if ([currChannel filterOnDiscoverPeripherals](peripheral.name)) {
[[babySpeaker callbackOnCurrChannel] blockOnDiscoverPeripherals](central,peripheral,advertisementData,RSSI);
}
}
//处理连接设备
if(needConnectPeripheral){
if ([currChannel filterOnConnetToPeripherals](peripheral.name)) {
[bleManager connectPeripheral:peripheral options:nil];
//开一个定时器监控连接超时的情况
connectTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(disconnect:) userInfo:peripheral repeats:NO];
}
}
}
//停止扫描
-(void)disconnect:(id)sender{
[bleManager stopScan];
}
//连接到Peripherals-成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
//NSLog(@">>>连接到名称为(%@)的设备-成功",peripheral.name);
[connectTimer invalidate];//停止时钟
[connectedPeripherals addObject:peripheral];
//执行回叫
//扫描到设备callback
if([currChannel blockOnConnectedPeripheral]){
[currChannel blockOnConnectedPeripheral](central,peripheral);
}
//扫描外设的服务
if (needDiscoverServices) {
[peripheral setDelegate:self];
[peripheral discoverServices:nil];
}
}
//连接到Peripherals-失败
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
// NSLog(@">>>连接到名称为(%@)的设备-失败,原因:%@",[peripheral name],[error localizedDescription]);
if ([currChannel blockOnFailToConnect]) {
[currChannel blockOnFailToConnect](central,peripheral,error);
}
}
//Peripherals断开连接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
// NSLog(@">>>外设连接断开连接 %@: %@\n", [peripheral name], [error localizedDescription]);
[connectedPeripherals removeObject:peripheral];
if ([currChannel blockOnDisconnect]) {
[currChannel blockOnDisconnect](central,peripheral,error);
}
}
//扫描到服务
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
// NSLog(@">>>扫描到服务:%@",peripheral.services);
if (error)
{
NSLog(@">>>Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]);
return;
}
//回叫block
if ([currChannel blockOnDiscoverServices]) {
[currChannel blockOnDiscoverServices](peripheral,error);
}
//discover characteristics
if (needDiscoverCharacteristics) {
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:nil forService:service];
}
}
}
//发现服务的Characteristics
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
if (error)
{
NSLog(@"error Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);
return;
}
//回叫block
if ([currChannel blockOnDiscoverCharacteristics]) {
[currChannel blockOnDiscoverCharacteristics](peripheral,service,error);
}
//如果需要更新Characteristic的值
if (needReadValueForCharacteristic) {
for (CBCharacteristic *characteristic in service.characteristics)
{
[peripheral readValueForCharacteristic:characteristic];
}
}
//如果搜索Characteristic的Descriptors
if (needDiscoverDescriptorsForCharacteristic) {
for (CBCharacteristic *characteristic in service.characteristics)
{
[peripheral discoverDescriptorsForCharacteristic:characteristic];
}
}
}
//读取Characteristics的值
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if (error)
{
NSLog(@"error didUpdateValueForCharacteristic %@ with error: %@", characteristic.UUID, [error localizedDescription]);
return;
}
//查找字段订阅
if([babySpeaker notifyCallback:characteristic]){
[babySpeaker notifyCallback:characteristic](peripheral,characteristic,error);
return;
}
//回叫block
if ([currChannel blockOnReadValueForCharacteristic]) {
[currChannel blockOnReadValueForCharacteristic](peripheral,characteristic,error);
}
}
//发现Characteristics的Descriptors
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if (error)
{
NSLog(@"error Discovered DescriptorsForCharacteristic for %@ with error: %@", characteristic.UUID, [error localizedDescription]);
return;
}
//回叫block
if ([currChannel blockOnDiscoverDescriptorsForCharacteristic]) {
[currChannel blockOnDiscoverDescriptorsForCharacteristic](peripheral,characteristic,error);
}
//如果需要更新Characteristic的Descriptors
if (needReadValueForDescriptors) {
for (CBDescriptor *d in characteristic.descriptors)
{
[peripheral readValueForDescriptor:d];
}
}
//执行一次的方法
if (oneReadValueForDescriptors) {
for (CBDescriptor *d in characteristic.descriptors)
{
[peripheral readValueForDescriptor:d];
}
oneReadValueForDescriptors = NO;
}
}
//读取Characteristics的Descriptors的值
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error{
if (error)
{
NSLog(@"error didUpdateValueForDescriptor for %@ with error: %@", descriptor.UUID, [error localizedDescription]);
return;
}
//回叫block
if ([currChannel blockOnReadValueForDescriptors]) {
[currChannel blockOnReadValueForDescriptors](peripheral,descriptor,error);
}
}
//characteristic.isNotifying 状态改变
-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
// NSLog(@">>>didUpdateNotificationStateForCharacteristic");
// NSLog(@">>>uuid:%@,isNotifying:%@",characteristic.UUID,characteristic.isNotifying?@"isNotifying":@"Notifying");
}
-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
// NSLog(@">>>didWriteValueForCharacteristic");
// NSLog(@">>>uuid:%@,new value:%@",characteristic.UUID,characteristic.value);
}
-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error{
// NSLog(@">>>didWriteValueForCharacteristic");
// NSLog(@">>>uuid:%@,new value:%@",descriptor.UUID,descriptor.value);
}
#pragma mark -私有方法
#pragma mark -设备list管理
-(void)addPeripheral:(CBPeripheral *)peripheral{
if(![peripherals objectForKey:peripheral.name] && ![peripheral.name isEqualToString:@""] ){
[peripherals setObject:peripheral forKey:peripheral.name];
}
}
-(void)deletePeripheral:(NSString *)peripheralName{
[peripherals removeObjectForKey:peripheralName];
}
-(CBPeripheral *)findPeripheral:(NSString *)peripheralName{
return [peripherals objectForKey:peripheralName];
}
-(NSMutableDictionary *)findPeripherals{
return peripherals;
}
@end