Xamarin Android Device Serial Number

  1. Xamarin Android Emulator
  2. Xamarin Android Camera

SerialNumber; Android.Mtp.MtpDeviceInfo.SerialNumber Property. Returns the unique serial number for the MTP device. Xamarin Inc., as a wholly-owned. Over a year ago, I wrote a quick tip on getting device Unique ID for Windows and Windows Phone (8 & 7).Lately, at RedBit, we have been doing quite a bit of iOS, Android and Windows projects using Xamarin and recently put two articles up on our learnings, Cross Platform Animations using Xamarin Forms and GZip Compression with Xamarin Forms.

Active12 months ago

I need to use a unique ID for an Android app and I thought the serial number for the device would be a good candidate. How do I retrieve the serial number of an Android device in my app ?

Idolon
23.4k10 gold badges84 silver badges109 bronze badges
EnoEno
5,31318 gold badges49 silver badges78 bronze badges

16 Answers

getSystemService is a method from the Activity class. getDeviceID() will return the MDN or MEID of the device depending on which radio the phone uses (GSM or CDMA).

Each device MUST return a unique value here (assuming it's a phone). This should work for any Android device with a sim slot or CDMA radio. You're on your own with that Android powered microwave ;-)

Tutorialhasemanhaseman
9,5438 gold badges37 silver badges38 bronze badges

As Dave Webb mentions, the Android Developer Blog has an article that covers this.

I spoke with someone at Google to get some additional clarification on a few items. Here's what I discovered that's NOT mentioned in the aforementioned blog post:

  • ANDROID_ID is the preferred solution. ANDROID_ID is perfectly reliable on versions of Android <=2.1 or >=2.3. Only 2.2 has the problems mentioned in the post.
  • Several devices by several manufacturers are affected by the ANDROID_ID bug in 2.2.
  • As far as I've been able to determine, all affected devices have the same ANDROID_ID, which is 9774d56d682e549c. Which is also the same device id reported by the emulator, btw.
  • Google believes that OEMs have patched the issue for many or most of their devices, but I was able to verify that as of the beginning of April 2011, at least, it's still quite easy to find devices that have the broken ANDROID_ID.

Based on Google's recommendations, I implemented a class that will generate a unique UUID for each device, using ANDROID_ID as the seed where appropriate, falling back on TelephonyManager.getDeviceId() as necessary, and if that fails, resorting to a randomly generated unique UUID that is persisted across app restarts (but not app re-installations).

ceph3us
5,4542 gold badges30 silver badges36 bronze badges
emmbyemmby
67.5k58 gold badges174 silver badges237 bronze badges

This code returns device serial number using a hidden Android API.

Wesley Wiser
7,1804 gold badges36 silver badges56 bronze badges
Roman SLRoman SL

Although, it is not guaranteed that the Android ID will be an unique identifier.

Anthony ForloneyAnthony Forloney
74.7k12 gold badges107 silver badges111 bronze badges

There is an excellent post on the Android Developer's Blog discussing this.

It recommends against using TelephonyManager.getDeviceId() as it doesn't work on Android devices which aren't phones such as tablets, it requires the READ_PHONE_STATE permission and it doesn't work reliably on all phones.

Instead you could use one of the following:

  • Mac Address
  • Serial Number
  • ANDROID_ID

The post discusses the pros and cons of each and it's worth reading so you can work out which would be the best for your use.

Dave WebbDave Webb
162k51 gold badges292 silver badges292 bronze badges

For a simple number that is unique to the device and constant for its lifetime (barring a factory reset or hacking), use Settings.Secure.ANDROID_ID.

To use the device serial number (the one shown in 'System Settings / About / Status') if available and fall back to Android ID:

Edward BreyEdward Brey
27.9k11 gold badges142 silver badges199 bronze badges

The IMEI is good but only works on Android devices with phone. You should consider support for Tablets or other Android devices as well, that do not have a phone.

You have some alternatives like: Build class members, BT MAC, WLAN MAC, or even better - a combination of all these.

I have explained these details in an article on my blog, see: http://www.pocketmagic.net/?p=1662

radhooradhoo

