简体   繁体   中英

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? While doing some testing I used following code for BLE advertising:

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. 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" . Can someone explain why in my case it is only 8 bytes then? Is it in practice always 8 bytes for this CBAdvertisementDataLocalNameKey on iOS or can this number change? How can I calculate number of bytes that will be successfully send in advertising packet (in my case 8 bytes)? Is there any way to advertise more than 8 bytes when using BLE on iOS? 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?

As with most BLE things, it depends. 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). The full advertising area is 37 bytes minus 6 mandatory bytes for the MAC. That includes everything: length, tag, and value (LTV) for each thing you send.

So first, there's going to be flags, which is three bytes (length, tag, value). So now we're at the documented 28 bytes.

So you want to send some service UUDs. I'm assuming that serviceCBUUID is a 128-bit ad hoc UIUD rather than a 16-bit assigned UUID. So that's a 2-byte header (length, Complete list of 128-bit Service UUIDs), plus 16 bytes of data.

Now we're at 10 bytes.

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).

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. Apple will generally manage that for you if there's room. If there isn't room, then Apple has a clever, proprietary trick of hashing that data into the overflow area. 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. It doesn't work if you need to be scanned by things other than Apple devices, though.

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 . Those cost US$2,500, and are 16 bits rather than 128 bits, which will get you back 14 bytes for your local name.

Regarding your question about "10 different uuids each with 8 bytes," I'm not sure where you're getting those numbers. BLE UUIDs are either 16 bytes (128-bit) or 2 (16-bit). But yes, if you tried to advertise 10 16-byte UUIDs that will overflow. iOS will likely advertise as many as will fit, and then hash the rest into the overflow area (described above). The full list of service UUIDs will still be available upon connection, even if they're not advertised.

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). To get more than two 16-bit UUIDs you would need to be at least an Associate Member of the Bluetooth SIG. That costs US$7,500 or US$35,000 per year, depending on the size of your company. Adopters (the free tier) can only register two of them at $2,500/each.

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. See "What's New in Core Bluetooth" from WWDC 2019 for details. The scanning device will also need to support extended scanning for this to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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