0

This is the code that I have:

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 true. The same code, I use for changing the NAME characteristic. Which gets changed. But the UUID remains the same. What am I doing wrong? PS: I am using this type of beacon.

I tried with the LightBlue app, and I did manage to change the UUID, and I checked, the characteristic is "writable"

This is where I think something is wrong:

  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

So you can see that I take the value before changing and is [B@e20538b which translates to: 116eadaf568c472a9ee799c6fb7dab9c which is the UUID currently it has. After this, I change it to 916eadaf568c472a9ee799c6fb7dab9c I ask for the value again: [B@57b4ac. But if I do the same toHexadecimal(value) instead of getting : "916eadaf568c472a9ee799c6fb7dab9c" I get "3931366561646166353638633437326139656537393963366662376461623963". Why the inconsistency?

This is the "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;
}

EDIT:

I added before changing the value and after this:

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

And it returns this:

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

So I feel that something is clearly wrong on the way I set the value, do I need to transformig into a hex or something before? I do not understand

Also I logged the array of bites. and the initial value is:

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]

Here I changed the String UUID to the same value it's already set. And if I make the byte array, it looks like this:

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

How can the exact same string have 2 different byte arrays? Also I noticed the first one has a lot with -, this one no, why?

rosu alin
  • 5,674
  • 11
  • 69
  • 150
  • 1
    The UUID is usually the identifier of a characteristic, not the value. You didn't write the UUID directly to the characteristic, but a String representation (in UTF-8 format) of the UUID. This string is at least 32 bytes (32 characters) long, while a characteristic usually can hold no more than 20 bytes. Did you increase the MTU to allow more than 20 bytes? – Peter Bruins Feb 19 '18 at 11:43
  • no, what does that mean? – rosu alin Feb 19 '18 at 12:28
  • And I also tried changing to a small String like "11111" but still did not work. (thinking that maybe there is an issue with the string being to long). I am kind of lost, since it's my first time working with beacons. I'm looking into the MTU now – rosu alin Feb 19 '18 at 12:30
  • I did a `requestMTU(512)` but still the same. What I do not understand, is why first the toHexa returns me the correct value, and after the "3931....." very long number? – rosu alin Feb 19 '18 at 12:40
  • 1
    `characteristic.getValue()` returns a byte array, so when you try to print it, it calls the toString() of `object`, which outputs `[B@e20538b`. `[B` is the name of the class, and `e20538b`is the hashcode of the object: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString() – Peter Bruins Feb 19 '18 at 13:05
  • Check my latest Edit. @PeterBruins – rosu alin Feb 19 '18 at 13:15
  • 1
    You could try to convert your uuid-string to a byte array correctly: https://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java. Then you can use this byte array to update the characteristic. – Peter Bruins Feb 19 '18 at 13:32
  • thanks, trying the value from that link now – rosu alin Feb 19 '18 at 13:34

1 Answers1

1

I needed to call this to make the byteArray, String.getBytes did not work:

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

Thanks a lot to @PeterBruins for helping me get the answer

rosu alin
  • 5,674
  • 11
  • 69
  • 150