官方的参考地址
后期自己会翻译一下.
import android.content.ContentResolver;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
/**
* @author Administrator
* Date 2016/12/19
* 参考链接: (谷歌官方技术博客,值得参考)
* <html>https://android-developers.googleblog.com/2011/03/identifying-app-installations.html<html/>
* 下面的蓝牙/WIFI获取mac地址的方法 在6.0下的获取方法还有待测试.目前方法给出的方法
* [链接](https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-notifications)中有提到,但是具体无法如何实现没有6.0机器没测试.
*/
public class IdentifyDeviceID {
/**
* Since Android 2.3 (“Gingerbread”) this is available via android.os.Build.SERIAL. Devices without telephony are required to report a unique device ID here; some phones may do so also.
*/
public static String SERIAL = android.os.Build.SERIAL;
/**
* More specifically, Settings.Secure.ANDROID_ID. This is a 64-bit quantity that is generated and stored when the device first boots. It is reset when the device is wiped.
*
ANDROID_ID seems a good choice for a unique device identifier. There are downsides: First, it is not 100% reliable on releases of Android prior to 2.2 (“Froyo”). Also, there has been at least one widely-observed bug in a popular handset from a major manufacturer, where every instance has the same ANDROID_ID.
*/
public static String getAndroidID(Context context) {
ContentResolver mContentResolver = context.getContentResolver();
return Settings.Secure.getString(mContentResolver, Settings.Secure.ANDROID_ID);
}
/**
* However, there are problems with this approach:
Non-phones: Wifi-only devices or music players that don’t have telephony hardware just don’t have this kind of unique identifier.
在有些只有wifi或者音乐播放器上没有SIM卡的硬件,获取不到这个id
Persistence: On devices which do have this, it persists across device data wipes and factory resets. It’s not clear at all if, in this situation, your app should regard this as the same device.
在有的设备上,它会一直保持下去,即使你工厂化或者是格式化.
Privilege:It requires READ_PHONE_STATE permission, which is irritating if you don’t otherwise use or need telephony.
要求一定读取权限
Bugs: We have seen a few instances of production phones for which the implementation is buggy and returns garbage, for example zeros or asterisks.
在一些设备上有bug
* @param context
* @return
*/
public static String getTelphonyDeviceID(Context context) {
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getDeviceId();
}
/**
* 获取WIFI的mac地址
* @param context
* @return
*/
public static String getWIFIMac(Context context){
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> scanResults = wifiManager.getScanResults();
if (!scanResults.isEmpty()) {
ScanResult scanResult = scanResults.get(0);
return scanResult.BSSID;
}
return null;
}
/**
* 获取wifi的mac地址的另外一种方法,跟上面的是姊妹 (在 6.0 上也可以运行哒)
* @return
*
* Just read the file /sys/class/net/wlan0/address. The MAC address will be in the usual colon deliminated form. Works on my unrooted Nexus 6P running Android 6.0.1.
*/
public static String getWIFIMac2() {
File fiel = new File("/sys/class/net/wlan0/address");
fiel.exists();
try {
char[] haod = new char[1024];
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(fiel));
inputStreamReader.read(haod);
String s = new String(haod);
return s;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取蓝牙的mac地址
* @param context
* @return
*/
public static String getBlueToothMac(Context context) {
return android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
}
}