iOS 7的CBCentralManager更改

Dre*_*w C 8 iphone bluetooth ios core-bluetooth ios7

我正在尝试使用Apple的" BTLE Transfer "示例项目来了解CoreBluetooth编程.如果我使用iOS 6设备作为中心,应用程序运行正常,但如果我使用iOS 7设备作为中心运行相同的应用程序它不起作用.外围设备在两个数据包之后停止发送,并且中心不接收其中任何一个数据包.

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

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

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

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

Tom*_*voy 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];
Run Code Online (Sandbox Code Playgroud)

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

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

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

_centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:centralQueue options:nil];//set the restoration options if you want
Run Code Online (Sandbox Code Playgroud)

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


man*_*e99 5

scanForPeripheralsWithServices:options:,如果您设置CBCentralManagerScanOptionAllowDuplicatesKey:@YES然后将其更改为, CBCentralManagerScanOptionAllowDuplicatesKey:@NO这意味着扫描应该运行而不重复过滤.

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