BLE Advertise data size limit

百般思念 提交于 2021-01-29 07:42:54

问题


On My Addroid app I'm trying to add some extra data when I try to start ble advertising, and as I've read, advertise data must be <= 31 bytes.

That's how I do it:

var testData = "abcdefghij"
var data = AdvertiseData.Builder().apply {
               addServiceData(
                   ParcelUuid(MY_UUID),
                   testData.toByteArray(Charsets.UTF_8)
               )
           }
bleAdvertiser.startAdvertising(
    settings.build(),
    data.build(),
    advertiseCallback
)

In this way everything works well. Now if testData fields become

var testData = "abcdefghijk"

advertising starts to fail due to exceeded advertise data size limit.

If a single char occupies 2 bytes, why if I had a string of 11 characters did I exceed the limit of 30 bytes?


回答1:


Three flag bytes are automatically added first.

A service data packet contains first one byte that tells how long it is, followed by a one byte packet identifier that says that here comes a service data packet. Then you have the payload of the service uuid (16 bytes) followed by your UTF-8-encoded string of 10 bytes.

That sums up to 31 bytes. If you add a 'k' you get 32 bytes and hence the data becomes too long.



来源:https://stackoverflow.com/questions/57756834/ble-advertise-data-size-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!