【问题标题】:Core Bluetooth - constant RSSI updates of in-range devices核心蓝牙 - 范围内设备的持续 RSSI 更新
【发布时间】: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


【解决方案1】:

据我所知,这应该可以满足您的需求。

当您开始使用原始调用扫描外围设备时,只要发现 BLE 设备,您的代理就应该开始接收调用。这将一直持续到您通过调用来停止扫描

[manager stopScan];

我认为您实际上不需要在您的 centralManager:didRetrievePeripherals 方法中第二次调用 scanForPeripheralsWithServices,因为据我所知,扫描不会停止,直到您告诉它。不过,我也仍在着手处理这个问题,可能还有一个我还没有找到的超时时间。

我很确定您每分钟接到一次电话的原因是因为 BLE 设备只经常做广告。如果它更频繁地做广告,比如处于发现模式的设备,我想你会更频繁地接到电话。如果你能证实这一点,我会很有趣。如果设备有发现模式,您可以尝试触发它以查看通知是否加快。

【讨论】:

    【解决方案2】:

    您不应该进行连续扫描,因为它非常耗电。一旦你发现了设备,你就会得到一个返回给你的 CBPeripheral 对象数组。在 CBPeripheral 上,您可以读取 RSSI 并在 RSSI 更改时获得回调。请参阅以下文档: http://developer.apple.com/library/mac/#documentation/CoreBluetooth/Reference/CBPeripheralDelegate_Protocol/translated_content/CBPeripheralDelegate.html

    【讨论】:

      【解决方案3】:

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

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

      如果您这样做,您的 iPhone 看到的每个广告包都会收到您的“didDiscoverPeripheral”回调,通常大约每 100 毫秒一次(尽管我发现同一设备的回调时间变化很大)。这包括它看到的每个设备的 RSSI。

      这应该比您大约 1 分钟的更新速度快很多。

      【讨论】:

      • 这两行的 Swift 等价物是什么?
      • @Gerard:请看下面我的回答。
      • Swift3: manager.scanForPeripherals(withServices: [sensorTagAdvertisingUUID], options: [CBCentralManagerScanOptionAllowDuplicatesKey : NSNumber(value: true)])
      【解决方案4】:

      @Anders 解决方案的 Swift 实现:

      manager.scanForPeripheralsWithServices(nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey : NSNumber(value: true)])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-18
        • 2014-08-10
        • 1970-01-01
        • 1970-01-01
        • 2013-04-21
        • 2016-09-14
        • 1970-01-01
        相关资源
        最近更新 更多