gpt4 book ai didi

ios - 获取 CBPeripheralManager 订阅的 Central

转载 作者:行者123 更新时间:2023-11-28 19:55:40 26 4
gpt4 key购买 nike

当实现 CBPeripheralManagerDelegate 方法 -peripheralManager:willRestoreState 时,字典中的对象传递给键 CBPeripheralManagerRestoredStateServicesKey Apple 的文档指出

All the information about a service is restored, including any included services, characteristics, characteristic descriptors, and subscribed centrals.

从返回的 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];
}
}

最佳答案

您可以检索 subscribed centrals来自 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];
}
}

关于ios - 获取 CBPeripheralManager 订阅的 Central,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26579255/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com