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

Asked

Viewed 839 times

1

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

2 answers

1

Use an object Wifimanager obtained with Context.getSystemService().

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

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

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

Each item 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 ACCCESS_FINE_LOCATION permissions are required.

0


Yes, there’s a way. You need a class called Wifiinfo

Add these permissions to the manifest:

<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 using 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: https://stackoverflow.com/questions/21391395/get-ssid-when-wifi-is-connected

Browser other questions tagged

You are not signed in. Login or sign up in order to post.