简体   繁体   English

iOS 和 CBAdvertisementDataLocalNameKey 大小上的 BLE 广告

[英]BLE advertising on iOS and CBAdvertisementDataLocalNameKey size

What is the maximum size of data advertised as CBAdvertisementDataLocalNameKey when using bluetooth low energy advertising on iOS?在 iOS 上使用蓝牙低功耗广告时,广告为 CBAdvertisementDataLocalNameKey 的数据的最大大小是多少? While doing some testing I used following code for BLE advertising:在进行一些测试时,我使用以下代码进行 BLE 广告:

var manager: CBPeripheralManager!
//...
let customData = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
manager.startAdvertising([CBAdvertisementDataServiceUUIDsKey:[serviceCBUUID], CBAdvertisementDataLocalNameKey: customData])

I noticed that on a second device in delegate method called when peripheral is detected ( func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) ) my custom data is cut to following string: "01234567" so it has only 8 bytes.我注意到,在检测到外围设备时调用的委托方法中的第二个设备( func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) )我的自定义数据被剪切为以下string: "01234567" 所以它只有 8 个字节。 The documentation of startAdvertising function mentions something about "up to 28 bytes of space in the initial advertisement data for any combination of the supported advertising data keys" . startAdvertising function 的文档提到“在初始广告数据中最多 28 个字节的空间,用于支持的广告数据键的任意组合” Can someone explain why in my case it is only 8 bytes then?有人可以解释为什么在我的情况下它只有 8 个字节吗? Is it in practice always 8 bytes for this CBAdvertisementDataLocalNameKey on iOS or can this number change? iOS 上的这个 CBAdvertisementDataLocalNameKey 实际上总是 8 个字节,还是这个数字可以改变? How can I calculate number of bytes that will be successfully send in advertising packet (in my case 8 bytes)?如何计算将在广告包中成功发送的字节数(在我的情况下为 8 个字节)? Is there any way to advertise more than 8 bytes when using BLE on iOS?在 iOS 上使用 BLE 时,有什么方法可以宣传超过 8 个字节? Is there any limitation for number of advertised services at the same time?同时广告服务的数量是否有限制? For example would it be possible to advertise at the same time 10 different uuid's each with 8 bytes which in total would allow to advertise 80 bytes at the same time?例如,是否可以同时宣传 10 个不同的 uuid,每个 8 个字节,总共允许同时宣传 80 个字节?

As with most BLE things, it depends.与大多数 BLE 的东西一样,这取决于。 But as a rule the size of the advertising packet is 31 bytes unless you have extended advertising (which is sometimes available on newer phones and newer versions of iOS, more about that below).但通常广告数据包的大小为 31 字节,除非您有扩展广告(有时可在较新的手机和较新版本的 iOS 上使用,更多信息见下文)。 The full advertising area is 37 bytes minus 6 mandatory bytes for the MAC.完整的广告区域是 37 字节减去 MAC 的 6 个强制字节。 That includes everything: length, tag, and value (LTV) for each thing you send.这包括所有内容:您发送的每件东西的长度、标签和价值 (LTV)。

So first, there's going to be flags, which is three bytes (length, tag, value).所以首先,会有标志,它是三个字节(长度、标签、值)。 So now we're at the documented 28 bytes.所以现在我们处于记录的 28 个字节。

So you want to send some service UUDs.所以你想发送一些服务 UUD。 I'm assuming that serviceCBUUID is a 128-bit ad hoc UIUD rather than a 16-bit assigned UUID.我假设 serviceCBUUID 是一个 128 位的临时 UIUD,而不是一个 16 位分配的 UUID。 So that's a 2-byte header (length, Complete list of 128-bit Service UUIDs), plus 16 bytes of data.所以这是一个 2 字节的 header(长度,128 位服务 UUID 的完整列表),加上 16 字节的数据。

Now we're at 10 bytes.现在我们有 10 个字节。

