核心蓝牙 - 范围内设备的不断RSSI更新

kj1*_*ett 14 ios ios5 core-bluetooth

我刚刚开始使用iOS的核心蓝牙框架,我正在开发一个需要不断扫描BLE设备的应用程序,以便我可以每分钟检索一次RSSI编号.

目前我有:

manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[manager scanForPeripheralsWithServices:nil options:options];
Run Code Online (Sandbox Code Playgroud)

这启动我的应用程序扫描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]];}
Run Code Online (Sandbox Code Playgroud)

这个方法得到我可以显示的外围设备的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];

}
Run Code Online (Sandbox Code Playgroud)

这段代码似乎正在工作,大约每1分钟扫描一次,但我不知道为什么它正在工作......

关于核心蓝牙的文档相当稀疏,所以如果有人对如何做到这一点有任何想法,或者有更好的方法来做我想要完成的事情,我会很感激帮助!

小智 20

您是否尝试将扫描选项更改为YES?

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber  numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[manager scanForPeripheralsWithServices:nil options:options];
Run Code Online (Sandbox Code Playgroud)

如果你这样做,你将获得你的iPhone看到的每个广告包的"didDiscoverPeripheral"回调,通常大约每100毫秒(虽然我看到这个回调时间对于同一设备变化很大).这包括它看到的每个设备的RSSI.

这应该比你的~1分钟更新速度快很多.