针对上一篇demo状态检索及处理的进一步优化和细节处理,重新添加了部分逻辑:
主要是针对设备应用退出再次打开后的自动重连做了处理:
当连接过设备后,除了内存下记录设备对象外,将设备的标识做了偏好设置缓存,当程序退出再次打开,在其他优先条件排除外,从偏好设置下读取设备标识,再尝试自动连接
示例代码:
+ (void)load {
// 程序一启动就开始获取设备状态 (默认一启动时设备状态为未知,暂时这里手动调用了一下获取设备状态的协议方法)
[[JSBluetoothManager sharedManager] centralManagerDidUpdateState:[JSBluetoothManager sharedManager].centralManager];
[JSBluetoothManager sharedManager].timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(getCentralDeviceState) userInfo:nil repeats:YES];
}
+ (void)getCentralDeviceState
{
JSBluetoothManager *manager = [JSBluetoothManager sharedManager];
BOOL isOn = manager.centralManager.state == CBCentralManagerStatePoweredOn;
manager.deviceBluetoothOn = isOn;
NSLog(@"设备启用状态:%zd,设备连接状态:%zd",manager.deviceBluetoothIsOn,manager.deviceIsConnecting);
// 蓝牙关闭
if (!isOn) {
// 自定义alert提示框已在界面中显示 || 当前应用不再前台 不做任何处理
UIApplicationState appState = [UIApplication sharedApplication].applicationState;
if (manager.alertController || appState != UIApplicationStateActive) {
return;
}
// 提示蓝牙关闭
[manager noticeUserToOpenBluetoothService];
} else {
// 蓝牙开启
BOOL isConnected = manager.deviceIsConnecting;
if (isConnected) {
// 开启并连接状态
NSLog(@"--->当前设备处于连接状态");
} else {
// 开启但断开连接状态
// 如果有绑定设备,自动搜索并连接
if (manager.peripheralConnected) {
[manager.centralManager connectPeripheral:manager.peripheralConnected options:nil];
NSLog(@"---->重新连接到设备");
return;
}
// 如果应用退出重启后,根据偏好设置缓存取出上次连接成功后的外设进行自动重连
NSString *lastPeripheralIdentifierConnected = [[NSUserDefaults standardUserDefaults] objectForKey:kLastPeriphrealIdentifierConnectedKey];
if (lastPeripheralIdentifierConnected.length <= 0 || lastPeripheralIdentifierConnected == nil) {
return;
}
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:lastPeripheralIdentifierConnected];
// 返回一个数组,此项目中我们只缓存一个外设
NSArray <CBPeripheral *>*peripheralConnecteds = [manager.centralManager retrievePeripheralsWithIdentifiers:@[uuid]];
// 可重连设备的标识符存在,但是获取到的外设记录为空,错误处理
if (peripheralConnecteds.count == 0 || peripheralConnecteds == nil ) {
if (manager.delegate && [manager.delegate respondsToSelector:@selector(js_centralTool:connectFailure:)]) {
NSError *error = JS_ERROR(JSCentralErrorConnectAutoConnectFail);
[manager.delegate js_centralTool:manager connectFailure:error];
}
return;
}
CBPeripheral *peripheralConnected = peripheralConnecteds.firstObject;
[manager.centralManager connectPeripheral:peripheralConnected options:nil];
// 再次记录 并 写入偏好设置进行缓存
manager.peripheralConnected = peripheralConnected;
NSLog(@"---->根据偏好设置缓存identifier重新连接到设备");
// 调试代码(应用退出重启后无法通过控制台打印判断)
UILabel *label = [[UILabel alloc] init];
label.text = @"根据偏好设置缓存identifier重新连接到设备";
label.textColor = [UIColor redColor];
label.backgroundColor = [UIColor blackColor];
label.frame = [UIApplication sharedApplication].keyWindow.rootViewController.view.frame;
[[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:label];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication].keyWindow.rootViewController.view bringSubviewToFront:label];
});
}
}
}