Central模式
中心模式(Central Model),可以简单理解为设备App作为中心,连接其他蓝牙外设设备。区别于外设模式,官方介绍可自行Google。这里主要介绍iOS上蓝牙设备作为中心模式的集成。
功能集成
作为蓝牙使用者,程序需要请求蓝牙设备授权,在info.plist配置文件中,添加代码:
项目Targets对应的 BuildPhases 中,引入 CoreBluetooth.framework ,使用的代码中引入头文件
请求蓝牙服务
使用CBCentralManager来进行蓝牙服务管理(授权请求),监听蓝牙状态变化。
设备(外设)搜索
在设备蓝牙开启的情况下,通过 CBCentralManager 的 scanForPeripheralsWithServices:options: 方法来搜索蓝牙外设,搜索成功后通过CBCentralManagerDelegate协议回调将外设返回。
设备连接
通过CBCentralManager的 connectPeripheral:options: 方法进行外设的连接,连接成功后通过CBCentralManagerDelegate协议回调将外设返回。
通常情况下,我们在外设连接成功的回调中,设置外设回调相关协议代理CBPeripheralDelegate,以便后续的服务发现等操作。
服务发现
在蓝牙外设连接成功的情况下,我们通过外设的 discoverServices: 方法进行外设服务的发现,发现成功后,通过外设协议CBPeripheralDelegate回调外设提供的服务信息。
//外设服务发现
[self.peripheral discoverServices:nil];
//外设服务回调
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error{
// NSLog(@"%s, line = %d", __FUNCTION__, __LINE__);
for (CBService *s in peripheral.services) {
NSLog(@" -- 发现服务 -- :%@", s);
[self.connectedPeripheral discoverCharacteristics:nil forService:s];
}
}
通常情况下,我们会对发现的服务进行过滤,查找有用的服务,并查阅其特征值。以上代码读取的所有服务的特征值。
特征值发现
外设服务发现的前提下,通过外设的方法 discoverCharacteristics:forService: 来进行服务特征值的发现。通过外设协议CBPeripheralDelegate回调服务携带的特征值信息。
//特征值发现
[self.peripheral discoverCharacteristics:nil forService:s];
//特征值发现回调
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error{
NSLog(@"%s, line = %d", __FUNCTION__, __LINE__);
for (CBCharacteristic *characteristics in service.characteristics) {
//NSLog(@"%s, line = %d, char = %@", __FUNCTION__, __LINE__, cha);
NSLog(@"characteristic name:%@ value is:%@",characteristics.UUID,characteristics.value);
[self.peripheral readValueForCharacteristic:characteristics];//读取特征值
[self.peripheral discoverDescriptorsForCharacteristic:characteristics];//发现特征值所携带的描述信息
}
}
发现外设特征值后,我们便可以读取特征值及发现特征值所携带的描述信息了
读取特征值
发现特征值后,通过外设的方法 readValueForCharacteristic: 读取特征值。通过外设协议CBPeripheralDelegate回调服务携带的特征值信息。
//特征值读取
[self.peripheral readValueForCharacteristic:characteristics];
//特征值更新回调
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
NSLog(@"%s, line = %d", __FUNCTION__, __LINE__);
//特征值更新
}
特征值携带的UUID、value信息可以直接读取,若特征值发生更新,则通过协议回调进行读取。
读取描述
发现特征值后,通过外设的方法 *discoverDescriptorsForCharacteristic:* 读取特征值的描述信息。通过外设协议CBPeripheralDelegate回调服务携带的特征值信息。
当特征值发生变更的时候,若想要监听,则需要调用蓝牙外设的方法 *- (void)setNotifyValue:(BOOL)enabled forCharacteristic:(CBCharacteristic *)characteristic*
//特征值描述字段读取
[self.peripheral discoverDescriptorsForCharacteristic:characteristics];
//特征值变更通知
[self.peripheral setNotifyValue:YES forCharacteristic:characteristics];//接受通知
//特征值描述更新回调
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error{
NSLog(@"===characteristic name:%@",characteristic.service.UUID);
for (CBDescriptor *d in characteristic.descriptors) {
NSLog(@"CBDescriptor name is :%@",d.UUID);
[self.peripheral readValueForDescriptor:d];
}
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error{
NSLog(@"Descriptor name:%@ value is:%@",descriptor.characteristic.UUID, descriptor.value);
}
可读取描述的UUID和value等信息。
其他