使用 iOS 获取低功耗蓝牙扫描响应数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25390484/
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-31 01:56:03  来源:igfitidea点击:

Obtaining Bluetooth LE scan response data with iOS

iosbluetoothbluetooth-lowenergycore-bluetoothibeacon

提问by ccpmark

I am working with Bluetooth Low Energy devices, and I was wondering whether it is possible to read the Scan Response Data to an advertisement with iOS and Core Bluetooth without connecting.

我正在使用低功耗蓝牙设备,我想知道是否可以在不连接的情况下使用 iOS 和 Core 蓝牙读取广告的扫描响应数据。

I understand that after reading an advertisement packet, you can request additional data from the peripheral in the format of a 31 byte scan response. I know that Core Bluetooth suggests that if the ad packet is full, you can put the local name in the scan response packet, but does it allow you to see the whole packet?

据我了解,在读取广告包后,您可以以 31 字节扫描响应的格式从外设请求附加数据。我知道Core Bluetooth 建议,如果广告包已满,您可以将本地名称放在扫描响应包中,但是否可以让您看到整个包?

回答by davidgyoung

Yes, you can use CoreBluetoothto read the full manufacturer data or service data bytes of a BLE advertisement as long as it is NOT an iBeacon advertisement.If it is an iBeacon advertisement, CoreBluetoothwill block your ability to see the bytes. The callback you use is as follows:

是的,您可以使用CoreBluetooth读取 BLE 广告的完整制造商数据或服务数据字节,只要它不是 iBeacon 广告。如果是 iBeacon 广告,CoreBluetooth将阻止您查看字节。你使用的回调如下:

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

The raw service data or manufacturer data bytes will be present inside the NSDictionary *advertisementData. But they key holding those data will be removed by the operating system for iBeacons.

原始服务数据或制造商数据字节将出现在NSDictionary *advertisementData. 但是它们保存这些数据的密钥将被 iBeacons 的操作系统删除。

Here's an example of what you get in the advertisementData NSDictionaryin the callback. This example is for detecting an AltBeacon advertisement (an open-source beacon standard), with identifiers 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 2

这是您NSDictionary在回调中的 adsData 中获得的示例。此示例用于检测 AltBeacon 广告(开源信标标准),标识符为 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 2

{
    kCBAdvDataIsConnectable = 0;
    kCBAdvDataManufacturerData = <1801beac 2f234454 cf6d4a0f adf2f491 1ba9ffa6 00010002 be00>;
}

You can see how to decode the above bytes by looking at the AltBeacon spec here.

您可以通过查看此处的 AltBeacon 规范了解如何解码上述字节

For more details about why you can't read iBeacon data along with additional code showing how you set this up, see here:

有关为什么无法读取 iBeacon 数据以及显示如何设置的附加代码的更多详细信息,请参见此处:

http://developer.radiusnetworks.com/2013/10/21/corebluetooth-doesnt-let-you-see-ibeacons.html

http://developer.radiusnetworks.com/2013/10/21/corebluetooth-doesnt-let-you-see-ibeacons.html

回答by eclux

I am working with a peripheral that has some manufacturer data which I believe is transmitted in the scan response because there's no room for it in the initial advertisement with a 128-bit UUID plus channel, RSSI, and connectable flag. I am receiving two calls to didDiscoverPeripheral:... in quick succession (3ms apart including some handling time in my code). The first does not have the kCBAdvDataManufacturerData key in the dict, but the second does. I am assuming that the scan response isbeing requested automatically and the reply results in the second call.

我正在使用具有一些制造商数据的外围设备,我认为这些数据是在扫描响应中传输的,因为在具有 128 位 UUID 加通道、RSSI 和可连接标志的初始广告中没有空间。我收到了两个对 didDiscoverPeripheral:... 的快速调用(相隔 3 毫秒,包括我的代码中的一些处理时间)。第一个在字典中没有 kCBAdvDataManufacturerData 键,但第二个有。我假设扫描应答被自动请求,并在第二个电话回复结果。