当实现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];
}
}
最佳答案
您可以从CBMutableCharacteristic
对象中检索subscribed centrals-
因此,类似-
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];
}
}