Get unique ANDROID_ID on flutter is it possible? - android

I would like to get the unique device id on android with Flutter.
I've tried this plugin device_info but it doesn't return the ANDROID_ID that I get in java Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
how can I get the same id?

In this case it might be simpler using platform specific code and a Flutter platform channel setup.
You can then use your Java code as included in your question and pass the Android ID to your Flutter application via a platform channel when needed.
See Writing custom platform-specific code with platform channels .

You can do what you did, but it is not reliable or guaranteed. Please read up on:
https://developer.android.com/training/articles/user-data-ids
for best handling of unique IDs.
Of course you can get the phone network ID, but what if that device doesn't have a carrier.
You could get the device ID, but what if it returns null for that manufacturer.
You could get the Advertiser ID, and this is often recommended, but it CAN CHANGE, it is unlikely to change, but if they do a factory reset or anything like that, it would.
So your best bet might be to generate your own unique IDs and store them locally with the application either in a DB implementation or SharedPreferences.
So to answer your question, if you have existing audience that you used the wrong, bad practice way of using unique IDs, then you should make an ID factory with if/else logic to fix it.
If (first time accessing an ID for this installed application)
//createOne and store it
else if(ID already exists and matches ANDROID_SECURE_ID method)
//get it the old way,
//create new one, and update your access to APIs or DB with new associated ID so that you never hit this code path again
else
//use correct ID implementation that you created
You could also just as easily do a one time check in your Application onCreate to fix anyone with wrong ID and then set a flag that you fixed it so you don't fix it again in sharedpref.

Related

How to get unique id for clients without registration? (Jodel app for e.g)

Imagine an application that consists of loads of clients and one server. The clients should be able to use the application without the need to register a username for e.g. (Therefore getting the feeling of anonymously using the app)
What unique data in a smartphone could be use to identify "anonymous" users and seperate them uniquely, so that each user would have its own personal data.
Could one use the IMEI id?
Is this possible?
At I/O 15 Google announced the 'instance ID' which uniquely identifies an app installation, and can be used for a veriety of other purposes as well.
I give more detail in an answer to an existing, similar question here:
https://stackoverflow.com/a/30948111/150016
Yes You can use the IMEI, but then you need a permission...or you could give every User a Unique ID on the server side
I think a long type variable is enough so the first one that connects you give him the ID 1 the app gets the ID and so on and so forth
You can use a built in feature here:
import android.provider.Settings.Secure;
private String uniqueIdentifier = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
where Secure.ANDROID_ID is presented as "A 64-bit number (as a hex string) that is randomly generated when the user first sets up the device and should remain constant for the lifetime of the user's device."
This is exactly the use case for the new Instance ID: getting a unique ID that you can use to identify even a not logged in user to your server.
This works on both phones and tablets (something IMEI does not) and avoid issues with ANDROID_ID (such as a few manufacturers always using the same ANDROID_ID on all devices).
You don't want to use the IMEI, as it will only apply to devices that have cell phones in them. There are plenty of wifi only devices out there in the wild.
Also, to get the IMEI, you need to ask for extra permission in your manifest.
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
You'd be better off either generating your own random GUID and storing it in your app's SQLite database, or use the Android ID field.
Android ID will give you extra benefits like providing a different identifier for different user profiles within a device.
Another answer here mentioned using Instance ID, but the big caveat there is it requires Google Play Services, so your app wouldn't work on AOSP / forked devices (i.e. Amazon's tablets). I'd have just inserted this as a comment to that answer, but my rep won't let me comment yet.

How to uniquely identify android device to track devices

