How to get GUID in android? - android

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();

Related

Can't set KeyConditionExpression parameter in DynamoDb for Android

I'm using the Amazon AWS DynamoDB for android, however, there's no way for me to set the KeyConditionExpression for a query.
See [http://docs.aws.amazon.com/AWSAndroidSDK/latest/javadoc/com/amazonaws/mobileconnectors/dynamodbv2/dynamodbmapper/DynamoDBQueryExpression.html][1]. The DynamoDBQueryExpression class is missing a withKeyConditionExpression() method.
I'm trying to make a query against dynamodb table but there's no way for me to set the key condition values.
Unfortunately keyConditionExpression No Longer exist for Java / Android. I wasted a significant amount of time because it still exist for Objective-C / iOS.
And since all AWS docs still refer to it, I thought I was in error. Once I have the new code written and working, I will document the replacement here.
The keyConditionExpression is set as follows:
AWSDynamoDBQueryExpression *queryExpression = [AWSDynamoDBQueryExpression new];
queryExpression.keyConditionExpression = #"#bookId = :bookId";
queryExpression.expressionAttributeNames = #{
#"#bookId" : #"bookId",
};
queryExpression.expressionAttributeValues = #{
#":bookId" : self.selectedBookId,
};
I encountered the similar problem for my Android Application where .withKeyConditionExpression() method was giving an error. Instead of that, I used:
TestTable object = new TestTable();
object.setHashKeyValue("12345"); //Set the value for HashKey
String queryString = "soverflow";
Condition rangeKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.BEGINS_WITH.toString())
.withAttributeValueList(new AttributeValue().withS(queryString.toString()));
DynamoDBQueryExpression newQueryExpression = new DynamoDBQueryExpression()
.withHashKeyValues(object)
.withRangeKeyCondition("AttributeName", rangeKeyCondition)
.withConsistentRead(false);
PaginatedQueryList<TestTable> result = mapper.query(TestTable.class, newQueryExpression);
The Point is that If you are Querying a table, the HashKey and the RangeKey will be the Partition Keys of the table and If you are Querying an Index, the Hash Key and the Range Key will be the partition keys of the Index.
Make sure to use the Annotations properly in the Table Class and to add Index's ARN to the Policy for authorization.

String to text field in android app

I have the following data scanned from a pdf417 and need to extract certain text to certain text fields (already created), not sure how to go about this... Data scanned with manatee works plugin and android app using android studio.
All help will be appreciated.
Data that was returned from scan -
%MVL1CC18%0154%4025M003%4025012RP01C%DC62XBGP%NISSAN%SILVER/SILWER%
Each part between the %'s need to go to a text field. I know that I need to make use of String substr=mysourcestring.substring(startIndex,endIndex); but this will work up to the first 2 % signs. How do I continue to the next few?
Thanks.
If you want to split string based on a delimiter, use the following
String delimitter="%";
String[] parts = inputString.split(delimitter);
Why not use String.split()?
In your case it would look something like this:
String[] extractedStrings = mysourcestring.split("%");
You can work on your string by using split method:
String yourString = "%MVL1CC18%0154%4025M003%4025012RP01C%DC62XBGP%NISSAN%SILVER/SILWER%";
String[] split = yourString.split("%");
In this way you will get an array where each item is a substring between two % chars.

How to create UUID from string in android

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)

Android - How are you dealing with 9774d56d682e549c ? Android ID

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.

Are string resource ID values guaranteed to be consistent over different projects?

I have some messages being passed back from my server through php. The problem is that the messages are in English and if the user is using another language they will still get the message in English.
So I had an idea that maybe instead of passing back the message I would instead pass the String resource Id from the android app, that way the app will get the correct string id for their language. I will use this in a number of apps so I just want to know if the string id is guaranteed to be the same across different android projects?
No.
The string resource IDs are likely not even guaranteed to be the same between re-compilations of the same application (e.g. differences between versions of aapt).
Christopher is right, but you can pass a parameter to your server like http://example.com/script.php?lang=en or lang=fr ....
This way the php scripts can use this parameter to return messages in the relevant locale.
You can get the resource ID from a string:
int resID = getResources().getIdentifier("org.my.package:strings/my_string", null, null);
// or
int resID = getResources().getIdentifier("my_string", "strings", "org.my.package");

Categories

Resources