android获取wifi的ssid和ip

话不多说,直接上代码

WifiManager wifi = (WifiManager) MainActivity.context
				.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();

if (info != null && info.getMacAddress() != null)

	ssidString = info.getSSID();

不过要申请下权限,在AndroidManifest.xml中添加

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

获取ip地址

public static String get_wifi_ip()
	{
		int ip ;
		
		if (info() == null)
			return "";
		
		ip = info().getIpAddress();
		return intToIp(ip);
	}
	

private static String intToIp(int i) {       
        
        return (i & 0xFF ) + "." +       
      ((i >> 8 ) & 0xFF) + "." +       
      ((i >> 16 ) & 0xFF) + "." +       
      ( i >> 24 & 0xFF) ;  
   }


©️2020 CSDN 皮肤主题: 黑客帝国 设计师:上身试试 返回首页