Hello I am working on an application in which we are tracking our app installation using device unique ID.
We are doing right now like this as https://stackoverflow.com/a/2785493/2455259
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
I got to know that from android 4.2 it can have different for different users https://stackoverflow.com/a/27013749/2455259.
Now I can't take mac address of wifi or bluetooth because it requires to make it turn on then fetch. Even I can't take IMEI because some tables don't have call feature so they don't have IMEI.
Now what are the option to uniquely identify android device ?
Now what are the option to uniquely identify android device ?
I am afraid assigning own unique number is most effective approach, because ANDROID_ID is broken (the answer you link to is incorrect - see comments there too), relying on it is quite futile as it can sometimes be the same for many devices or null, so anything but unique, therefore using it is pointless as it adds more troubles than helps. There's post on Android blog about this topic "Identifying App Installations" that you may want to get thru as well.
I know this is old and the OP might have already moved on but with the introduction of Instance ID, we can use it to identify a unique installation.
The associated code is pretty straightforward too.
String iid = InstanceID.getInstance(context).getId();
The benefits include not having to specify permissions for Android Marshmallow and above. This seems to be a plus for developers who would need a unique id at the outset and cannot rely on user provided permission model for the same.

Get unique Id for the current user

How can I get a unique Id for the current user who is using my application?
I don't want to retrieve the device UDID (for my users privacy concerns).
I saw once that in the latest API versions (4.0+) there is such method to generate a unique
id for the current android user, but I cannot find it now.
This is the official statement from Google
http://android-developers.blogspot.be/2011/03/identifying-app-installations.html
You should really read it.
I guess you are using some kind of backend, so maybe you can create an endpoint that return a unique token that you save in the shared preferences.

Unique ID for each user in Android

I want to allocate unique ID to each user as soon as he installs the application so that whenever the app contacts the server I know who is contacting.
For this purpose, I thought that on first time installation, the app contacts the server and gets unique ID. But I don't know where to store it permanently so that next time when app is started, it knows what its ID is rather than contacting server.
Sorry if that is some obvious question as I am newbie.
This question has been asked many times on Stack Overflow.
In short: Android has always supported a unique ID. However, prior to Android 2.2, the ID was not always identical on certain kinds of phones. Since 2.2 is pretty ubiquitous by now, I would use that ID.
The Android Developer Blog has a good article about this.
And as Joachim said - you may want to consider a different approach altogether. Android's unique ID is good and persistent across factory resets, but not across a device upgrade. Also keep in mind that many people have several devices (like a phone and a tablet). You may want to use the Google account instead, the AccountManager can help you there.
Use SharedPreferences to store the unique id.
Here is an example:
Android SharedPreferences
For more complex data, you can use SQlite.
For unique id, you can use IMEI of device on which application is going to install. Refer this link for how to get IMEI number. Then stored that IMEI number in shared preference. Refer Guillermo lobar's link for that. You need to check for that unique id in preference when you application starts. At very first time, save that in preference. So when next time it checks for that id, app find it in preference and hence no need to connecting server. :)
You could get the IMEI of the device. As of API 26, getDeviceId() is deprecated. If you need to get the IMEI of the device, use the following:
String deviceId = "";
if (Build.VERSION.SDK_INT >= 26) {
deviceId = getSystemService(TelephonyManager.class).getImei();
}else{
deviceId = getSystemService(TelephonyManager.class).getDeviceId();
}

How can I generate a unique ID to be use as session id on the server side?

According to the net, the most recommended method in generating unique id is to use the Settings.Secure.ANDROID_ID. However, Settings.Secure.ANDROID_ID has a bug on Android 2.2. I am using Android 2.2 so I can't use it. On the other hand, there is also the mac address of the wiFi device and the TelephonyManager.getDeviceId(). The ID that I will be generating will be used as the session ID on the server side so it must be unique.
Will I be okay with just using the mac address of the wiFi as my unique ID and the device ID of the phone as fallback if the device has no wiFi interface? The application needs connection to the internet and so a wiFi interface or deviceId for phone will surely be present right? Or is it a bad idea?
I am also considering the use of UUID.randomUUID(). But even though there is a very small chance of generating the same id here, the probability still exist.
What ID can I use if that ID must also serve as session ID on the server? Also, please note that if the ID is already existing on the server, the ownership of the session will be given to the new user.
You can simply create a random ID (UUID is fine for that) on the server. Then you can ensure it's not in use yet.
I used ANDROID_ID, but i added random number in front of it and i get a random number...i check it if i had it in the base and if not i used that number...
The device id is unique. What's wrong getting it ?

Categories

Resources