Ble Android 广告:AddServiceData

Ble Android Advertising: AddServiceData

在我的 ble 项目中,我正在尝试添加一些服务数据来宣传这样的数据

 var data = AdvertiseData.Builder().apply {
            setIncludeDeviceName(true)
            addServiceData(
                ParcelUuid(BleConsts.SERVICE_UUID),
                myData.toByteArray(Charsets.UTF_8)
            )
        }

  bleAdvertiser.startAdvertising(
      settings.build(), 
      data.build(), 
      advertiseCallback
  )

但是 onStartFailure 回调我的 A​​dvertiseCallback 执行被错误代码 1 触发。 如果我从 data 中删除 addServiceData 调用,做广告开始没有任何问题。 怎么了?

错误代码 1 对应常量 ADVERTISE_FAILED_DATA_TOO_LARGE。您的数据不得超过 31 个字节:

Failed to start advertising as the advertise data to be broadcasted is larger than 31 bytes.

尝试减小您要添加的数据的大小。