swift - 如何使用 CoreBluetooth 只扫描信标

标签 swift ibeacon core-bluetooth

我用这个功能扫描周围的BLE,但是我收到的信号太多了。我只想处理来自信标的信号。

func centralManagerDidUpdateState(_ central: CBCentralManager) {
    centralManager.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey : NSNumber(value: true)])
}

I had used LightBlue发现我的信标有 5 个服务:180A、FFA0、FFB0、FFF0 和 180F。

然后我尝试了这段代码,但没有找到与此服务相关的外围设备。

let serviceUUIDs = [CBUUID(string: "FFA0")]

func centralManagerDidUpdateState(_ central: CBCentralManager) {
    centralManager.scanForPeripherals(withServices: serviceUUIDs, options: [CBCentralManagerScanOptionAllowDuplicatesKey : NSNumber(value: true)])
}

didDiscover 方法从未调用过

func centralManager(_ central: CBCentralManager,
                    didDiscover peripheral: CBPeripheral,
                    advertisementData: [String : Any],
                    rssi: NSNumber)
{
    if peripheral.name == "MY_BEACON_NAME" {
        print("peripheral: \(peripheral)")
        print("rssi = \(rssi)")
    }
}

我做错了什么?

P. S. 我不能为这个项目使用 CoreLocation,只能使用 CoreBluetooth。

最佳答案

根据您的硬件信标广播的信标格式,可能无法使用 CoreBluetooth 检测。 CoreBluetooth 可以检测Eddystone 格式和AltBeacon 格式,但不能检测iBeacon 格式。

需要理解的几点:

  1. 信标格式不一定包含 GATT 服务 UUID,这是您在使用代码行 centralManager.scanForPeripherals(withServices: serviceUUIDs ... 时要过滤的内容。虽然 Eddystone 广告确实包含服务 UUID,但 iBeacon 和 AltBeacon 不包含,因为它们是一种不同类型的 BLE 广告,称为制造商广告。只有 GATT 广告具有服务 UUID。

  2. Eddystone 格式使用 FEAA 服务 UUID,您可以使用您描述的技术来过滤此信标类型。

  3. 您只能在没有 serviceUUID 过滤器的情况下检测 AltBeacon 广告,因为它没有过滤器。

  4. 永远无法使用 CoreBluetooth 检测到 iBeacon,因为 Apple 有特殊的代码过滤器可以阻止他的 API 接收这些广告。你别无选择,只能在 iOS 上使用 CoreLocation 进行 iBeacon 检测。

  5. 许多信标硬件制造商还包括一些具有自己的服务 UUIDS 的补充 GATT 服务。这些服务执行配置或附加操作等功能。但是这些与信标功能无关,并且无法知道何时(如果有的话)这些服务 UUID 被公布,以及它们的含义。这些 UUID 从一个信标制造商到另一个信标制造商绝对不同。 LightBlue 检测到的服务 UUID 可能是补充服务,因此它们根本无助于解决此问题。

底线:没有简单的方法可以使用 CoreBluetooth 仅过滤您想要的信标。您最好的选择是在 CoreBluetooth 之上编写您自己的过滤软件,并简单地忽略您不关心的数据包。

关于swift - 如何使用 CoreBluetooth 只扫描信标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49134666/

相关文章:

ios - 如何快速读取 UITextInput 中的文本?

ios - 估计距离测量单位

ios - 集成 iBeacon 和 core蓝牙外设

ios - 如何在 ibeacon 中使用唯一标识符?

ios - 多个服务的 BLE 设备相同的 UUID

ios - 在 Swift 5 中更改 Lottie AnimationView 大小

swift - 这个 Swift 语法是什么意思?

bluetooth - 蓝牙中的链路层与硬件相关吗?

ios - HM-10 蓝牙与 iPhone 配对

Swift - 对于 X == -X,Decimal 的 hashValue 是相同的,不能用于比较 hashValues