首先AndroidMainfest.xml文件中添加权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 获取WIFI信息状态的权限 --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 获取网络状态改变的权限 -->然后再activity中添加如下代码:
private String getConnectWifiSsid(){ WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); Log.d("wifiInfo", wifiInfo.toString()); Log.d("SSID",wifiInfo.getSSID()); return wifiInfo.getSSID(); }
该接口即返回当前连接的wifi的名字,也就是SSID。