peripheral模式的流程:
1. 设置CBPeripheralManagerDelegate的委托,初始化外围设备
2. 检测外设的蓝牙状态
3. 创建服务(service)和特征(characteristics)
4. 开始广播 (中心设备收到广播后连接外设,并进行一系列的请求)
5. 响应center的操作
* 读写characteristics
* 订阅和取消订阅characteristics
实现步骤:
1. 初始化外设
self.periperalMgr = [[CBPeripheralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
2. 检测外设的蓝牙状态后创建服务(service)和特征(characteristics)
#pragma mark - CBPeripheralManagerDelegate -
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
/*
CBManagerStateUnknown = 0, // 未知
CBManagerStateResetting, // 重置中
CBManagerStateUnsupported, // 不支持
CBManagerStateUnauthorized, // 未验证
CBManagerStatePoweredOff, // 关闭
CBManagerStatePoweredOn, // 开启
*/
if(peripheral.state == CBManagerStatePoweredOn) {
// 2. 创建服务(Service)和特征(Characteristics)
[self setup];
}
}
3.创建服务和特征
// 安装特征和服务
- (void)setup {
// 创建服务的UUID
CBUUID *serviceUUID = [CBUUID UUIDWithString:SERVICE_UUID];
// 创建服务
CBMutableService *service = [[CBMutableService alloc] initWithType:serviceUUID primary:YES];
// 创建特征的UUID
CBUUID *characteristicUUID = [CBUUID UUIDWithString:CHARACTERISTIC_UUID];
// 注意: value设置为nil 创建一个可读写,可订阅的characteristic
CBMutableCharacteristic *characteristic = [[CBMutableCharacteristic alloc] initWithType:characteristicUUID properties:CBCharacteristicPropertyRead | CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsReadable | CBAttributePermissionsWriteable];
// 把特征放进服务中
service.characteristics = @[characteristic];
// 把服务器添加到外设中
//添加后就会调用代理的- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error
[self.periperalMgr addService:service];
}
4.开始广播
- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error
{
// 开始广播
[self.periperalMgr startAdvertising:@{
CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:SERVICE_UUID]]
}];
}
//peripheral开始发送advertising
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error
{
}
5.响应center操作
//中心设备读characteristics请求
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request{
//判断是否有读数据的权限
if (request.characteristic.properties & CBCharacteristicPropertyRead) {
NSData *data = request.characteristic.value;
[request setValue:data];
//对请求作出成功响应
[self.periperalMgr respondToRequest:request withResult:CBATTErrorSuccess];
}else{
[self.periperalMgr respondToRequest:request withResult:CBATTErrorWriteNotPermitted];
}
}
//中心设备写characteristics请求
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests{
CBATTRequest *request = requests[0];
//判断是否有写数据的权限
if (request.characteristic.properties & CBCharacteristicPropertyWrite) {
//需要转换成CBMutableCharacteristic对象才能进行写值
CBMutableCharacteristic *myCharacteristic =(CBMutableCharacteristic *)request.characteristic;
myCharacteristic.value = request.value;
[self.periperalMgr respondToRequest:request withResult:CBATTErrorSuccess];
}else{
[self.periperalMgr respondToRequest:request withResult:CBATTErrorWriteNotPermitted];
}
}