Android需要添加权限以获得Mac地址和IP地址,
在AndroidManifest.xml中:
uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
1、获取手机的Mac地址,即使未打开Wifi或未连接Wi-Fi也可以获取手机的Mac地址。代码如下:
public static String getMacAddress(Context context) {
String macAddress = null;
WifiInfo wifiInfo = getWifiInfo(context);
if (wifiInfo != null) {
macAddress = wifiInfo.getMacAddress();
}
return macAddress;
}
2、获取手机IP地址,只有在Wifi连接的情况下才能获取IP地址,否则为0,对应的转换IP为0.0.0.0,其中getIpAddress ()获取IP地址,它是一个整数,需要转换为IP地址。代码如下:
public static String getIpAddress(Context context) {
String IpAddress = null;
WifiInfo wifiInfo = getWifiInfo(context);
if (wifiInfo != null) {
IpAddress = intToIpAddress(wifiInfo.getIpAddress());
}
return IpAddress;
}
public static String intToIpAddress(long ipInt) {
StringBuffer sb = new StringBuffer();
sb.append(ipInt & 0xFF).append(".");
sb.append((ipInt >> 8) & 0xFF).append(".");
sb.append((ipInt >> 16) & 0xFF).append(".");
sb.append((ipInt >> 24) & 0xFF);
return sb.toString();
}
3、获取路由器的Mac地址,仅当连接成功(废话……)时,代码如下:
public static String getConnectedWifiMacAddress(Context context) {
String connectedWifiMacAddress = null;
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List wifiList;
if (wifiManager != null) {
wifiList = wifiManager.getScanResults();
WifiInfo info = wifiManager.getConnectionInfo();
if (wifiList != null && info != null) {
for (int i = 0; i < wifilist.size();="" i++)="" {="" scanresult="" result="">get(i);
if (info.getBSSID().equals(result.BSSID)) {
connectedWifiMacAddress = result.BSSID;
}
}
}
}
return connectedWifiMacAddress;
}
获取Mac和IP地址的完整工具代码如下:
package com.cpxiao.wifimacdemo;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import java.util.List;
/**
* Created by cpxiao on 15/9/21.
*/
public class MacAddressUtils {
/**
* 获取手机的Mac地址,在Wifi未开启或者未连接的情况下也能获取手机Mac地址
*/
public static String getMacAddress(Context context) {
String macAddress = null;
WifiInfo wifiInfo = getWifiInfo(context);
if (wifiInfo != null) {
macAddress = wifiInfo.getMacAddress();
}
return macAddress;
}
/**
* 获取手机的Ip地址
*/
public static String getIpAddress(Context context) {
String IpAddress = null;
WifiInfo wifiInfo = getWifiInfo(context);
if (wifiInfo != null) {
IpAddress = intToIpAddress(wifiInfo.getIpAddress());
}
return IpAddress;
}
/**
* 获取WifiInfo
*/
public static WifiInfo getWifiInfo(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = null;
if (null != wifiManager) {
info = wifiManager.getConnectionInfo();
}
return info;
}
public static long ipAddressToint(String ip) {
String[] items = ip.split("\\.");
return Long.valueOf(items[0]) <>24
| Long.valueOf(items[1]) <>16
| Long.valueOf(items[2]) <>8
| Long.valueOf(items[3]);
}
public static String intToIpAddress(long ipInt) {
StringBuffer sb = new StringBuffer();
sb.append(ipInt & 0xFF).append(".");
sb.append((ipInt >> 8) & 0xFF).append(".");
sb.append((ipInt >> 16) & 0xFF).append(".");
sb.append((ipInt >> 24) & 0xFF);
return sb.toString();
}
/**
* 获取当前可连接Wifi列表
*/
public static List??> getAvailableNetworks(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List wifiList = null;
if (wifiManager != null) {
wifiList = wifiManager.getScanResults();
}
return wifiList;
}
/**
* 获取已连接的Wifi路由器的Mac地址
*/
public static String getConnectedWifiMacAddress(Context context) {
String connectedWifiMacAddress = null;
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List wifiList;
if (wifiManager != null) {
wifiList = wifiManager.getScanResults();
WifiInfo info = wifiManager.getConnectionInfo();
if (wifiList != null && info != null) {
for (int i = 0; i < wifilist.size();="" i++)="" {="" scanresult="" result="wifiList.get(i);">if (info.getBSSID().equals(result.BSSID)) {
connectedWifiMacAddress = result.BSSID;
}
}
}
}
return connectedWifiMacAddress;
}
}
参考:
本文来自本站,转载请注明本文网址:
http://www.pc-fly.com/a/tongxingongju/article-348584-1.html
……