【发布时间】:2012-04-12 06:10:28
【问题描述】:
我刚开始使用 iOS 的核心蓝牙框架,我正在开发一个需要不断扫描 BLE 设备的应用程序,以便我可以每分钟左右检索它们的 RSSI 编号。
目前我有:
manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[manager scanForPeripheralsWithServices:nil options:options];
这将启动我的应用程序扫描 BLE 设备并在发现设备时调用此委托方法:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.UUID, advertisementData);
//Do something when a peripheral is discovered.
rssiLabel.text = [RSSI stringValue];
[manager retrievePeripherals:[NSArray arrayWithObject:(id)peripheral.UUID]];}
这个方法可以得到我可以显示的外围设备的 RSSI 号码。最后一行然后调用这个委托方法:
- (void) centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals {
NSLog(@"Currently known peripherals :");
int i = 0;
for(CBPeripheral *peripheral in peripherals) {
NSLog(@"[%d] - peripheral : %@ with UUID : %@",i,peripheral,peripheral.UUID);
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[manager scanForPeripheralsWithServices:nil options:options];
}
这段代码似乎在工作,大约每 1 分钟扫描一次,但我不知道它为什么工作......
关于核心蓝牙的文档非常少,所以如果有人对如何做到这一点有任何想法,或者有更好的方法来完成我想要完成的工作,我将不胜感激!
【问题讨论】:
-
我自己刚开始阅读文档,所以你比我走得更远。问题,你为什么在委托方法 didRetrievePeripherals 中调用 scanForPeripheralsWithServices?在分配 CBCentralManager 之后,您已经调用它。这可能会导致您提到的重复扫描。
-
似乎工作正常... RSSI 多久更新一次?编辑:每分钟一次?我认为您没有连接时会超时,因此它会重新开始扫描。
-
我的两分钱>=7.0:从现在开始需要使用retrievePeripheralsWithIdentifiers。
标签: ios ios5 core-bluetooth