首页 > 解决方案 > 扫描多个供应商的设备时如何从 BLE 广告中获取制造商数据

问题描述

我正在扫描来自 2 个不同制造商的 BLE 外围设备。如果不扫描所有服务,我就无法从两个设备接收广告,这是不可接受的。

当我配置 scanForPeripherals(withServices: nil) 我收到两个广告。但是,当我扫描特定服务(0xF4FD 和 0x0275)时,我没有收到来自 0x0275 设备的数据包。

因此,1 个供应商在 ServiceData 中提供数据,而另一个则使用 ManufacturerData。

如何在不扫描所有外围设备的情况下接收这两个广告?

if AppDelegate.cbManager?.state == .poweredOn {

    // 0xF4FD is VendorA device. 0x0275 is VendorB
    // 0xFDF4 alone collects VendorA devices.
    // 0x7502 alone gets nothing
    // both behave the same as 0xFDF4 alone
    // nil gets everything, but kills the battery and collects unwanted devices
    let svcId1 = CBUUID(string: "0xFDF4")
    let svcId2 = CBUUID(string: "0x7502")
    let desiredServices = [svcId2, svcId1]
    cbManager.scanForPeripherals(withServices: desiredServices, options: nil)
}

...和接收方...

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

    if let key = advertisementData.keys.first(where: { $0.contains(CBAdvertisementDataServiceDataKey) }), let result = advertisementData[key] {
        print("0xFDF4 peripheral: \(result)")
    }

    if let key = advertisementData.keys.first(where: { $0.contains(CBAdvertisementDataManufacturerDataKey) }), let result = advertisementData[key] {
        print("0x7502 peripheral: \(result)")
    }
}

... 输出 ...

0x7502 peripheral: <750200c6 64ff0b01 3e839fcb f863000e ed91>
0xFDF4 peripheral: {
    FDF4 = <0103fa60 19aaa900 002b51>;
}
0xFDF4 peripheral: {
    FDF4 = <0111f31f 6f8fee00 05368d>;
}
0x7502 peripheral: <750200c6 64ff0b01 103e1cde a0550005 f545>

接收代码成功提取数据(如果/何时获取数据)。

编辑 1

对 scanForPeripherals(withServices: "0xFDF4" ) 的调用允许我仅扫描 VendorA 的服务。这与广告数据包中的第 5 个字节 = 0x16相关(在 BLE 规范中称为“服务数据 16 位 UUID”)。

但是,VendorB 在制造商数据中提供了所需的信息,这与 Advertising Data= 0xff相关。(BLE 规范中的“制造商特定数据”)。这两个参数似乎是互斥的:我可以获得服务数据制造商数据。

编辑 2

我了解到供应商 B 不宣传任何服务。此外,Advertising Data Type=0xFF 对应于 iBeacon 格式。我的问题现在变成了:

标签: iosswiftcore-bluetooth

解决方案


推荐阅读