此篇文章对比android设备唯一码的获取之一看比较好,地址
http://blog.csdn.net/fastthinking/article/details/18001967
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); String deviceid = tm.getDeviceId(); if(deviceid == null || deviceid.length==0){ WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE); if (manager != null) { deviceid = manager.getConnectionInfo().getMacAddress(); } }
Android 中的几中方法,使用中常常不可靠
1. DEVICE_ID
假设我们确实需要用到真实设备的标识,可能就需要用到DEVICE_ID。通过 TelephonyManager.getDeviceId()获取,它根据不同的手机设备返回IMEI,MEID或者ESN码.
缺点:在少数的一些设备上,该实现有漏洞,会返回垃圾数据
2. MAC ADDRESS
我们也可以通过Wifi获取MAC ADDRESS作为DEVICE ID
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE); if (manager != null) { String MacAddress = manager.getConnectionInfo().getMacAddress(); Log.i(tag, "MacAddress is " + MacAddress); }
如果Wifi关闭的时候,硬件设备可能不能返回MAC ADDRESS。经测试,某些定制平板在wifi开关关闭的时候确实返回不了mac地址。另外使用一个无电话功能的台电a10四核pad和小米1s联通版。
A hardware serial number, if available. Alphanumeric only, case-insensitive.
android.os.Build.SERIAL 直接读取缺点:在少数的一些设备上,会返回垃圾数据
2 mac 地址
文件路径 /sys/class/net/wlan0/address
adb shell cat /sys/class/net/wlan0/address
xx:xx:xx:xx:xx:aa
如果Wifi关闭的时候,硬件设备不能返回MAC ADDRESS.测试使用一个无电话功能的台电a10四核pad和小米1s联通版
在wifi打开的时候获取的mac地址同方法2地址相同。
获取MAC 地址代码
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TelephonyManager tm = (TelephonyManager) this .getSystemService(Context.TELEPHONY_SERVICE); String simSerialNumber = tm.getSimSerialNumber(); Log.i(tag, "simSerialNumber is " + simSerialNumber); String deviceid = tm.getDeviceId(); Log.i(tag, "DeviceId is " + deviceid); String SubscriberId = tm.getSubscriberId(); Log.i(tag, "SubscriberId is " + SubscriberId); String Line1Number = tm.getLine1Number(); Log.i(tag, "Line1Number is " + Line1Number); String ANDROID_ID = Secure.getString(getContentResolver(), Secure.ANDROID_ID); Log.i(tag, "ANDROID_ID is " + ANDROID_ID); Log.i(tag, "android.os.Build.SERIAL is " + android.os.Build.SERIAL); WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE); if (manager != null) { String MacAddress = manager.getConnectionInfo().getMacAddress(); Log.i(tag, "MacAddress is " + MacAddress); } Log.i(tag, "macSerial getted by linux is " + getMac()); } private String getMac() { String macSerial = null; String str = ""; Process pp = null; try { <span style="white-space:pre"> </span>pp = Runtime.getRuntime().exec( <span style="white-space:pre"> </span>"cat /sys/class/net/wlan0/address "); <span style="white-space:pre"> </span>InputStreamReader ir = new InputStreamReader(pp.getInputStream()); <span style="white-space:pre"> </span>LineNumberReader input = new LineNumberReader(ir); <span style="white-space:pre"> </span>for (; null != str;) { <span style="white-space:pre"> </span>str = input.readLine(); <span style="white-space:pre"> </span>if (str != null) { <span style="white-space:pre"> </span>macSerial = str.trim();// 去空格 <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} catch (IOException ex) { <span style="white-space:pre"> </span>// 赋予默认值 <span style="white-space:pre"> </span>ex.printStackTrace(); <span style="white-space:pre"> </span>}finally { <span style="white-space:pre"> </span>if(pp!=null){ <span style="white-space:pre"> </span>pp.destroy(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>return macSerial; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>}台电a10 4核平板 打开wifi
07-29 14:43:06.880: I/MainActivity(21404): DeviceId is 3588170056-------
07-29 14:43:06.880: I/MainActivity(21404): SubscriberId is
07-29 14:43:06.880: I/MainActivity(21404): Line1Number is
07-29 14:43:06.880: I/MainActivity(21404): ANDROID_ID is c76ec70cf84-------
07-29 14:43:06.880: I/MainActivity(21404): android.os.Build.SERIAL is unknown
07-29 14:43:06.890: I/MainActivity(21404): MacAddress is 00:7d:0e:0-------
07-29 14:43:06.920: I/MainActivity(21404): macSerial getted by linux is 00:7d:0e:03:c6:e6
台电a10 4核平板 关闭wifi
07-29 14:45:25.340: I/MainActivity(21404): macSerial getted by linux is null
小米1s 打开wifi
07-29 14:46:35.934: I/MainActivity(10172): simSerialNumber is 898601128---------
07-29 14:46:35.934: I/MainActivity(10172): DeviceId is 8630920---------
07-29 14:46:35.934: I/MainActivity(10172): SubscriberId is 4600191---------
07-29 14:46:35.944: I/MainActivity(10172): Line1Number is +86156---------
07-29 14:46:35.944: I/MainActivity(10172): ANDROID_ID is fa7ae33b---------
07-29 14:46:35.944: I/MainActivity(10172): android.os.Build.SERIAL is 341---------
07-29 14:46:35.954: I/MainActivity(10172): MacAddress is d4:97:0b---------
07-29 14:43:06.920: I/MainActivity(21404): macSerial getted by linux is 00:7d:0-------
小米1s 关闭wifi
07-29 14:46:35.994: I/MainActivity(10172): macSerial getted by linux is null