核心蓝牙 - 范围内设备的不断RSSI更新 [英] Core Bluetooth - constant RSSI updates of in-range devices

查看:92
本文介绍了核心蓝牙 - 范围内设备的不断RSSI更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用适用于iOS的核心蓝牙框架,我正在开发一款需要不断扫描BLE设备的应用程序,这样我每分钟都可以检索到它们的RSSI号码。

I just started with the core bluetooth framework for iOS and I'm developing an app that needs to constantly scan for BLE devices so that I can retrieve their RSSI number every minute or so.

目前我有:

manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[manager scanForPeripheralsWithServices:nil options:options];

这启动我的应用程序扫描BLE设备并在发现设备时调用此委托方法:

this starts my app scanning for BLE devices and calls this delegate method when a device is discovered:

- (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号码。然后最后一行调用此委托方法:

this method gets me the peripheral's RSSI number which i can display. The last line then calls this delegate method:

- (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分钟,但我不确切知道它为什么工作...

This code seems to be working and doing a scan roughly every 1 minute, but I don't exactly know why it working...

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

The documentation on core bluetooth is pretty sparse so if anyone has any idea on how to do this, or has a better way to do what I'm trying to accomplish I'd appreciate the help!

推荐答案

你试过吗?将扫描选项更改为YES?

Have you tried changing the scan option to YES?

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber  numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[manager scanForPeripheralsWithServices:nil options:options];

如果这样做,您将获得didDiscoverPeripheral回调,其中包含您的每个广告包iPhone,通常大约每100毫秒(虽然我看到这个回调时间在同一设备上变化很大)。这包括它看到的每个设备的RSSI。

If you do this you will get your "didDiscoverPeripheral" callback with every ad packet that is seen by your iPhone, which would normally be about every 100ms (although I see this callback timing varying a lot for the same device). This includes the RSSI of each device it sees.

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

This should be a lot faster than your ~1 minute update rate.

这篇关于核心蓝牙 - 范围内设备的不断RSSI更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