Since no answer here mentions a perfect, fail-proof ID that is both PERSISTENT through system updates and exists in ALL devices (mainly due to the fact that there isn't an individual solution from Google), I decided to post a method that is the next best thing by combining two of the available identifiers, and a check to chose between them at run-time.

Before code, 3 facts:

  1. TelephonyManager.getDeviceId() (a.k.a.IMEI) will not work well or at all for non-GSM, 3G, LTE, etc. devices, but will always return a unique ID when related hardware is present, even when no SIM is inserted or even when no SIM slot exists (some OEM's have done this).

  2. Since Gingerbread (Android 2.3) android.os.Build.SERIALmust exist on any device that doesn't provide IMEI, i.e., doesn't have the aforementioned hardware present, as per Android policy.

  3. Due to fact (2.), at least one of these two unique identifiers will ALWAYS be present, and SERIAL can be present at the same time that IMEI is.

Note: Fact (1.) and (2.) are based on Google statements

SOLUTION

With the facts above, one can always have a unique identifier by checking if there is IMEI-bound hardware, and fall back to SERIAL when it isn't, as one cannot check if the existing SERIAL is valid. The following static class presents 2 methods for checking such presence and using either IMEI or SERIAL:

I would advice on using getCleartextID_HARDCHECK. If the reflection doesn't stick in your environment, use the getCleartextID_SIMCHECK method instead, but take in consideration it should be adapted to your specific SIM-presence needs.

P.S.: Do please note that OEM's have managed to bug out SERIAL against Google policy (multiple devices with same SERIAL), and Google as stated there is at least one known case in a big OEM (not disclosed and I don't know which brand it is either, I'm guessing Samsung).

Disclaimer: This answers the original question of getting a unique device ID, but the OP introduced ambiguity by stating he needs a unique ID for an APP. Even if for such scenarios Android_ID would be better, it WILL NOT WORK after, say, a Titanium Backup of an app through 2 different ROM installs (can even be the same ROM). My solution maintains persistence that is independent of a flash or factory reset, and will only fail when IMEI or SERIAL tampering occurs through hacks/hardware mods.

leRobotleRobot
1,3101 gold badge16 silver badges27 bronze badges

There are problems with all the above approaches. At Google i/o Reto Meier released a robust answer to how to approach this which should meet most developers needs to track users across installations.

This approach will give you an anonymous, secure user ID which will be persistent for the user across different devices (including tablets, based on primary Google account) and across installs on the same device. The basic approach is to generate a random user ID and to store this in the apps shared preferences. You then use Google's backup agent to store the shared preferences linked to the Google account in the cloud.

Lets go through the full approach. First we need to create a backup for our SharedPreferences using the Android Backup Service. Start by registering your app via this link: http://developer.android.com/google/backup/signup.html

Google will give you a backup service key which you need to add to the manifest. You also need to tell the application to use the BackupAgent as follows:

Then you need to create the backup agent and tell it to use the helper agent for sharedpreferences:

To complete the backup you need to create an instance of BackupManager in your main Activity:

Finally create a user ID, if it doesn't already exist, and store it in the SharedPreferences:

Serial

This User_ID will now be persistent across installations, even if the user switches devices.

For more information on this approach see Reto's talk here http://www.google.com/events/io/2011/sessions/android-protips-advanced-topics-for-expert-android-app-developers.html

And for full details of how to implement the backup agent see the developer site here: http://developer.android.com/guide/topics/data/backup.html I particularly recommend the section at the bottom on testing as the backup does not happen instantaneously and so to test you have to force the backup.

Stanley
1,0995 gold badges17 silver badges32 bronze badges
TechnoTonyTechnoTony

Another way is to use /sys/class/android_usb/android0/iSerial in an App with no permissions whatsoever.

To do this in java one would just use a FileInputStream to open the iSerial file and read out the characters. Just be sure you wrap it in an exception handler because not all devices have this file.

At least the following devices are known to have this file world-readable:

  • Galaxy Nexus
  • Nexus S
  • Motorola Xoom 3g
  • Toshiba AT300
  • HTC One V
  • Mini MK802
  • Samsung Galaxy S II

You can also see my blog post here: http://insitusec.blogspot.com/2013/01/leaking-android-hardware-serial-number.html where I discuss what other files are available for info.

insitusecinsitusec

As @haserman says:

But it's necessary including the permission in the manifest file:

cesardscesards
10.4k11 gold badges54 silver badges52 bronze badges

Unique device ID of Android OS Device as String.

but I strngly recommend this method suggested by Google::

JorgesysJorgesys
101k16 gold badges263 silver badges230 bronze badges

Build.SERIAL is the simplest way to go, although not entirely reliable as it can be empty or sometimes return a different value (proof 1, proof 2) than what you can see in your device's settings.

There are several ways to get that number depending on the device's manufacturer and Android version, so I decided to compile every possible solution I could found in a single gist. Here's a simplified version of it :

flawyteflawyte
5,5603 gold badges32 silver badges53 bronze badges

I know this question is old but it can be done in one line of code

String deviceID = Build.SERIAL;

MichaelStoddartMichaelStoddart
3,7593 gold badges20 silver badges39 bronze badges

I found the example class posted by @emmby above to be a great starting point. But it has a couple of flaws, as mentioned by other posters. The major one is that it persists the UUID to an XML file unnecessarily and thereafter always retrieves it from this file. This lays the class open to an easy hack: anyone with a rooted phone can edit the XML file to give themselves a new UUID.

I've updated the code so that it only persists to XML if absolutely necessary (i.e. when using a randomly generated UUID) and re-factored the logic as per @Brill Pappin's answer:

Carlos PCarlos P

Yes. It is a device hardware serial number and it is unique. So on api level 2.3 and above you can use android.os.Build.ANDROID_ID to get it. For below 2.3 API level use TelephonyManager.getDeviceID().

you can read this http://android-developers.blogspot.in/2011/03/identifying-app-installations.html

Mahesh Ramchandra BhatkandeMahesh Ramchandra Bhatkande

Not the answer you're looking for? Browse other questions tagged androidserial-number or ask your own question.

Device Information Plugin for Xamarin and Windows

Simple way of getting common device information in Xamarin.iOS, Xamarin.Android, Windows, and Xamarin.Forms projects.

Setup

  • Available on NuGet: http://www.nuget.org/packages/Xam.Plugin.DeviceInfo
  • Install into your PCL/netstandard project and Client projects.

Build status:

The Future: Xamarin.Essentials

I have been working on Plugins for Xamarin for a long time now. Through the years I have always wanted to create a single, optimized, and official package from the Xamarin team at Microsoft that could easily be consumed by any application. The time is now with Xamarin.Essentials, which offers over 50 cross-platform native APIs in a single optimized package. I worked on this new library with an amazing team of developers and I highly highly highly recommend you check it out.

I will continue to work and maintain my Plugins, but I do recommend you checkout Xamarin.Essentials to see if it is a great fit your app as it has been for all of mine!

Platform Support

PlatformVersion
Xamarin.iOSiOS 7+
Xamarin.AndroidAPI 13+
Windows 10 UWP10+
Xamarin.MacAll
Xamarin.MacAll
watchOSAll
tvOSAll
Tizen4.0+

API Usage

Call CrossDeviceInfo.Current from any project or PCL to gain access to APIs.

GenerateAppIdUsed to generate a unique Id for your app.

IdThis should not be used as a stable ID as each vendor on Android may or may not set it to a different value and on iOS it is changed when applications are uninstalled.

Device Model

Manufacturer

DeviceName

Version

Xamarin Android Emulator

Returns the specific version number of the OS such as:

  • iOS: 8.1
  • Android: 4.4.4
  • Windows Phone: 8.10.14219.0
  • UWP: 10.0.14393.105
  • Tizen: 4.0

VersionNumber

AppVersion

AppBuild

Platform

Returns the Platform enum of:

Idiom

Returns the Idiom enum of:

IsDevice

Returns true, if the app is running on a real physical device. false is returned if the app is running on an emulator or simulator (whatever applies for the platform).

Additional Android Setup

This plugin uses the Current Activity Plugin to get access to the current Android Activity. Be sure to complete the full setup if a MainApplication.cs file was not automatically added to your application. Please fully read through the Current Activity Plugin Documentation. At an absolute minimum you must set the following in your Activity's OnCreate method:

It is highly recommended that you use a custom Application that are outlined in the Current Activity Plugin Documentation](https://github.com/jamesmontemagno/CurrentActivityPlugin/blob/master/README.md)

Contributions

Contributions are welcome! If you find a bug please report it and if you want a feature please report it.

If you want to contribute code please file an issue and create a branch off of the current dev branch and file a pull request.

License

Xamarin Android Camera

Under MIT, see LICENSE file.