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.
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 ofUUID
class.To Convert short hand 16-bit uuid to 128 bit uuid you can use this template
"0000XXXX-0000-1000-8000-00805F9B34FB"
. here replaceXXXX
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
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)
Using the class UUID
An example like this:
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.
For example:
This creates a UUID object from
"0000FFF0-0000-1000-8000-00805F9B34FB"
.The accepted answer was provided in a comment by @Michael:
I'm converting that comment to an answer because I missed it first time I read through this thread.
you can use