How to create UUID from string in android

后端 未结 6 1210
Happy的楠姐
Happy的楠姐 2020-12-29 01:53

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(

相关标签:
6条回答
  • 2020-12-29 02:04

    you can use

    String str = "1234";
    UUID uuid = UUID.nameUUIDFromBytes(str.getBytes());
    
    System.out.print(uuid.toString());
    
    0 讨论(0)
  • 2020-12-29 02:08

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

    0 讨论(0)
  • 2020-12-29 02:10

    Using the class UUID

    An example like this:

     UUID.randomUUID().toString()
    
    0 讨论(0)
  • 2020-12-29 02:16

    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)

    0 讨论(0)
  • 2020-12-29 02:19

    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

    0 讨论(0)
  • 2020-12-29 02:20

    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.

    0 讨论(0)
提交回复
热议问题