在实施CBPeripheralManagerDelegate
方法-peripheralManager:willRestoreState
时,字典中传递给密钥CBPeripheralManagerRestoredStateServicesKey
Apple文档的方法的对象表明
恢复有关服务的所有信息,包括任何包含的服务,特征,特征描述符和订阅的中心。
从返回的CBMutableServices
数组中,我可以遍历服务,然后循环遍历每个服务的特征。但是,我无法弄清楚如何访问订阅的中心,任何人都可以帮忙吗?
以下是我使用的通用代码,它将值分配给局部变量等:
- (void)peripheralManager:(CBPeripheralManager *)peripheral willRestoreState:(NSDictionary *)dict
{
advertisementData = dict[CBPeripheralManagerRestoredStateAdvertisementDataKey];
NSArray *services = dict[CBPeripheralManagerRestoredStateServicesKey];
for (CBMutableService *service in services) {
mainService = service;
for (CBMutableCharacteristic *charactristic in mainService.characteristics) {
if ([charactristic.UUID.UUIDString isEqualToString:AUTH_UUID]) {
authCharacteristic = charactristic;
} else if ([charactristic.UUID.UUIDString isEqualToString:CENTRAL_NAME_UUID]) {
receiveDeviceNameCharacteristic = charactristic;
}
}
// How would I reinstantiate subscribed centrals?
// subscribedCentrals = ?
[manager addService:mainService];
}
}
答案 0 :(得分:3)
您可以从CBMutableCharacteristic
对象
所以,像 -
NSMutableSet *centrals=[NSMutableSet new];
for (CBMutableCharacteristic *charactristic in mainService.characteristics) {
if ([charactristic.UUID.UUIDString isEqualToString:AUTH_UUID]) {
authCharacteristic = charactristic;
} else if ([charactristic.UUID.UUIDString isEqualToString:CENTRAL_NAME_UUID]) {
receiveDeviceNameCharacteristic = charactristic;
}
for (CBCentral *central in characteristic.subscribedCentrals) {
[centrals addObject:central];
}
}