原来以为没有开启wifi功能是不能够获取wifi网卡mac地址的,在真机测试后发现能够获取到Mac地址只是获取的ip为0,当然wifi未连接状态下是肯定没有IP分配的。具体看看下面:
AndroidManifest.xml代码:
1
|
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
|
Java代码:
1
2 3 4 5 6 7 8 9 |
//在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 ) ; |
注:Android提供的api获取IP为整型模式,需要将其转换为字符串模式。具体查看IP地址与整数之间的转换
转载请注明地址: http://orgcent.com/android-wifi-mac-ip-address/ | 萝卜白菜的博客