首页 文章

iOS 7的CBCentralManager更改

提问于
浏览
8

我正在尝试使用Apple 's ' BTLE Transfer ' sample project to understand CoreBluetooth programming. The app runs fine if I use an iOS 6 device as the Central, but if I run the same app with the iOS 7 device as the Central it doesn' t工作 . 外围设备在两个数据包之后停止发送,并且中心不接收其中任何一个数据包 .

唯一的线索就是这个警告,我只有在iOS 7上运行才会得到:

CoreBluetooth[WARNING] <CBCentralManager: 0x15654540> is disabling duplicate filtering, but is using the default queue (main thread) for delegate events

任何人都可以告诉我需要更改什么才能使这个应用程序与iOS 7兼容?

编辑:当两个设备都是iOS7时没有问题 . 这只会在iOS7中心与iOS6外围设备通话时中断 .

2 回答

  • 8

    好吧,我刚刚在iOS 7外设的iOS 7中心运行它 . 如果您想要关于禁用重复过滤的警告消失,请在另一个线程上运行它 . 做这样的事情:

    dispatch_queue_t centralQueue = dispatch_queue_create("com.yo.mycentral", DISPATCH_QUEUE_SERIAL);// or however you want to create your dispatch_queue_t
    _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue];
    

    现在,它将允许您扫描并启用重复项 . 但是,您必须在主线程上调用textView setter才能设置文本而不会崩溃:

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.textview setText:[[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]];
            });
    

    顺便说一句,您可能也想采用新的iOS 7委托初始化:

    _centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:centralQueue options:nil];//set the restoration options if you want
    

    (只需检查iOS版本并调用相应的初始化方法)

  • 5

    scanForPeripheralsWithServices:options: 中,如果设置 CBCentralManagerScanOptionAllowDuplicatesKey:@YES ,则将其更改为 CBCentralManagerScanOptionAllowDuplicatesKey:@NO ,这意味着扫描应该在没有重复过滤的情况下运行 .

    对我来说,它也适用于iOS7 .

相关问题