2

I'm writing an Android app which need to communicate with a Bluetooth LE device that has characteristics I need to subscribe to whose UUIDs are all formatted like 0xA009, 0xA011, etc.

There is a UUID.fromString method, but I haven't found anything that accepts that UUID format. What do I need to do in java to turn those into instances of java.util.UUID?

I just need to encode 0xA009 as java.util.UUID so that I can subscribe to Bluetooth LE characteristics with that and other similarly formatted UUIDs.

If it helps at all, I'm using RxAndroidBle to handle bluetooth communications.

KG6ZVP
  • 3,610
  • 4
  • 26
  • 45

2 Answers2

1

A UUID is 16 bytes and the constructor let's you build that with two longs. It's not clear from your question whether 0xA009, 0xA011, etc are multiple 2 byte chunks of a single UUID or whether they are separate UUIDs with assumed leading zeros or something like that.

So you'll have to do some pre-processing to strip off the 0x, possibly pad with zeros, and get the hex bytes together at least 8 together in a row, so you can parse them into longs. If you had all 16 hex bytes together, you could do something like this:

import java.util.UUID;

public class UUIDTest {
    public static void main(String[] args) {
        String uuidString = "0123456789ABCEF00FECBA9876543210";
        long mostSig = Long.parseLong(uuidString.substring(0, 16), 16);
        long leastSig = Long.parseLong(uuidString.substring(16, 32), 16);
        UUID uuid = new UUID(mostSig, leastSig);
        System.out.println(uuid);
    }
}
J. Lenthe
  • 1,330
  • 10
  • 10
0

0x is hex format. Try converting from hex to string and then use UUID.fromString. Good luck!

user2656851
  • 1,643
  • 4
  • 12
  • 15
  • So there's no direct way? I haven't found anything good to go from hex to string, just string to hex (which I don't care about). Do you have a direction you could point me in? – KG6ZVP Aug 22 '16 at 20:02
  • How does unicode to string help? I still need hex to string or am I missing something? I've never dealt with these formats outside of C where I can simply declare uint a = 0xA009 – KG6ZVP Aug 22 '16 at 20:11
  • Show me the output you get and how you get and i'll write you a method to convert it – user2656851 Aug 22 '16 at 20:16
  • This is all I need to be able to do: java.util.UUID aUUID = 0xA009 I'm sorry I can't give a little more detail. I am developing proprietary software and cannot disclose detail. Thank you for your help. – KG6ZVP Aug 22 '16 at 20:21
  • This can not be all the uuid. [look here](http://www.hexadecimaldictionary.com/hexadecimal/0xA009/). I need to know what type is it in memory to know how to convert it. – user2656851 Aug 22 '16 at 20:34
  • It's 16-bit. This question looks kind of similar, but I think doesn't give enough base knowledge for me to completely understand what's going on: http://stackoverflow.com/questions/4632524/how-to-find-the-uuid-of-serial-port-bluetooth-device/11622794#11622794 – KG6ZVP Aug 22 '16 at 20:38
  • Bassically you can do it like this: Integer i = 0xA009; i.toString(); output:40969 like [here](http://www.hexadecimaldictionary.com/hexadecimal/0xA009/) – user2656851 Aug 22 '16 at 20:38
  • And that will produce a valid Bluetooth characteristic UUID? – KG6ZVP Aug 22 '16 at 20:40
  • this will help you to build the string you need in order to use UUID.fromString – user2656851 Aug 22 '16 at 20:41
  • I receive an Invalid UUID exception: UUID.fromString(i.toString()) is apparently invalid. Am I missing something I should already know? – KG6ZVP Aug 22 '16 at 20:42
  • Obviously you are missing something. As I said it can not be your uuid. It is to short to be uuid – user2656851 Aug 22 '16 at 20:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121573/discussion-between-kg6zvp-and-user2656851). – KG6ZVP Aug 22 '16 at 20:50