3

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 Answer 1

5

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.

1
  • thank you, i was missing default bytes added automatically.
    – giozh
    Sep 2, 2019 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.