我正在尝试开发基于Eddystone的应用。 我拿了谷歌示例代码并尝试修改它。 android规范称服务数据的最大长度为31个字节
我尝试在以下代码中更改服务数据长度 buildServiceData()
此处最大值仅接受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);
答案 0 :(得分:0)
我怀疑serviceData
格式不正确,因此其长度变得太大。很难确切地说出原因,因为并未显示所有代码。
我建议您检查serviceData的长度,看看格式错误是否导致它太长。将字节数组打印为十六进制字符串并将其粘贴到您的问题中可能也会有所帮助,如果不明显是什么导致它格式错误。
您可以使用以下代码打印serviceData:
// Put this line near the "advertise the data" comment
Log.d(TAG, "Service data bytes: "+byteArayToHexString(serviceData));
public static String byteArrayToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sb.append(String.format("%02x", bytes[i]));
}
return sb.toString();
}