获取CBPeripheralManager的订阅中心

时间:2014-10-26 23:39:34

标签: ios objective-c macos core-bluetooth

在实施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 个答案:

答案 0 :(得分:3)

您可以从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];
        }
}
相关问题