本(混合和Marakana等各種答案的匹配)將同時得到你想要提取的一切:
- 所有WiFi路由器範圍
- 連接無線路由器
所有存儲WiFi網絡(在設備上)
public String getCurrentSsid(Context context) {
String ssid = null;
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) {
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
if (connectionInfo != null && !(connectionInfo.getSSID().equals(""))) {
//if (connectionInfo != null && !StringUtil.isBlank(connectionInfo.getSSID())) {
ssid = connectionInfo.getSSID();
}
// Get WiFi status MARAKANA
WifiInfo info = wifiManager.getConnectionInfo();
String textStatus = "";
textStatus += "\n\nWiFi Status: " + info.toString();
String BSSID = info.getBSSID();
String MAC = info.getMacAddress();
List<ScanResult> results = wifiManager.getScanResults();
ScanResult bestSignal = null;
int count = 1;
String etWifiList = "";
for (ScanResult result : results) {
etWifiList += count++ + ". " + result.SSID + " : " + result.level + "\n" +
result.BSSID + "\n" + result.capabilities +"\n" +
"\n=======================\n";
}
Log.v(TAG, "from SO: \n"+etWifiList);
// List stored networks
List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();
for (WifiConfiguration config : configs) {
textStatus+= "\n\n" + config.toString();
}
Log.v(TAG,"from marakana: \n"+textStatus);
}
return ssid;
}
免責聲明:雖然這是工作代碼,而不是僞代碼,但它的唯一目的是說明從wifi連接中提取數據的方法,它應在使用前進行調整(並清理)。
有誰知道需要哪些權限才能做到這一點? – 2014-01-15 09:41:10
android.permission.ACCESS_WIFI_STATE – 2014-01-29 23:04:02