无法更改信标的UUID特性。相同的代码更改了信标的名称和其他特征

时间:2018-02-19 11:21:30

标签: android bluetooth-lowenergy uuid beacon characteristics

这是我的代码:

try {
                            String uuid = "116eadaf568c472a9ee799c6fb7dab9c";
                            Log.i(TAG, "BGC value NEW UUID: " + uuid);
                            boolean change = characteristic.setValue(URLEncoder.encode(uuid, "utf-8"));
                            Log.i(TAG, "BGC value after: " + characteristic.getValue() + " / has changed: " + change);
                            Log.i(TAG, "BGC value after: " + toHexadecimal(characteristic.getValue()) + " / has changed: " + change);
                            boolean statusWrite = gatt.writeCharacteristic(characteristic);
                            Log.i(TAG, "BGC value after statusWRITE: " + statusWrite);
                        } catch (Exception e) {
                            Log.e("", "BGC error is: " + e.getMessage());
                        }

StatusWrite返回true。相同的代码,我用于更改NAME特性。哪个改变了。但是UUID仍然是一样的。我究竟做错了什么? PS:我正在使用这种beacon

我尝试使用LightBlue应用程序,我确实设法更改UUID,我检查了,特征是“可写”

这是我认为出了问题的地方:

  02-19 12:26:30.727: I/BEACON(24089): BGC value before before HEXA: [B@e20538b
  02-19 12:26:31.381: I/BEACON(24089): BGC value before toHexa: 116eadaf568c472a9ee799c6fb7dab9c
  02-19 12:26:43.925: I/BEACON(24089): BGC value NEW UUID: 916eadaf568c472a9ee799c6fb7dab9c
  02-19 12:26:44.987: I/BEACON(24089): BGC value after: [B@57b4ac / has changed: true
  02-19 12:26:54.826: I/BEACON(24089): BGC value after: 3931366561646166353638633437326139656537393963366662376461623963 / has changed: true

因此,您可以看到我在更改之前获取了值,并且[B@e20538b转换为:116eadaf568c472a9ee799c6fb7dab9c,这是当前的UUID。在此之后,我将其更改为916eadaf568c472a9ee799c6fb7dab9c 我再次要求价值:[B@57b4ac。但如果我做同样的toHexadecimal(value)而不是得到:“916eadaf568c472a9ee799c6fb7dab9c”我得到“3931366561646166353638633437326139656537393963366662376461623963”。为什么不一致?

这是“toHexadecimal”:

  private static String toHexadecimal(byte[] digest) {
    String hash = "";
    for (byte aux : digest) {
        int b = aux & 0xff;
        if (Integer.toHexString(b).length() == 1) hash += "0";
        hash += Integer.toHexString(b);
    }
    return hash;
}

编辑:

我在更改值之前添加了之后:

           Log.i(TAG, "BGC value before before HEXA new String: " + new String(characteristic.getValue(), "UTF-8"));
           boolean change = characteristic.setValue(uuid.getBytes());
           Log.i(TAG, "BGC value after new String: " + new String(characteristic.getValue(), "UTF-8") + " / has changed: " + change);

然后它返回:

02-19 14:12:41.140: I/BEACON(12090): BGC value before before HEXA new String: n��V�G*����}��
02-19 14:12:41.141: I/BEACON(12090): BGC value after new String: 1111 / has changed: true

所以我觉得在设置值的过程中出现了明显的错误,我是否需要先将变换为十六进制或其他内容?我不明白

我还记录了一系列的咬伤。初始值为:

02-19 14:21:46.059: I/BEACON(21134): BGC value before before HEXA byte array: [17, 110, -83, -81, 86, -116, 71, 42, -98, -25, -103, -58, -5, 125, -85, -100]

这里我将String UUID更改为已设置的相同值。如果我创建字节数组,它看起来像这样:

02-19 14:21:46.060: I/BEACON(21134): BGC value after byte array: [49, 49, 54, 101, 97, 100, 97, 102, 53, 54, 56, 99, 52, 55, 50, 97, 57, 101, 101, 55, 57, 57, 99, 54, 102, 98, 55, 100, 97, 98, 57, 99] / has changed: true

完全相同的字符串如何有2个不同的字节数组?另外我注意到第一个有很多 - ,这个没有,为什么?

1 个答案:

答案 0 :(得分:1)

我需要调用它来使byteArray,String.getBytes不起作用:

 public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

非常感谢@PeterBruins帮助我获得答案