How do I get the name of the Wi-Fi network (SSID)?

1

I want to make an app that takes the information of the Wi-Fi connection, that the cell phone is connected, being more specific I want to make an app that takes the name (SSID) of the Wi-Fi network where the phone is connected . Is it possible to do that? If so, could you show me how or send me links that show how.

    
asked by anonymous 16.05.2018 / 05:53

2 answers

0

Yes, there is a way. You need a class called WifiInfo

Add these permissions to the manifest file:

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

And the code specifically, without the use of a broadcast:

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo;
String ssid = null;

wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
    ssid = wifiInfo.getSSID();
}
  • From Android 8.1 (API 27), you also need to add this permission to manifest , if you are working with this API:

ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION

Source: link

    
16.05.2018 / 13:14
1

Use a WifiManager object obtained with Context.getSystemService() .

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

Use the getScanResults() method to get the list of access point detected.

List<ScanResult> scanResults = wifi.getScanResults();

Each element in the list contains detailed information of each of the access point detected.

The SSID can be obtained with

String ssid = scanResults[i].SSID; // i = 0 para o primeiro access point

ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions are required.

    
16.05.2018 / 13:05