如何从Android使用android API获取wifinetworking的名称?
我以为我应该使用NetworkInterface :: getDisplayName。 我有一些名字,但是这个名字是不同的,我可以看到这个名字,当我select我想要连接的networking。
请帮忙..
[编辑]
根据Loxley的回答:
WifiManager wifiMgr = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); String name = wifiInfo.getSSID();
android.net.wifi.WifiInfo.getSSID
?
这个(从Marakana和其他人的各种答案的混合和匹配)将同时得到你想要提取的一切:
- 所有的无线路由器的范围
- 连接wifi路由器
-
所有存储的WiFinetworking(在您的设备上)
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连接数据提取方法,它应该适应(和清理)使用前。
public String getWifiName(Context context) { WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (manager.isWifiEnabled()) { WifiInfo wifiInfo = manager.getConnectionInfo(); if (wifiInfo != null) { DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()); if (state == DetailedState.CONNECTED || state == DetailedState.OBTAINING_IPADDR) { return wifiInfo.getSSID(); } } } return null; }