Changing WiFi network on Android

Hi! I need to change my WiFi network in my app, but I don’t know how. Can you tell me how? If I haven’t searched enough, it’s okay if you let me know. Thanks!

@KTibow ====

though i think i have already answered to that here
that is easy: you use the WifiManager API
create an instance of it
list all wifiSpots
put them in a list
disconnect from all
connect to what you want
(of course you need the wep code)

Can you give me an example?

I’m pinging you, @akenaton. Can you give me an example?

@KTibow ===
example here for scanning:
https://developer.android.com/guide/topics/connectivity/wifi-scan
dont forget permissions (3)

Thanks! Do you know what libraries to import, @akenaton?

@KTibow ===

import java.util.List;/// FOR LISTING
import android.content.Context;// for creating the WIFIM instance
import android.content.BroadcastReceiver;////for creating the receiver
import android.content.Intent;///for adding an intent to it
import android.content.IntentFilter;///// for filtering the receipts
import android.net.wifi.ScanResult;/// for scanning
import android.net.wifi.WifiManager;///FIRSTLY
import android.app.Activity;/// FRAGMENT
import android.net.wifi.WifiConfiguration.KeyMgmt;/// For the key WEP
import android.net.wifi.WifiConfiguration;///for creating a new wifi config
1 Like

Thanks for sharing!! I don’t know much about this. You may refer any wifi guide. It might be helpful to you. Looking for some more responses.

I don’t understand if you have still problems with the listing, bur here is my code.

import java.util.List;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;


// This example scans for available access points and ad hoc networks. 

WifiManager wifi;
WifiScanReceiver wifiReceiver;
Activity act;
Context mC;


@Override
  void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  act = this.getActivity();
  mC= act.getApplicationContext();
  IntentFilter intentFilter = new IntentFilter();
  intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
  wifiReceiver = new WifiScanReceiver();
  mC.registerReceiver(wifiReceiver, intentFilter);
  wifi = (WifiManager) act.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  wifi.startScan();
}

class WifiScanReceiver extends BroadcastReceiver {
  public void onReceive(Context c, Intent intent) {
    List<ScanResult> wifiScanList = wifi.getScanResults();
    for (int i = 0; i < wifiScanList.size(); i++) {
      String info = ((wifiScanList.get(i)).toString());
      println(info+"\n");
    }
  }
}