Skip to content

Commit

Permalink
添加2个方法,用于管理外设的自动重连功能
Browse files Browse the repository at this point in the history
  • Loading branch information
coolnameismy committed Mar 16, 2016
1 parent 5efa749 commit f55f5f9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
21 changes: 13 additions & 8 deletions Classes/objc/BabyBluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,17 @@ sec秒后停止

#pragma mark - 工具方法

/**
* 单例构造方法
* @return BabyBluetooth共享实例
*/
+ (instancetype)shareBabyBluetooth;

/**
断开连接
*/
- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;


/**
断开所有连接
*/
Expand Down Expand Up @@ -441,31 +446,31 @@ characteristic:(CBCharacteristic *)characteristic
- (void)cancelNotify:(CBPeripheral *)peripheral
characteristic:(CBCharacteristic *)characteristic;


/**
获取当前连接的peripherals
*/
- (NSArray *)findConnectedPeripherals;


/**
获取当前连接的peripheral
*/
- (CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName;


/**
获取当前corebluetooth的centralManager对象
*/
- (CBCentralManager *)centralManager;

/**
* 单例构造方法
* @return BabyBluetooth共享实例
添加断开自动重连的外设
*/
+ (instancetype)shareBabyBluetooth;

- (void)AutoReconnect:(CBPeripheral *)peripheral;

/**
删除断开自动重连的外设
*/
- (void)AutoReconnectDelete:(CBPeripheral *)peripheral;

#pragma mark - peripheral model

//进入外设模式
Expand Down
14 changes: 14 additions & 0 deletions Classes/objc/BabyBluetooth.m
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,20 @@ - (CBCentralManager *)centralManager {
return babyCentralManager->centralManager;
}

/**
添加断开自动重连的外设
*/
- (void)AutoReconnect:(CBPeripheral *)peripheral{
[babyCentralManager sometimes_ever:peripheral];
}

/**
删除断开自动重连的外设
*/
- (void)AutoReconnectDelete:(CBPeripheral *)peripheral{
[babyCentralManager sometimes_never:peripheral];
}


#pragma mark - peripheral model

Expand Down
13 changes: 13 additions & 0 deletions Classes/objc/BabyCentralManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
NSMutableArray *connectedPeripherals;
//已经连接的设备
NSMutableArray *discoverPeripherals;
//需要自动重连的外设
NSMutableArray *reConnectPeripherals;
}


Expand All @@ -73,6 +75,17 @@
//获取当前连接的peripheral
- (CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName;

/**
sometimes ever,sometimes never. 相聚有时,后会无期
this is center with peripheral's story
**/

//sometimes ever:添加断开重连接的设备
- (void)sometimes_ever:(CBPeripheral *)peripheral ;
//sometimes never:删除需要重连接的设备
- (void)sometimes_never:(CBPeripheral *)peripheral ;

@end


Expand Down
24 changes: 22 additions & 2 deletions Classes/objc/BabyCentralManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ - (instancetype)init {
pocket = [[NSMutableDictionary alloc]init];
connectedPeripherals = [[NSMutableArray alloc]init];
discoverPeripherals = [[NSMutableArray alloc]init];
reConnectPeripherals = [[NSMutableArray alloc]init];

//监听通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(scanForPeripheralNotifyReceived:) name:@"scanForPeripherals" object:nil];
Expand Down Expand Up @@ -231,14 +232,19 @@ - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPe
[currChannel blockOnDisconnect](central,peripheral,error);
}

//判断是否全部链接都已经段开
//判断是否全部链接都已经段开,调用blockOnCancelAllPeripheralsConnection委托
if ([self findConnectedPeripherals].count == 0) {
//停止扫描callback
if ([currChannel blockOnCancelAllPeripheralsConnection]) {
[currChannel blockOnCancelAllPeripheralsConnection](centralManager);
}
// NSLog(@">>> stopConnectAllPerihperals");
}

//检查并重新连接需要重连的设备
if ([reConnectPeripherals containsObject:peripheral]) {
[self connectToPeripheral:peripheral];
}
}

//扫描到服务
Expand Down Expand Up @@ -428,8 +434,22 @@ - (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray *)inval
}
}

/**
sometimes ever,sometimes never. 相聚有时,后会无期
this is center with peripheral's story
**/


//sometimes ever:添加断开重连接的设备
- (void)sometimes_ever:(CBPeripheral *)peripheral {
if (![reConnectPeripherals containsObject:peripheral]) {
[reConnectPeripherals addObject:peripheral];
}
}
//sometimes never:删除需要重连接的设备
- (void)sometimes_never:(CBPeripheral *)peripheral {
[reConnectPeripherals removeObject:peripheral];
}

#pragma mark - 私有方法

Expand Down

0 comments on commit f55f5f9

Please sign in to comment.