SoFunction
Updated on 2025-03-03

Steps to get a list of WiFi networks on Android

Android gets a list of Wi-Fi networks

Step 1: Add permissions

First, we need to add the required permissions to the file. Open the file and then<manifest>Add the following permissions under the tag:

xmlCopy code
<uses-permission android:name=".ACCESS_COARSE_LOCATION" />
<uses-permission android:name=".ACCESS_FINE_LOCATION" />
<uses-permission android:name=".ACCESS_WIFI_STATE" />
<uses-permission android:name=".CHANGE_WIFI_STATE" />

The above permissions include permissions to obtain device location and Wi-Fi status.

Step 2: Create a Wi-Fi Manager

Next, we will create a Wi-Fi manager in the Activity or Fragment. In the Java class, add the following code:

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

Step 3: Check and Turn on Wi-Fi

Before getting the list of Wi-Fi networks, we need to make sure that Wi-Fi is turned on. You can add the following code snippet:

if (!()) {
    (true);
}

The above code checks the Wi-Fi status, and if Wi-Fi is off, turn on Wi-Fi.

Step 4: Get the Wi-Fi network list

The following code snippet shows how to get a list of Wi-Fi networks:

List&lt;ScanResult&gt; scanResults = ();
for (ScanResult result : scanResults) {
    String ssid = ;
    String bssid = ;
    int signalStrength = ;
    // Other operations, such as displaying in a list or making connections, etc.}

The above code is calledgetScanResults()Method to obtain the list of Wi-Fi networks and passScanResultObjects access information such as SSID, BSSID, and signal strength. You can operate on this information based on your needs, such as displaying it in a list or selecting a connection.

Step 5: Permission Request (optional)

Starting with Android 6.0 (API level 23), permissions to access location information are required at runtime. You can use the runtime permission request mechanism to achieve this. This is beyond the scope of this article, but make sure to request the necessary permissions when using the application.

Showcases a list of Wi-Fi networks and allows users to choose to connect to one of the networks. Here is a sample code that combines this scenario:
 

public class WifiListActivity extends AppCompatActivity {
    private WifiManager wifiManager;
    private List&lt;ScanResult&gt; scanResults;
    private RecyclerView recyclerView;
    private WifiListAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView(.activity_wifi_list);
        // Initialize the Wi-Fi Manager        wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        // Check and turn on Wi-Fi        if (!()) {
            (true);
        }
        // Get the Wi-Fi network list        scanResults = ();
        // Initialize RecyclerView        recyclerView = findViewById(.recycler_view);
        (new LinearLayoutManager(this));
        adapter = new WifiListAdapter(scanResults);
        (adapter);
        // Connect to Wi-Fi network when clicking on the list item        (new () {
            @Override
            public void onItemClick(int position) {
                if (position &gt;= 0 &amp;&amp; position &lt; ()) {
                    ScanResult selectedWifi = (position);
                    connectToWifi(selectedWifi);
                }
            }
        });
    }
    private void connectToWifi(ScanResult selectedWifi) {
        String networkSSID = ;
        String networkPassword = "your_wifi_password"; // Replace with the actual Wi-Fi password        WifiConfiguration wifiConfiguration = new WifiConfiguration();
         = "\"" + networkSSID + "\"";
         = "\"" + networkPassword + "\"";
        int networkId = (wifiConfiguration);
        ();
        (networkId, true);
        ();
        // After the connection is successful, perform other processing        // ...
    }
}

In the example code above, we first get the Wi-Fi network list and pass it to a custom WifiListAdapter adapter. The adapter displays the Wi-Fi network list in the RecyclerView. When the user clicks on a Wi-Fi network in the list, we call the connectToWifi() method to connect to the selected network. Note that in the connectToWifi method we use a virtual Wi-Fi password that you need to replace with the actual Wi-Fi password. After connecting to a Wi-Fi network, you can perform other processing in the corresponding callback method, such as updating the UI, sending data, etc.

When a user clicks on a Wi-Fi network in the application and wants to connect to the network, the following code can be used to implement the connection function:

public class WifiListActivity extends AppCompatActivity {
    private WifiManager wifiManager;
    private List&lt;ScanResult&gt; scanResults;
    private RecyclerView recyclerView;
    private WifiListAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView(.activity_wifi_list);
        // Initialize the Wi-Fi Manager        wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        // Check and turn on Wi-Fi        if (!()) {
            (true);
        }
        // Get the Wi-Fi network list        scanResults = ();
        // Initialize RecyclerView        recyclerView = findViewById(.recycler_view);
        (new LinearLayoutManager(this));
        adapter = new WifiListAdapter(scanResults);
        (adapter);
        // Connect to Wi-Fi network when clicking on the list item        (new () {
            @Override
            public void onItemClick(int position) {
                if (position &gt;= 0 &amp;&amp; position &lt; ()) {
                    ScanResult selectedWifi = (position);
                    connectToWifi(selectedWifi);
                }
            }
        });
    }
    private void connectToWifi(ScanResult selectedWifi) {
        String networkSSID = ;
        String networkBSSID = ;
        
        // Create configuration object        WifiConfiguration wifiConfiguration = new WifiConfiguration();
         = "\"" + networkSSID + "\"";
         = networkBSSID;
         = 1;
        // Add configuration and connect to Wi-Fi        int networkId = (wifiConfiguration);
        
        //Judge connection results        if (networkId == -1) {
            // Connection failed            (this, "Connection failed, please try again", Toast.LENGTH_SHORT).show();
        } else {
            // Disconnect the currently connected Wi-Fi network            ();
            
            // Enable the specified Wi-Fi network            (networkId, true);
            
            // Reconnect to Wi-Fi network            ();
            
            // Connection is successful            (this, "Connected to" + networkSSID, Toast.LENGTH_SHORT).show();
        }
    }
}

In the above example code, the connectToWifi() method is used to connect to the selected Wi-Fi network. In this method, we create a WifiConfiguration object and set the SSID and BSSID required for the connection. We then use the addNetwork() method to add the configuration to the Wi-Fi manager and get the network ID. The continuous steps are to disconnect the currently connected Wi-Fi network, enable the specified Wi-Fi network, and reconnect Wi-Fi. Please note that the operation of connecting to the Wi-Fi network requires the corresponding permissions. If you do not add permissions to the file, the connection may fail. Make sure to add the following permissions to your project:

<uses-permission android:name=".ACCESS_FINE_LOCATION" />
<uses-permission android:name=".ACCESS_COARSE_LOCATION" />
<uses-permission android:name=".ACCESS_WIFI_STATE"/>
<uses-permission android:name=".CHANGE_WIFI_STATE"/>
<uses-permission android:name=""/>

Also, remember to replace the example code.activity_wifi_listFor your layout files and make appropriate modifications according to actual needs. This sample code only implements the basic functions of Wi-Fi network connections, which you can expand and optimize according to actual needs. Hopefully this code can help you achieve Wi-Fi network connection.

in conclusion

By following the above steps, you can get a list of Wi-Fi networks in your Android app. This is very useful for developing applications that require Wi-Fi functionality, such as Wi-Fi scanning, connecting to a specific network, etc. Please note that when using Wi-Fi features, you should follow Wi-Fi-related best practices and security considerations, such as features like passwords, encryption, and authentication in your application.

The above is the detailed content of the process steps for Android to obtain Wi-Fi network list. For more information about Android to obtain Wi-Fi list, please follow my other related articles!