【发布时间】:2016-01-22 13:03:40
【问题描述】:
我正在尝试开发基于 Eddystone 的应用程序。 我拿了谷歌示例代码并尝试修改它。 android 规范说服务数据的最大长度是 31 个字节
我尝试在以下代码中更改服务数据长度 构建服务数据()
这里最多只接受 20 个字节。 不止于此(例如 21 个字节)我得到了 ADVERTISE_FAILED_DATA_TOO_LARGE 错误
//错误
类:AdvertiseCallback
错误:ADVERTISE_FAILED_DATA_TOO_LARGE
由于要广播的广告数据大于31字节,无法启动广告。
我正在使用 UID 框架并在棒棒糖设备上进行测试。
请让我知道我做错了什么?
byte[] serviceData = null;
// 1+1+10+6+1+1+1 = 21 bytes
private byte[] buildServiceData() throws IOException {
byte txPower = txPowerLevelToByteValue();
byte[] namespaceBytes = toByteArray(namespace.getText().toString());
byte[] instanceBytes = toByteArray(instance.getText().toString());
ByteArrayOutputStream os = new ByteArrayOutputStream();
os.write(new byte[]{FRAME_TYPE_UID, txPower});
os.write(namespaceBytes);
os.write(instanceBytes);
//for testing only
// os.write(new byte[]{txPower});
// os.write(new byte[]{txPower});
// os.write(new byte[]{txPower});
return os.toByteArray();
}
//advertise the data
AdvertiseData advertiseData = new AdvertiseData.Builder()
.addServiceData(SERVICE_UUID, serviceData)
.addServiceUuid(SERVICE_UUID)
.setIncludeTxPowerLevel(false)
.setIncludeDeviceName(false)
.build();
namespace.setError(null);
instance.setError(null);
setEnabledViews(false, namespace, instance, rndNamespace, rndInstance, txPower, txMode);
adv.startAdvertising(advertiseSettings, advertiseData, advertiseCallback);
【问题讨论】:
-
在一部具有最低规格和 Android 8.1.0 (Oreo Go) 的手机中,我只能附加 .addServiceData(SERVICE_UUID, serviceData) 和 addServiceUuid(SERVICE_UUID) 中的一个(添加 .setIncludeDeviceName(真的) )。只有 .addServiceData(SERVICE_UUID, serviceData) 我可以发送 serviceData 最多 10 个字节而不会失败。我有另一部具有中等规格和 Android 7.1.2 (Nougat) 的手机,我可以在其中接收信号,但 serviceRecord.getServiceData() 返回一个空白地图。但是从 Nougat 中,我最多可以发送 24 个字节的数据。在 Oreo Go 中我可以检索数据,地图不为空
标签: java android maxlength bluetooth-lowenergy eddystone