Eddystone-url 设置或禁用扩展

Eddystone-url set or disable expansion

我正在为 android 编写 Eddystone-url BLE 广告商。但是我在正确编码 url 时遇到问题。根据规范 (https://github.com/google/eddystone/tree/master/eddystone-url), the scheme prefix (e.g. 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"

检索到的 url 看起来又像原来的了。

如何使用扩展编码?或者我怎样才能禁用它?好像很不靠谱

我的对应代码:

 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 中的字节显示: 对于“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 字节 到字节。这会导致您看到的症状。

如果您post定义此函数并将实际字节的输出记录到输出流中,这可能有助于诊断问题。

编辑: 或者,您用于解码传输的 BLE 扫描仪应用程序可能未正确解压缩。如果您有 Android 设备,您可以试试我的 Locate Beacon 应用程序:https://play.google.com/store/apps/details?id=com.radiusnetworks.locate&hl=en