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

Cannot change UUID characteristic of a beacon. Same code changes the NAME of the beacon, and other 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 returns 正确。相同的代码,我用于更改 NAME 特征。哪个被改变了。但 UUID 保持不变。我究竟做错了什么? PS: 我用的是这种beacon.

我用 LightBlue 应用程序试过,我确实设法改变了 UUID,我检查了一下,特征是 "writable"

这是我认为有问题的地方:

  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);

它returns这个:

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]

在这里,我将字符串 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个不同的字节数组?我还注意到第一个有很多 -,这个不,为什么?

我需要调用它来生成 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 帮我找到答案