Android中Wifi未开启情况下获取Mac地址和IP

原来以为没有开启wifi功能是不能够获取wifi网卡mac地址的,在真机测试后发现能够获取到Mac地址只是获取的ip为0,当然wifi未连接状态下是肯定没有IP分配的。具体看看下面:

AndroidManifest.xml代码:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
//在wifi未开启状态下,仍然可以获取MAC地址,但是IP地址必须在已连接状态下否则为0
String macAddress = null, ip = null;
WifiManager wifiMgr = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
if (null != info) {
    macAddress = info.getMacAddress();
    ip = int2ip(info.getIpAddress());
}
System.out.println("mac:" + macAddress + ",ip:" + ip); 


 

posted @ 2012-06-22 16:40  坏混混  阅读(1622)  评论(2编辑  收藏  举报