核心蓝牙特征通知不起作用

 林小琳LLL 发布于 2023-01-09 17:25

我正在尝试编写一个接收通知的BTLE监控应用程序.我使用Core Bluetooth(在iOS 7.1上)构建了中央应用程序和模拟外围应用程序.我的中心可以很好地连接到外围设备,它宣传具有2个CBCharacteristicPropertyNotify特征的服务.但是,一旦中心发现了这些特征(为了清晰起见,删除了非必要的代码),就会发生这种情况.

PD = CBPeripheralDelegate在中央

PMD =外围设备中的CBPeripheralManagerDelegate

调用PD回调

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    for (CBCharacteristic *characteristic in service.characteristics)
    {
        [peripheral setNotifyValue:YES forCharacteristic:characteristic];
    }
}

对于2个特征,PMD回调被调用两次

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic
{
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:FILTER_GENUINE_CHARACTERISTIC_UUID]])
    {
        BOOL genuineSent = [self.peripheralManager updateValue:genuineValue
                                                     forCharacteristic:self.filterGenuineCharacteristic
                                                  onSubscribedCentrals:nil];
    }
    else if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:FILTER_LOADING_CHARACTERISTIC_UUID]])
    {
        BOOL loadingSent = [self.peripheralManager updateValue:loadingValue
                                                     forCharacteristic:self.filterLoadingCharacteristic
                                                  onSubscribedCentrals:nil];
    }
}

注:第一次调用此方法返回的updateValue:forCharacteristic:onSubscribedCentrals:YES.第二次总是回归NO.

PD回调为2个特征调用了两次

- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

注意:characteristic.isNotifying总是YES.

但就是这样,没有其他回调方法被调用!

peripheralManagerIsReadyToUpdateSubscribers:永远不会为updateValue:forCharacteristic:onSubscribedCentrals:返回的调用调用PMD回调NO

- (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral

peripheral:didUpdateValueForCharacteristic:error:永远不会为updateValue:forCharacteristic:onSubscribedCentrals:返回的调用调用PD回调YES

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

我一直在运行iOS 7.1的iPad Air和iPhone 5s上测试这些应用程序.

知道为什么永远不会调用通知吗?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有