BLE广告数据大小限制 [英] BLE Advertise data size limit

查看:76
本文介绍了BLE广告数据大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Addroid应用程序上,当我尝试开始ble广告时,我正在尝试添加一些额外的数据,而正如我所阅读的,广告数据必须< = 31字节.

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.

这就是我的方法:

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

通过这种方式,一切正常.现在,如果testData字段变为

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.

如果单个字符占用2个字节,为什么我有11个字符的字符串,我超过了30个字节的限制?

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

推荐答案

首先会自动添加三个标志字节.

Three flag bytes are automatically added first.

服务数据包包含第一个字节,该字节告诉它有多长时间,其后是一个字节数据包标识符,表示该服务数据包来自此.然后,您将获得服务uuid​​的有效负载(16个字节),后跟10个字节的UTF-8编码字符串.

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.

总和为31个字节.如果添加"k",则将获得32个字节,因此数据变得太长.

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

这篇关于BLE广告数据大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