问题
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