ios - 扫描多个供应商的设备时如何从 BLE 广告中获取制造商数据
问题描述
我正在扫描来自 2 个不同制造商的 BLE 外围设备。如果不扫描所有服务,我就无法从两个设备接收广告,这是不可接受的。
当我配置 scanForPeripherals(withServices: nil) 我收到两个广告。但是,当我扫描特定服务(0xF4FD 和 0x0275)时,我没有收到来自 0x0275 设备的数据包。
- 供应商 0xF4FD 在 CBAdvertisementDataServiceDataKey 中宣传我需要的信息。
- 供应商 0x0275 宣传我需要的信息,但在 CBAdvertisementDataManufacturerDataKey 中。
因此,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 格式。我的问题现在变成了:
- 我可以同时扫描服务和 iBeacons 吗?
解决方案
推荐阅读
- python - 根据条目长度将字符串添加到列
- angular - Angular OnInit:从服务订阅返回的值返回 Empty 空白,因为 API 尚未调用
- sql - 删除sql单元格中的重复数字
- html - 我们可以在没有表单操作提交的情况下将数据从文本框传递到节点 js 吗?
- python - struct.error: 在 Python3.5 中解压需要一个长度为 16 的字节对象
- excel - 如何将相应的值添加到右列(我有一个代码,但速度不够快)
- python - 输入形状的奇数 Dimension() 对象?
- elasticsearch - 检查无痛中的空参数 - Elasticsearch
- python - 如何在返回列表项的标签中显示函数的结果?
- android - 如何从 ImageView 中的相机显示中获取图像而不会在 Android 中丢失其质量?