How to create UUID from string in android

2019-02-02 20:13发布

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.

6条回答
不美不萌又怎样
2楼-- · 2019-02-02 20:35

Hope this will help
The exception is due to invalid argument in UUID.fromString() method.

The UUID.fromString() method expect "302a5a70-c085-4946-b702-fc1deb1046af" type of string as its argument and returns instance of UUID class.
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 case use 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

查看更多
叛逆
3楼-- · 2019-02-02 20:43

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)

查看更多
看我几分像从前
4楼-- · 2019-02-02 20:48

Using the class UUID

An example like this:

 UUID.randomUUID().toString()
查看更多
forever°为你锁心
5楼-- · 2019-02-02 20:51

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".

查看更多
姐就是有狂的资本
6楼-- · 2019-02-02 20:57

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.

查看更多
Explosion°爆炸
7楼-- · 2019-02-02 20:58

you can use

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

System.out.print(uuid.toString());
查看更多
登录 后发表回答