本来以为对手机系统蓝牙已连接的设备的搜索、连接、通信是使用ExternalAccessory框架的。后来和硬件部的沟通,才发现ExternalAccessory框架是对Lightning接口的硬件或者蓝牙(2.1)的设备才管用。我们使用的是ble协议的蓝牙,所以不能使用ExternalAccessory框架。只能继续找办法。后来发现CBCentralManager类就能实现。下面是CBCentralManager对系统蓝牙已连接外设的搜索、连接的一个简单的Demo代码。(连接成功自然就能发送指令了,通信前要不要发送内部协商的握手指令就看自己了)
#import "ViewController.h"
#import
#import "XJConstants.h"
@interface ViewController ()
/** 系统蓝牙管理类 */
@property (nonatomic, strong) CBCentralManager *bleCentral;
/** 找到的指定的设备 */
@property (nonatomic, strong) CBPeripheral *findPeripheral;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 创建蓝牙操作线程
dispatch_queue_t queue = dispatch_queue_create("xj.ble.thread", DISPATCH_QUEUE_CONCURRENT);
// 创建系统蓝牙管理类
_bleCentral = [[CBCentralManager alloc] initWithDelegate:self queue:queue];
}
#pragma mark - CBCentralManagerDelegate
/** 蓝牙连接状态改变 */
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
XJBleLog(@"蓝牙状态改变 %ld",central.state);
// 如果蓝牙不是开启状态
if(central.state != CBManagerStatePoweredOn){ return; }
// 自定义的服务ID。注意这里是服务ID,不是设备的[peripheral.identifier UUIDString]这个值
CBUUID* serviceId = [CBUUID UUIDWithString:@"自定义的服务ID"];
// 根据自定义的服务ID找出手机系统已连接的设备
NSArray* peripheralArray = [_bleCentral retrieveConnectedPeripheralsWithServices:@[serviceId]];
// 如果没有发现已经连接的设备
if(peripheralArray.count == 0){
XJBleLog(@"没有发现已连接的设备");
// -------------------- 扫描设备 -------------------
// 设置 不重复扫描已发现设备
NSDictionary *option = @{ CBCentralManagerScanOptionAllowDuplicatesKey : [NSNumber numberWithBool:NO] };
// 开始扫描
[_bleCentral scanForPeripheralsWithServices:nil options:option];
}else{
// 遍历系统已连接的设备
for (CBPeripheral* peripheral in peripheralArray) {
NSString* deviceId = [peripheral.identifier UUIDString];
// 这里必须保存这个引用才可以连接设备成功,不然会连接失败
self.findPeripheral = peripheral;
XJBleLog(@"存在设备 - peripheral.name: %@ 连接状态:%ld 设备ID: %@ ",peripheral.name, peripheral.state, deviceId);
[_bleCentral connectPeripheral:peripheral options:nil];
}
}
}
/**
* 发现外设
* advertisementData 是广播的值,一般携带设备名,serviceUUIDs等信息
* RSSI绝对值越大,表示信号越差,设备离的越远。如果想装换成百分比强度,(RSSI+100)/100
*/
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
// 设备id
NSString* deviceId = [peripheral.identifier UUIDString];
// 设备名称
NSString* deviceName = peripheral.name;
// 根据设备名找到指定设备
if([deviceName isEqualToString:@"自定义的设备名"]){
XJBleLog(@"发现设备 - peripheral.name: %@ RSSI: %@ 设备ID: %@",peripheral.name, RSSI, deviceId);
// 这里必须保存这个引用才可以连接设备成功,不然会连接失败
self.findPeripheral = peripheral;
[_bleCentral connectPeripheral:peripheral options:nil];
}
}
/** 连接外设成功 */
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
// 设备id
NSString* deviceId = [peripheral.identifier UUIDString];
NSString* deviceName = peripheral.name;
XJBleLog(@"连接设备成功 - peripheral.name: %@ 连接状态:%ld 设备ID: %@ ",deviceName, peripheral.state, deviceId);
// 注意这里是服务ID,不是设备的[peripheral.identifier UUIDString]这个值
CBUUID* serviceId = [CBUUID UUIDWithString:@"自定义的服务ID"];
NSArray* peripheralArray = [_bleCentral retrieveConnectedPeripheralsWithServices:@[serviceId]];
for (CBPeripheral* peripheral in peripheralArray) {
XJBleLog(@"------已连接的设备 - peripheral.name: %@ 连接状态:%ld 设备ID: %@ ",peripheral.name, peripheral.state, deviceId);
}
}
/** 连接外设失败 */
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{}
/** 已经断开连接 */
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{}
#pragma mark - CBPeripheralDelegate
/** 发现服务 */
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{}
/** 发现特征 */
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{}
/** 订阅的特征值的value发生改变的回调 */
// read 外设 --> 中心 (由控制类去解析返回的数据)
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{}
/**
* 当写入某个特征值后 外设代理执行的回调
* 发送类型必须是CBCharacteristicWriteWithResponse才会回调
*/
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{}
/**
* 读取信号强度回调的方法
* peripheral 调用 readRSSI方法触发该代理
*/
-(void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error{}
@end