如何从android中的字符串创建UUID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20325105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:40:40  来源:igfitidea点击:

How to create UUID from string in android

androiduuidbluetooth-lowenergy

提问by user3056760

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.

在我的应用程序中,我扫描低功耗蓝牙以获取特定服务 uuid 2415。要将字符串 2415 转换为我正在使用的 uuid,UUID serviceUUID = UUID.fromString("2415");但在这一行出现异常 IllegalArgumentException: Invalid UUID 2415。

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

请在这方面帮助我,我将在这方面非常感谢。提前致谢。

回答by Alécio Carvalho

Using the class UUID

使用类UUID

An example like this:

像这样的例子:

 UUID.randomUUID().toString()

回答by Richard Le Mesurier

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

接受的答案在@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)?

您是否尝试过将短 UUID 与蓝牙基础 UUID 结合使用?即“00002415-0000-1000-8000-00805F9B34FB”?(假设你的意思是 2415 十六进制)?

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

我正在将该评论转换为答案,因为我第一次阅读此线程时错过了它。

回答by ?zer ?zcan

you can use

您可以使用

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

System.out.print(uuid.toString());

回答by 92tonywills

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.

这里可能会导致很多人感到困惑的是,您可以使用短代码 UUID 来引用其他平台上的蓝牙服务和特性 - 例如在带有CBUUID 的iOS 上。但是,在 Android 上,您必须提供RFC4122 中指定的完整的 128 位长度的 UUID 。

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.

修复(如@Michael 指出的)是将您的 16 位或 32 位短 UUID 附加到基本蓝牙 UUID。您可以使用这些功能使这更容易一些。

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

这将创建一个 UUID 对象"0000FFF0-0000-1000-8000-00805F9B34FB"

回答by Akhilesh Kumar

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

To Convert short hand 16-bit uuid to 128 bit uuid you can use this template "0000XXXX-0000-1000-8000-00805F9B34FB". here replace XXXXwith 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

希望这会有所帮助
异常是由于UUID.fromString()方法中的无效参数。

UUID.fromString()方法期望"302a5a70-c085-4946-b702-fc1deb1046af"字符串类型作为其参数并返回UUID类的实例。

要将简写 16 位 uuid 转换为 128 位 uuid,您可以使用此模板"0000XXXX-0000-1000-8000-00805F9B34FB"。此处替换XXXX为您的 16 位 uuid。

例如:
在您的用例中,128 位 UUID 将是"00002415-0000-1000-8000-00805F9B34FB".
并从字符串中获取 UUID,您应该使用这样的代码

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

回答by bremen_matt

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:

我有一种感觉,您的字符串“2415”可能只是从 long 直接转换而来,因为正如其他人指出的那样,“2415”与 UUID 不太相似。如果是这种情况,那么您应该使用带有两个 long 的 UUID 构造函数:

uuid = new UUID(long mostSignificant, long leastSignificant)

uuid = new UUID(long mostSignificant, long leastSignificant)

where you can retrieve those long values via

您可以通过以下方式检索这些长值

uuid.getMostSignificantBits()uuid.getLeastSignificantBits()

uuid.getMostSignificantBits()uuid.getLeastSignificantBits()

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

所以在你的情况下,你可能会做类似的事情 uuid = new UUID(2415,2415)