我正在为Android编写一个Eddystone-url BLE广告商。但是我在正确编码 url 时遇到问题。根据规范(https://github.com/google/eddystone/tree/master/eddystone-url),方案前缀(例如 http://www.)可以用单个字节(例如0x00)设置。在扩展时也应该发生同样的情况(例如.com/= 0x00)。有一个针对不同前缀和扩展的完整列表。
当前缀工作时,扩展行为异常。
如果我输入:
url = "http://www.orf.at/"
BLE扫描仪精确地检索此URL。
如果我输入:
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;
}
更新字符串2字节:
private byte[] string2byte(String st) throws UnsupportedEncodingException {
return st.getBytes("UTF-8");
}
在日志猫中打印字节显示:对于"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
您不能禁用扩展 - 它是规范的一部分。 接收器将扩展与扩展模式匹配的任何字节。 因此,您需要确保传输的任何字节都不会无意中包含扩展字节。
我怀疑 string2byte
函数中存在一个错误,即在将其转换为字节时在"http://www.orf.at"末尾添加一个额外的 0 字节。 这会导致您看到的症状。
如果发布此函数的定义以及放入输出流的实际字节的记录输出,则可能有助于诊断问题。
编辑:或者,您用于解码传输的BLE扫描仪应用程序可能无法正确解压缩。 如果你有安卓设备, 你可以试试我的定位信标应用程序: https://play.google.com/store/apps/details?id=com.radiusnetworks.locate&hl=en