【问题标题】:Get CBPeripheralManager's Subscribed Centrals获取 CBPeripheralManager 订阅的 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];
    }
}

【问题讨论】:

    标签: ios objective-c macos core-bluetooth


    【解决方案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];
            }
    }
    

    【讨论】:

    • 完美。完全不知道我是如何在文档中错过的。当然,现在我已经看到了,我可以立即找到它!感谢您的帮助。
    猜你喜欢
    • 2017-12-03
    • 2019-07-29
    • 2017-08-05
    • 2022-09-27
    • 2022-01-19
    • 2016-07-23
    • 2022-10-15
    • 2013-02-13
    • 2011-12-14
    相关资源
    最近更新 更多