48

In my app, I scan low energy Bluetooth for specific service uuid 2415. To convert the string 2415 into uuid I am using UUID serviceUUID = UUID.fromString("2415"); but at this line exception arises IllegalArgumentException: Invalid UUID 2415.

Please help me in this respect I would b very thankful to in this regard. Thanks in advance.

user3056760
  • 481
  • 1
  • 4
  • 3

6 Answers6

35

Using the class UUID

An example like this:

 UUID.randomUUID().toString()
Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
  • 33
    I don't understand how this answer should be helpful. This gives you a random UUID which is not related to the problem of the OP. – EarlGrey Nov 22 '16 at 08:07
  • 5
    I don't understand why people upvote this, yes it links to the correct class but it is not relevant to the question why in other platforms we can use CBUUID "140D" for example and in Android there is no baked in support for this. – automaticoo Dec 05 '18 at 14:43
  • This answer is irrelevant to the question asked – Sergio Flores Apr 17 '21 at 19:33
22

The accepted answer was provided in a comment by @Michael:

Have you tried combining your short UUID with the Bluetooth base UUID? I.e. "00002415-0000-1000-8000-00805F9B34FB"? (assuming that you meant 2415 hexadecimal)?

I'm converting that comment to an answer because I missed it first time I read through this thread.

Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
17

you can use

String str = "1234";
UUID uuid = UUID.nameUUIDFromBytes(str.getBytes());

System.out.print(uuid.toString());
Özer Özcan
  • 1,253
  • 16
  • 17
  • 7
    Although this might solve the problem, it is always good to add an explanation why/how this works. – BDL Nov 22 '16 at 09:54
13

The confusion that may lead many people here is that you can use short code UUIDs to reference bluetooth services and characteristics on other platforms - for instance on iOS with CBUUID. On Android however, you must provide a full, 128-bit length UUID as specified in RFC4122.

The fix (as @Michael pointed out) is to prepend your 16bit or 32bit short UUID to the base bluetooth UUID. You can use these functions to make this a bit easier.

public static final String baseBluetoothUuidPostfix = "0000-1000-8000-00805F9B34FB";

public static UUID uuidFromShortCode16(String shortCode16) {
    return UUID.fromString("0000" + shortCode16 + "-" + baseBluetoothUuidPostfix);
}

public static UUID uuidFromShortCode32(String shortCode32) {
    return UUID.fromString(shortCode32 + "-" + baseBluetoothUuidPostfix);
}

For example:

UUID uuid = uuidFromShortCode16("FFF0");

This creates a UUID object from "0000FFF0-0000-1000-8000-00805F9B34FB".

92tonywills
  • 849
  • 9
  • 13
10

Hope this will help
To Convert short hand 16-bit uuid to 128 bit uuid you can use this template "0000XXXX-0000-1000-8000-00805F9B34FB". here replace XXXX with your 16 bit uuid.

For example:
In your use case 128 bit UUID will be "00002415-0000-1000-8000-00805F9B34FB".
and to get UUID from string you should use code like this

UUID uuid = UUID.fromString("00002415-0000-1000-8000-00805F9B34FB");
https://newcircle.com/s/post/1786/2016/01/04/bluetooth-uuids-and-interoperable-advertisements

Akhilesh Kumar
  • 796
  • 1
  • 5
  • 12
0

I have a feeling that your String "2415" might just have been straight-up converted from a long, because, as the others point out, "2415" is not close to resembling a UUID. If that is the case, then you should use the UUID constructor which takes two longs:

uuid = new UUID(long mostSignificant, long leastSignificant)

where you can retrieve those long values via

uuid.getMostSignificantBits() uuid.getLeastSignificantBits()

So in your case, you might do something like uuid = new UUID(2415,2415)

bremen_matt
  • 6,902
  • 7
  • 42
  • 90