Your name won't fit in 10 bytes, so I assume it's sending the "Shortened local name" tag, along with the 8 bytes that will fit (including one length byte).你的名字不适合 10 个字节,所以我假设它正在发送“缩短的本地名称”标签,以及适合的 8 个字节(包括一个长度字节)。

If you want to send a longer name, you'll need to shorten the other things in your payload.如果要发送更长的名称,则需要缩短有效负载中的其他内容。

In your case, you probably want to just remove CBAdvertisementDataServiceUUIDsKey.在您的情况下,您可能只想删除 CBAdvertisementDataServiceUUIDsKey。 Apple will generally manage that for you if there's room.如果有空间,Apple 通常会为您管理。 If there isn't room, then Apple has a clever, proprietary trick of hashing that data into the overflow area.如果没有空间,Apple 有一个聪明的专有技巧,将数据散列到溢出区域。 As long as you're only going to be scanned by other Apple devices, this works really well and you can get away without the cost of advertising your full service UUIDs.只要您只会被其他 Apple 设备扫描,这非常有效,您无需为您的完整服务 UUID 做广告就可以逃脱。 It doesn't work if you need to be scanned by things other than Apple devices, though.但是,如果您需要被 Apple 设备以外的东西扫描,它就不起作用了。

If you do need to advertise your service UUIDs, and you also want more room for your local name, then you'll want to buy an assigned UUID .如果您确实需要宣传您的服务 UUID,并且您还希望为您的本地名称提供更多空间,那么您将需要购买分配的 UUID Those cost US$2,500, and are 16 bits rather than 128 bits, which will get you back 14 bytes for your local name.这些成本为 2,500 美元,是 16 位而不是 128 位,这将为您的本地名称返回 14 个字节。

Regarding your question about "10 different uuids each with 8 bytes," I'm not sure where you're getting those numbers.关于您关于“10 个不同的 uuid,每个 8 个字节”的问题,我不确定您从哪里得到这些数字。 BLE UUIDs are either 16 bytes (128-bit) or 2 (16-bit). BLE UUID 为 16 字节(128 位)或 2(16 位)。 But yes, if you tried to advertise 10 16-byte UUIDs that will overflow.但是,是的,如果您尝试发布 10 个 16 字节 UUID 会溢出。 iOS will likely advertise as many as will fit, and then hash the rest into the overflow area (described above). iOS 可能会发布尽可能多的广告,然后 hash 将 rest 放入溢出区域(如上所述)。 The full list of service UUIDs will still be available upon connection, even if they're not advertised.服务 UUID 的完整列表在连接时仍然可用,即使它们没有被广告。

If you had 10 16-bit UUIDs, then that would take 22 bytes, which would comfortably fit in the advertising packet (without a local name).如果您有 10 个 16 位 UUID,那么这将占用 22 个字节,这将很适合广告数据包(没有本地名称)。 To get more than two 16-bit UUIDs you would need to be at least an Associate Member of the Bluetooth SIG.要获得两个以上的 16 位 UUID,您至少需要成为蓝牙 SIG 的准成员 That costs US$7,500 or US$35,000 per year, depending on the size of your company.每年的费用为 7,500 美元或 35,000 美元,具体取决于您公司的规模。 Adopters (the free tier) can only register two of them at $2,500/each.采用者(免费层)只能以每人 2,500 美元的价格注册其中两个。

Now, if this is only for fairly new iPhones (8+, I believe) running fairly new versions of iOS (12+, I believe), then there's extended advertising, which allows up to 124 bytes of payload.现在,如果这仅适用于运行相当新版本的 iOS(我相信是 12+)的相当新的 iPhone(我相信是 8+),那么就有扩展广告,它允许高达 124 字节的有效负载。 See "What's New in Core Bluetooth" from WWDC 2019 for details.有关详细信息,请参阅 WWDC 2019 的“Core Bluetooth 中的新增功能” The scanning device will also need to support extended scanning for this to work.扫描设备还需要支持扩展扫描才能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM