1. 常用方法,调用Android的API:WifiManager
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
return info.getMacAddress();
此方法需要保证WIFI在本次开机以来曾经是打开过的,否则会返回null。所以需要后台尝试先打开WIFI再获取。
示例代码:
//尝试打开wifi
private static boolean tryOpenMAC(WifiManager manager)
{
boolean softOpenWifi = false;
int state = manager.getWifiState();
if (state != WifiManager.WIFI_STATE_ENABLED && state != WifiManager.WIFI_STATE_ENABLING)
{
manager.setWifiEnabled(true);
softOpenWifi = true;
}
return softOpenWifi;
}
//尝试关闭MAC
private static void tryCloseMAC(WifiManager manager)
{