在 iOS 中获取 BLE 的广告数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22833198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:53:41  来源:igfitidea点击:

Get advertisement data for BLE in iOS

iosobjective-cbluetooth-lowenergyibeacon

提问by user2564511

I'm creating an app that gets the UUID of all BLE devices within range. I've gotten it working fine in Android, where it gives me the advertisement data as a byte array. Now I'm trying to do the same in iOS.

我正在创建一个应用程序,它获取范围内所有 BLE 设备的 UUID。我让它在 Android 中运行良好,它以字节数组的形式为我提供广告数据。现在我正在尝试在 iOS 中做同样的事情。

I'm running a scan and it's detecting the device, and in the callback there's an NSDictionary called advertisementData. But the only information in it is as follows:

我正在运行扫描,它正在检测设备,在回调中有一个名为 adsData 的 NSDictionary。但其中唯一的信息如下:

kCBAdvDataChannel = 39;
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = jaalee;
kCBAdvDataTxPowerLevel = 0;

Am I right in thinking I should be getting the entirety of the advertising packet? If not, how can I get it?

我认为我应该获得整个广告包是正确的吗?如果没有,我怎样才能得到它?

回答by davidgyoung

Unfortunately, iOS does not allow you to access the raw advertisement data. I wrote a blog postdemonstrating this. While the post is specifically about iBeacons, it applies to any BLE advertisement.

不幸的是,iOS 不允许您访问原始广告数据。我写了一篇博客文章来证明这一点。虽然该帖子专门针对 iBeacons,但它适用于任何 BLE 广告。

EDIT:To clarify, you can read the raw manufacturer data bytes or service data bytes of non-iBeacon advertisements. It is only the iBeacon advertisements that have their manufacturer data bytes hidden by CoreLocation. See here: Obtaining Bluetooth LE scan response data with iOS

编辑:澄清一下,您可以读取非 iBeacon 广告的原始制造商数据字节或服务数据字节。只有 iBeacon 广告的制造商数据字节被CoreLocation. 请参阅此处:使用 iOS 获取蓝牙 LE 扫描响应数据

The equivalent MacOS CoreLocation methods do allow this, so it is probably an intentional security or power saving restriction on iOS.

等效的 MacOS CoreLocation 方法确实允许这样做,因此这可能是对 iOS 的有意安全或节能限制。

回答by megabri

Based on official documentation from Apple iOS and my personal experience:

基于 Apple iOS 的官方文档和我的个人经验:

YES, iOS does not allow you to access the RAW advertisement data.

是的,iOS 不允许您访问 RAW 广告数据。

BUT

If your intention is to put information in advertising packet and read them from the iOS app without connect with the peripheral, this is possible to do. Here is described how:

如果您的意图是将信息放入广告包中并在不连接外设的情况下从 iOS 应用程序中读取它们,则可以这样做。这里描述了如何:

1) in the peripheral firmwareyou have to insert your manufacturer specifica data in the advertising packet, with the data type GAP_ADTYPE_MANUFACTURER_SPECIFIC(0xFF) Remember that in the Manufacturer Specific Data, first 2 octets contain the Company Identifier Code followed by the additional manufacturer specific data

1) 在外围固件中,您必须在广告数据包中插入制造商规范数据,数据类型为GAP_ADTYPE_MANUFACTURER_SPECIFIC( 0xFF) 请记住,在制造商特定数据中,前 2 个八位字节包含公司标识符代码,后跟附加制造商特定数据

2) in the iOS

2)在iOS中

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

You can see the manufacturer specific data in advertisementData dictionary with key:

您可以使用键在 adsData 字典中查看制造商特定数据:

CBAdvertisementDataManufacturerDataKey

3) if you want that in your App receive a callback notification every advertising packet sent by peripheral to iOS, remember to change the scan option to YES. Look at this post about that: Core Bluetooth - constant RSSI updates of in-range devices

3)如果你想让你的应用程序在每个外设发送到iOS的广告包中收到回调通知,记得将扫描选项更改为是。看看这篇关于这个的帖子:核心蓝牙 - 范围内设备的持续 RSSI 更新

In my blog post will be soon a tutorial: http://www.megabri.com/

在我的博客文章中将很快有一个教程:http: //www.megabri.com/