2015-04-04 30 views
0
_manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; 
[NSThread sleepForTimeInterval:10]; // waiting for centralManagerDidUpdateState invocation 
[_manager scanForPeripheralsWithServices:@[ _serviceUUID ] options:nil]; // warning here 

... 

- (void)centralManagerDidUpdateState:(CBCentralManager *)central { 
    NSLog(@"CBCentralManager state is %i", (int)central.state); 
} 

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { 
    NSLog(@"Found peripheral: %@", peripheral); 
    ... 
} 

出于某种原因,代表centralManagerDidUpdateState则永远不会调用和我得到警告:CBCentralManager没有通电

2015-04-09 12:59:20.850 xctest [30276:303] CoreBluetooth [警告] 未通电

开始发现外设时。 AFAIK它应该在OSX上工作(我在2013年MBA和OSX Maverics上运行XCTest)。蓝牙已打开,我可以运行LightBlue应用程序并发现一些BLE设备(我确定在测试代码时没有运行BLE应用程序)。所以没有didDiscoverPeripheral按预期调用。

+0

可能重复的[无法蓝牙设备连接在IOS 8甚至现有的代码没有找到蓝牙和它回环非常细的上IOS 7最主要](http://stackoverflow.com/questions/26074216/无法连接蓝牙设备在IOS 8甚至现有的代码找不到bluet) – Paulw11 2015-04-04 08:31:00

+0

测试核心蓝牙是棘手的。你应该仔细计划。但是我会说你手动测试CB部件要好得多,因为太多的错误因素使自动化测试变得不可靠。您应该将小型模块/库/接口中的CB交互分开,并且只测试与该接口的交互,您可以轻松地将任何返回值和回调替换为任何调用。 – allprog 2015-04-05 13:30:30

回答

0

我试着使用后台线程作为sleepForTimeInterval块主线程和CBCentralManager默认使用主线程:

dispatch_queue_t bt_queue = dispatch_queue_create("BT_queue", 0); 
_manager = [[CBCentralManager alloc] initWithDelegate:self queue:bt_queue]; 

所以centralManagerDidUpdateState:几乎立即调用和外围发现有didDiscoverPeripheral:

+0

为核心蓝牙使用单独的队列是很好的,但不要“睡眠”。核心蓝牙或多或少是由异步调用和回调构建的。依靠回调,不要尝试自己处理事情。阅读[Apple CB教程](https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/AboutCoreBluetooth/Introduction.html)的相应部分 – allprog 2015-04-05 13:26:02

+0

很好有确认单独的队列使用是该解决方案(不像Palw11坚持)。为了在一个地方使用应用程序逻辑(而不是回调函数),我使用了'sleep'来进行测试。在一般情况下,我同意睡觉不好。 – 4ntoine 2015-04-06 06:03:45

0

对于OS X 10.13.2,CBCentralManager.init()的队列参数必须为非零来触发回调,例如,在夫特

manager = CBCentralManager(
    delegate: self, 
    queue: DispatchQueue(label: "BT_queue")) 
相关问题