Hello, in the application I display the characteristics of the WiFi network. It is not possible to display a field with the type of network protection. Tell me, what am I doing wrong? ps implementation through fragments

public String getScanResultSecurity(ScanResult scanResult) { final String cap = scanResult.capabilities; final String[] securityModes = {"WEP", "PSK", "EAP", "WPA"}; for (int i = securityModes.length - 1; i >= 0; i--) { if (cap.contains(securityModes[i])) { return securityModes[i]; } } return "OPEN"; } //далее часть кода из onCreateView WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); String currentSSID = wifiInfo.getSSID(); List<ScanResult> results = wifiManager.getScanResults(); for (ScanResult result : results) { if (currentSSID.equals(result.SSID)) { String securityMode = getScanResultSecurity(result); if (securityMode.equals("WEP")) { textView_network92.setText("WEP"); } else if (securityMode.equals("PSK")) { textView_network92.setText("PSK"); }else if (securityMode.equals("EAP")) { textView_network92.setText("EAP"); }else if (securityMode.equals("WPA")) { textView_network92.setText("WPA") }else textView_network92.setText("OPEN"); } } 
  • And where is this textView_network92 then used? The value was assigned, and forgot to show. What is written in the logs? - Enikeyschik

1 answer 1

 WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE); wifiManager.startScan(); List<ScanResult> results = wifiManager.getScanResults(); for (ScanResult result : results) { textView_network92.setText(result.capabilities); } 

And also make sure that you have the required permissions:

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

UPD. (thanks @Timur)

Starting with API version 23+, the following permission is also required.

 <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/> 
  • Checked for all permissions. I noticed such a problem in the logs: the size of the List <ScanResult> results = 0 in the variable results.size(); - Timur
  • @Timur, supplemented the answer with the startScan method - A. Shakhov
  • one
    thanks for the help. I figured out the problem was that in order for List<ScanResult> be non-zero in Android OS 6.0 and ACCESS_COARSE_LOCATION permission is needed ACCESS_COARSE_LOCATION - Timur
  • @Timur, ok) Well, maybe your version also works if you call startScan() after initializing the wifiManager
  • @Shakhov I tried your option, but the list was also empty - Timur