androidbluetooth-lowenergyuuidbeaconcharacteristics

Cannot change UUID characteristic of a beacon. Same code changes the NAME of the beacon, and other characteristics


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?


Solution

  • 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