我正在为Android编写Eddystone-url BLE广告客户。但是我正确编码网址有问题。根据规范(https://github.com/google/eddystone/tree/master/eddystone-url),可以用单个字节(例如0x00)设置方案前缀(例如http://www。)。应该在扩展时发生同样的事情(例如.com / = 0x00)。有不同前缀和扩展的完整列表。
虽然前缀有效但扩展行为却很奇怪。
如果我输入:
url = "http://www.orf.at/"
BLE扫描程序正好检索此网址。
如果我输入:
url = "http://www.orf.at"
没有" /"最后,BLE扫描仪告诉我一个" .com"添加如下:
"http://www.orf.at.com"
如果我输入:
url = "http://www.orf.at/test"
检索到的网址再次与原始网址相同。
如何使用扩展编码?或者我该如何禁用它?这似乎非常不可靠。
我的相应代码:
private AdvertiseData buildEddystoneURLData(String url) throws UnsupportedEncodingException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
//frame type
os.write((byte) 0x10);
//TX Power
os.write((byte) 0xB5);
//URL scheme code
/*
0 0x00 http://www.
1 0x01 https://www.
2 0x02 http://
3 0x03 https://
*/
if(url.startsWith("http://www.")){
os.write((byte) 0x00);
url = url.substring(11,url.length());
log("starts with http://www.");
log("cutting to: "+url);
}else if(url.startsWith("https://www.")){
os.write((byte) 0x01);
url = url.substring(12,url.length());
log("starts with https://www.");
log("cutting to: "+url);
}
else if(url.startsWith("http://")){
os.write((byte) 0x02);
url = url.substring(7,url.length());
log("starts with http://");
log("cutting to: "+url);
}
else if(url.startsWith("https://")){
os.write((byte) 0x03);
url = url.substring(8,url.length());
log("starts with https://");
log("cutting to: "+url);
}
byte [] advertiseBytes = string2byte(url);
//0-17: url
for (Byte bt: advertiseBytes) {
os.write(bt);
}
byte[] serviceData = os.toByteArray();
ParcelUuid SERVICE_UUID = ParcelUuid.fromString("0000FEAA-0000-1000-8000-00805F9B34FB");
AdvertiseData.Builder builder = new AdvertiseData.Builder();
if(serviceData!=null) {
builder.addServiceData(SERVICE_UUID, serviceData)
.addServiceUuid(SERVICE_UUID)
.setIncludeTxPowerLevel(false)
.setIncludeDeviceName(false); //don't include device name - it does not work
return builder.build();
}
else
return null;
}
更新 string2byte:
private byte[] string2byte(String st) throws UnsupportedEncodingException {
return st.getBytes("UTF-8");
}
在logcat中打印字节显示: for" http://www.orf.at/":
starts with http://www.
cutting to: orf.at/
getServiceData at: 0000feaa-0000-1000-8000-00805f9b34fb
byte: 16
byte: -75
byte: 0
byte: 111
byte: 114
byte: 102
byte: 46
byte: 97
byte: 116
byte: 47
getServiceData as String: ���orf.at/
这适用于:" http://www.orf.at":
starts with http://www.
cutting to: orf.at
getServiceData at: 0000feaa-0000-1000-8000-00805f9b34fb
byte: 16
byte: -75
byte: 0
byte: 111
byte: 114
byte: 102
byte: 46
byte: 97
byte: 116
getServiceData as String: ���orf.at
答案 0 :(得分:0)
您无法停用扩展程序 - 它是规范的一部分。接收器将扩展与扩展模式匹配的任何字节。所以你需要确保传输的任何字节都不会无意中包含扩展字节。
我怀疑string2byte
函数中存在一个错误,即在将其转换为字节时,会在“http://www.orf.at”末尾添加额外的0字节。这会导致你看到的症状。
如果您发布此函数的定义以及放入输出流的实际字节的记录输出,这可能有助于诊断问题。
编辑:或者,您用于解码传输的BLE扫描仪应用程序可能无法正确解压缩。如果您使用的是Android设备,则可以尝试使用我的定位信标应用:https://play.google.com/store/apps/details?id=com.radiusnetworks.locate&hl=en