android 设备唯一码的获取,Cpu号,Mac地址
2014-01-06 17:28
507 查看
开发Android应用中,我们常常需要设备的唯一码来确定客户端。
Android 中的几中方法,使用中常常不可靠
1. DEVICE_ID
假设我们确实需要用到真实设备的标识,可能就需要用到DEVICE_ID。通过 TelephonyManager.getDeviceId()获取,它根据不同的手机设备返回IMEI,MEID或者ESN码.
缺点:在少数的一些设备上,该实现有漏洞,会返回垃圾数据
2. MAC ADDRESS
我们也可以通过Wifi获取MAC ADDRESS作为DEVICE ID
缺点:如果Wifi关闭的时候,硬件设备可能无法返回MAC ADDRESS.。
3. Serial Number
android.os.Build.SERIAL直接读取
缺点:在少数的一些设备上,会返回垃圾数据
4. ANDROID_ID
ANDROID_ID是设备第一次启动时产生和存储的64bit的一个数,
缺点:当设备被wipe后该数改变, 不适用。
android 底层是 Linux,我们还是用Linux的方法来获取:
1 cpu号:
文件在: /proc/cpuinfo
通过Adb shell 查看:
adb shell cat /proc/cpuinfo
2 mac 地址
文件路径 /sys/class/net/wlan0/address
adb shell cat /sys/class/net/wlan0/address
xx:xx:xx:xx:xx:aa
这样可以获取两者的序列号,
方法确定,剩下的就是写代码了
以Mac地址为例:
String getMac() {
String macSerial = null;
String str = "";
try {
Process pp = Runtime.getRuntime().exec(
"cat /sys/class/net/wlan0/address ");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; null != str;) {
str = input.readLine();
if (str != null) {
macSerial = str.trim();// 去空格
break;
}
}
} catch (IOException ex) {
// 赋予默认值
ex.printStackTrace();
}
return macSerial;
}
作者:hpccn
Android 中的几中方法,使用中常常不可靠
1. DEVICE_ID
假设我们确实需要用到真实设备的标识,可能就需要用到DEVICE_ID。通过 TelephonyManager.getDeviceId()获取,它根据不同的手机设备返回IMEI,MEID或者ESN码.
缺点:在少数的一些设备上,该实现有漏洞,会返回垃圾数据
2. MAC ADDRESS
我们也可以通过Wifi获取MAC ADDRESS作为DEVICE ID
缺点:如果Wifi关闭的时候,硬件设备可能无法返回MAC ADDRESS.。
3. Serial Number
android.os.Build.SERIAL直接读取
缺点:在少数的一些设备上,会返回垃圾数据
4. ANDROID_ID
ANDROID_ID是设备第一次启动时产生和存储的64bit的一个数,
缺点:当设备被wipe后该数改变, 不适用。
android 底层是 Linux,我们还是用Linux的方法来获取:
1 cpu号:
文件在: /proc/cpuinfo
通过Adb shell 查看:
adb shell cat /proc/cpuinfo
2 mac 地址
文件路径 /sys/class/net/wlan0/address
adb shell cat /sys/class/net/wlan0/address
xx:xx:xx:xx:xx:aa
这样可以获取两者的序列号,
方法确定,剩下的就是写代码了
以Mac地址为例:
String getMac() {
String macSerial = null;
String str = "";
try {
Process pp = Runtime.getRuntime().exec(
"cat /sys/class/net/wlan0/address ");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; null != str;) {
str = input.readLine();
if (str != null) {
macSerial = str.trim();// 去空格
break;
}
}
} catch (IOException ex) {
// 赋予默认值
ex.printStackTrace();
}
return macSerial;
}
作者:hpccn
相关文章推荐
- android 代码实现控件之间的间距
- 解決Linux下Android开发真机调试设备不被识别问题
- [Android]在代码里运行另一个程序的方法
- Android实现表情 抓取新浪表情
- 详解Android解析Xml的三种方式——DOM、SAX以及XMLpull
- [软件咨询]WPS2012正式版已发布 金山Office移动版4.0发布
- Android笔记-Linux Kernel Ftrace (Function Trace)解析
- android USB如何修改VID具体实现
- Android Mouse实现过程详细笔记
- 深入Android Browser配置管理的详解
- Android Mms之:深入理解对话列表管理
- Android APP与媒体存储服务的交互
- android 多线程技术应用
- Android之采用execSQL与rawQuery方法完成数据的添删改查操作详解
- Android数据类型之间相互转换系统介绍
- 通过Android trace文件分析死锁ANR实例过程
- Android实现左右滑动效果的方法详解
- Android 各国语言缩写及简称详细介绍
- Android 自动判断是电话,网址,EMAIL方法之Linkify的使用
- android IPC之binder通信机制