How to create UUID from string in android

In my application, I scan low-energy Bluetooth for a specific uuid 2415 service. To convert string 2415 to uuid, I use UUID serviceUUID = UUID.fromString("2415"); , but an IllegalArgumentException is thrown on this line: Invalid UUID 2415.

Please help me in this regard, I would be very grateful in this regard. Thanks in advance.

+28
android uuid bluetooth-lowenergy
Dec 02 '13 at 9:44
source share
7 answers

Using the UUID Class

Example:

  UUID.randomUUID().toString() 
+39
Jun 03 '14 at 15:49
source share

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

Have you tried combining your short UUID with the base Bluetooth UUID? That is, "00002415-0000-1000-8000-00805F9B34FB"? (assuming you mean 2415 hexadecimal)?

I am converting this comment into a response because I missed it the first time I read this stream.

+13
Jun 25 '14 at 12:35
source share

you can use

 String str = "1234"; UUID uuid = UUID.nameUUIDFromBytes(str.getBytes()); System.out.print(uuid.toString()); 
+6
Nov 22 '16 at 8:01
source share

The confusion that can lead many people here is that you can use short-code UUIDs to refer to bluetooth services and features on other platforms - like iOS with CBUUID , for example . However, on Android, you must provide a full UUID of 128 bits, as specified in RFC4122 .

The fix (as @Michael pointed out) is to add a 16-bit or 32-bit short UUID to the base Bluetooth UUID . You can use these features to make it a little 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" .

+5
May 05 '16 at 15:02
source share

Hope this helps
The exception is due to an invalid argument in the UUID.fromString() method.

The UUID.fromString() method expects a "302a5a70-c085-4946-b702-fc1deb1046af" string type as an argument and returns an instance of the UUID class.
To convert a 16-bit combination with a 16-bit uuid to a 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 case, the 128-bit UUID will be "00002415-0000-1000-8000-00805F9B34FB" .
and in order to get the UUID from the 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

+2
Jan 18 '17 at 12:10
source share

I have the feeling that your String "2415" might just be converted from long because, as others point out, "2415" is not close to the UUID. If so, then you should use the UUID constructor, which spans two long lines:

uuid = new UUID(long mostSignificant, long leastSignificant)

where you can get these long values ​​with

uuid.getMostSignificantBits() uuid.getLeastSignificantBits()

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

+1
Nov 01 '16 at 14:13
source share

Go to a website like this and get a valid UUID string.

-3
Dec 02 '13 at 9:48 on
source share



All Articles