有时我们需要将我们的设备作为外设,来为其他中心设备提供服务。需要以下步骤:
1.创建一个CBPeripheralManager实例
2.基于peripheral实例,创建services和characteristics实例。
3.发布services和characteristics到你设备本地数据库
4.广播你创建的服务
5.对中心设备的读写请求做出相应,向订阅数据的中心设备发送数据更新通知
myPeripheralManager =
[[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
通过唯一的UUID来标示你创建的characteristics和services,UUID为128位,可以通过命令行uuidgen来产生多个UUID,一些通用的UUID为16位标示,但是系统会默认将其补充为128位。自己生成的UUID必须为128位。
CBUUID *heartRateServiceUUID = [CBUUID UUIDWithString: @"180D"];//180D为通用的心率服务ID
CBUUID *myCustomServiceUUID =
[CBUUID UUIDWithString:@"71DA3FD1-7E10-41C1-B16F-4430B506CDE7"];
myCharacteristic =
[[CBMutableCharacteristic alloc] initWithType:myCharacteristicUUID
properties:CBCharacteristicPropertyRead
value:myValue permissions:CBAttributePermissionsReadable];//创建特征
myService = [[CBMutableService alloc] initWithType:myServiceUUID primary:YES];//创建服务
myService.characteristics = @[myCharacteristic];//
[myPeripheralManager addService:myService];//发布服务,发布之后会加入到本地数据库,之后就不能修改服务了
// CBPeripheralManagerDelegate
- (void)peripheralManager:(CBPeripheralManager *)peripheral
didAddService:(CBService *)service
error:(NSError *)error {
if (error) {
NSLog(@"Error publishing service: %@", [error localizedDescription]);
}
...
}
发布服务到本地数据库之后就可以广播服务了
[myPeripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey :
@[myFirstService.UUID, mySecondService.UUID] }];
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral
error:(NSError *)error {
if (error) {
NSLog(@"Error advertising: %@", [error localizedDescription]);
}
...
}
对中心设备的读写请求相应
- (void)peripheralManager:(CBPeripheralManager *)peripheral
didReceiveReadRequest:(CBATTRequest *)request {
if ([request.characteristic.UUID isEqual:myCharacteristic.UUID]) {
...
if (request.offset > myCharacteristic.value.length) {
[myPeripheralManager respondToRequest:request
withResult:CBATTErrorInvalidOffset];
return;
}else {
request.value = [myCharacteristic.value
subdataWithRange:NSMakeRange(request.offset,
myCharacteristic.value.length - request.offset)];
[myPeripheralManager respondToRequest:request withResult:CBATTErrorSuccess];
}
}
}
对于写请求,在写代理方法中类似实现方式,修改值使用
myCharacteristic.value = request.value;方法。
对于中心设备注册监听的特征
- (void)peripheralManager:(CBPeripheralManager *)peripheral
central:(CBCentral *)central
didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {
NSLog(@"Central subscribed to characteristic %@", characteristic);
...
}
NSData *updatedValue = // fetch the characteristic's new value
BOOL didSendValue = [myPeripheralManager updateValue:updatedValue
forCharacteristic:characteristic onSubscribedCentrals:nil];//如果用于更新的队列满了,会返回NO,当有队列有空位时会调用peripheralManagerIsReadyToUpdateSubscribers: 方法,你可以在这个方法里重新发送更新数据
其他需要注意的事项:
1.限制广播数据的大小,最大为28byte + 10byte(专用于localNameKey),在广播的字典里,只能传入 CBAdvertisementDataLocalNameKey
and CBAdvertisementDataServiceUUIDsKey类型的数据。
2.在不需要时,停止广播
[myPeripheralManager stopAdvertising];
3.对于敏感数据,进行配对验证,对于需要订阅的数据设置,订阅权限。
myCharacteristic = [[CBMutableCharacteristic alloc]
initWithType:myCharacteristicUUID
properties:CBCharacteristicPropertyRead | CBCharacteristicPropertyNotify
value:nil permissions:CBAttributePermissionsReadable];
emailCharacteristic = [[CBMutableCharacteristic alloc]
initWithType:emailCharacteristicUUID
properties:CBCharacteristicPropertyRead
| CBCharacteristicPropertyNotifyEncryptionRequired
value:nil permissions:CBAttributePermissionsReadEncryptionRequired];