There are several occasions when the unique identifier of a device is required. For instance you need it to generate a serial key and unlock a trial version, to generate encryption keys or to have the unique signature of a device.
On Android there are several ways to get such an ID.

  • The IMEI

  • Pseudo-Unique ID

  • The Android ID

  • The WLAN MAC Address string

  • The BT MAC Address string

1. The IMEI: only for Android devices with Phone use:

    TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String szImei = TelephonyMgr.getDeviceId(); // Requires READ_PHONE_STATE

This requires adding a permission in AndroidManifest.xml, and users will be notified upon installing your software: android.permission.READ_PHONE_STATE. The IMEI is unique for your phone and it looks like this: 359881030314356 (unless you have a pre-production device with an invalid IMEI like 0000000000000).

2. Pseudo-Unique ID, that works on all Android devices
Some devices don't have a phone (eg. Tablets) or for some reason you
don't want to include the READ_PHONE_STATE permission. You can still
read details like ROM Version, Manufacturer name, CPU type, and other
hardware details, that will be well suited if you want to use the ID for
a serial key check, or other general purposes. The ID computed in this
way won't be unique: it is possible to find two devices with the same ID
(based on the same hardware and rom image) but the chances in real
world applications are negligible. For this purpose you can use the
Build class:

String m_szDevIDShort = "" + //we make this look like a valid IMEI
Build.BOARD.length()%+ Build.BRAND.length()% +
Build.CPU_ABI.length()% + Build.DEVICE.length()% +
Build.DISPLAY.length()% + Build.HOST.length()% +
Build.ID.length()% + Build.MANUFACTURER.length()% +
Build.MODEL.length()% + Build.PRODUCT.length()% +
Build.TAGS.length()% + Build.TYPE.length()% +
Build.USER.length()% ; //13 digits

Most of the Build members are strings, what we're doing here is to take their length and transform it via modulo in a digit. We have 13 such digits and we are adding two more in front (35) to have the same size ID like the IMEI (15 digits). There are other possibilities here are well, just have a look at these strings.
Returns something like: 355715565309247 . No special permission are required, making this approach very convenient.

3. The Android ID , considered unreliable because it can
sometimes be null. The documentation states that it "can change upon
factory reset". This string can also be altered on a rooted phone.

  1. String m_szAndroidID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);

Returns: 9774d56d682e549c . No special permissions required.

4. The WLAN MAC Address string, is another unique identifier that you can use as a device id. Before you read it, you will need to make sure that your project has the android.permission.ACCESS_WIFI_STATE permission or the WLAN MAC Address will come up as null.

  1. WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
  2. String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();

Returns: 00:11:22:33:44:55 (not a real address since this is a custom ROM , as you can see the MAC address can easily be faked). WLAN doesn't have to be on, to read this value.

5. The BT MAC Address string, available on Android devices with Bluetooth, can be read if your project has the android.permission.BLUETOOTH permission.

  1. BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter
  2. m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  3. String m_szBTMAC = m_BluetoothAdapter.getAddress();

Returns: 43:25:78:50:93:38 . BT doesn't have to be on, to read it.

Combined Device ID
Above, you have here 5 ways of reading a device unique identifier. Some
of them might fail and return null, or you won't be able to use them
because of the special permissions or because the hardware is missing
(phone, bluetooth, wlan).
Nevertheless on all platforms you will find at least one that works. So a
very good idea is to mix these strings, and generate a unique result
out of their sum. To mix the strings you can simply concatenate them,
and the result can be used to compute a md5 hash:

String m_szLongID = m_szImei + m_szDevIDShort + m_szAndroidID+ m_szWLANMAC + m_szBTMAC;
// compute md5
MessageDigest m = null;
try {
m = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
m.update(m_szLongID.getBytes(),,m_szLongID.length());
// get md5 bytes
byte p_md5Data[] = m.digest();
// create a hex string
String m_szUniqueID = new String();
for (int i=;i<p_md5Data.length;i++) {
int b = (0xFF & p_md5Data[i]);
// if it is a single digit, make sure it have 0 in front (proper padding)
if (b <= 0xF) m_szUniqueID+="";
// add number to string
m_szUniqueID+=Integer.toHexString(b);
}
// hex string to uppercase
m_szUniqueID = m_szUniqueID.toUpperCase();

The result has 32 hex digits and it looks like this:

9DDDF85AFF0A87974CE4541BD94D5F55
You can use it to generate any size unique string using hex digits or other sets of characters.

The Serial Number is the ADB Serial Number for Developer.

For Detail:http://android-developers.blogspot.sg/2011/03/identifying-app-installations.html

最新文章

  1. MAGENTO - APACHE SOLR INTEGRATION - PART II (SETUP)
  2. PagerTabStrip及自定义的PagerTab
  3. Serenity框架官方文档翻译3.1(教程)
  4. BZOJ 1452 [JSOI2009] Count
  5. Retry Pattern
  6. 函数lock_rec_bitmap_reset
  7. Linq 用法笔记
  8. INCOIN Importing Multilingual Items (Doc ID 278126.1)
  9. 致命错误: Python.h:没有那个文件或目录
  10. 李洪强iOS开发之-cocopods安装
  11. JPA概要
  12. HDU 4720Naive and Silly Muggles热身赛2 1005题(分锐角钝角三角形讨论)
  13. node.js零基础详细教程(7):node.js操作mongodb,及操作方法的封装
  14. 将本地sql文件导入到mysql中
  15. Spark的性能调优杂谈
  16. JavaScript heap out of memory解决方法
  17. 啊金学习javascript系列一之javascript整体印象
  18. android 的几个黄色警告解决办法(转)
  19. 【JAVA】在线程里使用线程外的变量为什么一定要是final类型
  20. c#——表达式树在LINQ动态查询

热门文章

  1. 【FFT卷积】BZOJ3527-力
  2. bzoj 4506: [Usaco2016 Jan]Fort Moo
  3. hibernate处理视图问题(记录)
  4. WebAPI 操作返回
  5. CDHtmlDialog 基本使用
  6. POI创建Excel使用的常见的属性
  7. Cookie个数压缩存存储实践
  8. JStorm模型设计
  9. SEO之巴莱多定律
  10. python之epoll服务器源码分析