这是我拥有的代码:
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 返回真。相同的代码,我用于更改 NAME 特征。哪个会改变。但是 UUID 保持不变。我究竟做错了什么?PS:我正在使用这种类型的信标。
我尝试使用 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
I ask for the value again: [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 个不同的字节数组?我还注意到第一个有很多-,这个没有,为什么?