获取 CBPeripheralManager 订阅的 Centrals
Posted
技术标签:
【中文标题】获取 CBPeripheralManager 订阅的 Centrals【英文标题】:Get CBPeripheralManager's Subscribed Centrals 【发布时间】:2014-10-26 23:39:34 【问题描述】:在实现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];
【问题讨论】:
【参考方案1】:您可以从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];
【讨论】:
完美。完全不知道我是如何在文档中错过的。当然,现在我已经看到了,我可以立即找到它!感谢您的帮助。以上是关于获取 CBPeripheralManager 订阅的 Centrals的主要内容,如果未能解决你的问题,请参考以下文章
如何修复 CBPeripheralManager 传输队列已满?
为啥我的 CBPeripheralManager 有广告但无法被发现?
CBPeripheralManager AddService 上的 iOS 崩溃断言失败: