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.
Using the class UUID
An example like this:
UUID.randomUUID().toString()
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.
you can use
String str = "1234";
UUID uuid = UUID.nameUUIDFromBytes(str.getBytes());
System.out.print(uuid.toString());
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".
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
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)
Related
I am using the ArduinoBLE library to create a service and characteristic:
BLEService angleService("1826");
BLEFloatCharacteristic pitchBLE("2A57", BLERead | BLENotify);
I add the services and characteristics and advertise my device:
BLE.setLocalName("acsAssist");
BLE.setAdvertisedService(angleService);
angleService.addCharacteristic(pitchBLE);
BLE.addService(angleService);
pitchBLE.writeValue(0);
BLE.advertise();
I perform some calculations and then write my calculated value to the service:
pitchBLE.writeValue(posiPitch);
posiPitch is a float value, like 1.96 for example. it can go from -90.00 to 90.00
I try to read this value from my Android app:
(characteristic.getFloatValue(BluetoothGattCharacteristic.FORMAT_SFLOAT,0))
I get crazy numbers such as -1.10300006E9.
How can I read my float value so that my android app value matches that of the arduino value?
I ran into a similar issue. Instead of using getFloatValue(), I used getValue() which returns a byte array.
The reason for the weird numbers is that the byte order in which arduino stores data is different from that of java.
To change the order use byteBuffer:
byte[] b = characteristic.getValue();
float f = ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getFloat();
This post helped me: How to convert 4 bytes array to float in java
I have simplified the situation drastically by utilizing a string characteristic instead.
Although this is probably more resource intensive, it reduces the headache of having to parse through a byte array to convert the data into what I want.
My string characteristic is made with a custom UUID (to avoid Bluetooth GATT standard conflicts):
BLEStringCharacteristic pitchBLE("78c5307a-6715-4040-bd50-d64db33e2e9e", BLERead | BLENotify, 20);
After I advertise and perform calculations as shown in my original post, I simply write to my characteristic and pass in the value as a string:
pitchBLE.writeValue(String(posiPitch));
In my Android application, I simply get the characteristic's string value:
characteristic.getStringValue(0)
I hope this will help future developers like me who are struggling to find clearer resources on information such as this :)
How to generate unique id in android.. Any help will be appreciated
Let's see, you'll need 10 digit alphanumeric, so that's around 60 bits?
UUID random = UUID.randomUUID();
long first64 = random.getLeastSignificantBits();
long last64 = random.getMostSignificantBits();
And there you have 128 random bits, which you can turn into an alpha-string.
Take a look at the UUID Class of Android Documentation.
You may find useful the following to use: UUIrandomUUID()
I'm a little confused about how to send data over a Bluetooth connection. In the Android API documentation, from the Bluetooth Chat example, the class BluetoothChat.java constructs a Handler object. Within there is a switch statement, and a MESSAGE_WRITE case. Do I need to implement similar code to send Strings over Bluetooth? A case statement for each String I want to send? In particular I want to send (name,value) pairs so I know what is sent and what it's value is. How do I implement this? If, following the example, I call BluetoothChatService.write(String.getBytes()) a bunch of times to send...? Then how would I know which strings are associated with which names? Please help.
I'm using Google's Protocol Buffers to send structured data over bluetooth connections in my Android app. protobuf takes care of figuring out how to serialize the message for you so that you only have to send a byte value (length of the message) and then the serialized message; the library takes care of unserializing the message on the other end and populating the fields of a custom object. Definitely take a look at it; it made the writing of a custom bluetooth socket protocol quite easy.
Serialize pairs to any of formats which allows byte representation. Such as XML or JSON. Or even your custom format, it wouldn't be difficult for pairs of strings. And then send it.
For simple pairs of strings (Such as names), you could simply use some character to define when the first string stops, and the next begins.
For example, I use a format such as this to send a set of 3 strings from one device to another:
String toSend = partOne + ":" + partTwo + ":" + partThree;
On the other device, to get the strings you sent, use the String.split() method like so:
String parts[] = received.split(":",3);
The 2nd parameter is a limit to how many times to split. In this example, there are 3 strings, so split 3 times max.
The downside to doing this is that you need to use characters that will never be in all but the last string.
In my application, I used this method to send data about text messages, and the first 2 parts are the phone number and timestamp, so there can never be a : in it. For names, a newline would probably work.
If your going to send more complex data, definitely use something like Protocol Buffers.
So, I thought I was being clever and using various hashes and permutations of Android's secure unique ID to identify my users....
But it turns out that 9774d56d682e549c is a magic ID returned by
Secure.getString(getContentResolver(), Secure.ANDROID_ID);
for a good number of devices... It appears every emulator I build has the same ID, and many of other peoples phones (lots of moto droids!) and flashed OS mods tend to return this same repeating value. Non-MotoDroid / Non-Flashed handsets seem to all give me a unique string back. But this one is in my DB about 60 times!
I'm going to be optimizing my app to check for that string before registering, but what would be a recommended way of handling it to get another unique value?
My current thought is to check for it, generate an EXTREMELY LARGE random value, hash it, then store than in SharedPreferences and then either use the ANDROID_ID or the one stored in sharedprefs (if the users phone is giving the value). Anyone have any better ideas, or does this seem solid enough to mitigate this crazy bug?
Take a look at the Identifying app installations article. You can't rely on ANDROID_ID.
The best solution is to generate a custom id with:
String id = UUID.randomUUID().toString();
If you want to create one with the same format as real ANDROID_IDs, you can use the same method they use here:
private static String generateAndroidId() {
String generated = null;
try {
final SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed( (System.nanoTime() + new SecureRandom().nextLong()).getBytes() );
generated = Long.toHexString(random.nextLong());
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "Unexpected exception", e);
}
return generated;
}
Outputs: 9e7859438099538e
Though not ideal, things like the Google AdMob SDK use the permission android.permission.READ_PHONE_STATE to read the device's phone number, etc.
There's some useful, related information in the following blog post: http://strazzere.com/blog/?p=116
This phenomenon and also this Stackoverflow thread were talked about at the summercon 2012 by Oberheide and Miller, who recently dissected Google's Bouncer: http://jon.oberheide.org/files/summercon12-bouncer.pdf
Maybe you can extract some more useful info for your project.
we are developing the application using the .Net webservice(soap protocal) for that i need Pass GUID from android class.
in .Net we have statement like below
Guid myGuid1 = new Guid();
i need the similar functionality in Android ,
is there any way to make this kind of functionality in android code?
Regards,
Jeyavel N
Yes you should use UUID like the following code:
String uniqueID = UUID.randomUUID().toString();
You can use the java.util.UUID class.
We can use
String uniqueID = UUID.randomUUID().toString();
An Instance ID or a GUID is scoped to the app that creates it, which prevents it from being used to track users across apps.For more information and significance please refer this link here
You will get UUID in Android,
UUID uuid = UUID.randomUUID();
String uuidInString = uuid.toString();