I want to design an app which shows a list of wifi networks available and connect to the network when it is selected. I have implemented the part showing the scan results. Now i want to connect to a particular network selected by the user from the list of scan results. Can anyone please tell me how to do this?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(R.id.listView);
adapter = new ArrayAdapter
(this, android.R.layout.simple_list_item_1);
listview.setAdapter(adapter);
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WiFiScanReceiver();
}
public void onToggleClicked(View view) {
adapter.clear();
ToggleButton toggleButton = (ToggleButton) view;
if (wifiManager == null) {
// Device does not support Wi-Fi
Toast.makeText(getApplicationContext(), "Oop! Your device does not support Wi-Fi",
Toast.LENGTH_SHORT).show();
toggleButton.setChecked(false);
} else {
if (toggleButton.isChecked()) { // To turn on Wi-Fi
if (!wifiManager.isWifiEnabled()) {
Toast.makeText(getApplicationContext(), "Wi-Fi is turned on." +
"\n" + "Scanning for access points...",
Toast.LENGTH_SHORT).show();
wifiManager.setWifiEnabled(true);
} else {
Toast.makeText(getApplicationContext(), "Wi-Fi is already turned on." +
"\n" + "Scanning for access points...",
Toast.LENGTH_SHORT).show();
}
wifiManager.startScan();
} else { // To turn off Wi-Fi
Toast.makeText(getApplicationContext(), "Wi-Fi is turned off.",
Toast.LENGTH_SHORT).show();
wifiManager.setWifiEnabled(false);
}
}
}
class WiFiScanReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
// wifiScanResult = wifiManager.getScanResults();
if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
List<ScanResult> wifiScanResultList = wifiManager.getScanResults();
for (int i = 0; i < wifiScanResultList.size(); i++) {
ScanResult accessPoint = wifiScanResultList.get(i);
//wifiScanList[i] = wifiScanResultList.get(i).SSID;
String listItem = " Name: " + accessPoint.SSID + " \n Mac: " + accessPoint.BSSID + "\n Signal Strenght: " + accessPoint.level+ "dBm";
adapter.add(listItem);
// }
// }
//listview.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, wifiScanList));
}
}
}
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(wifiReciever);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.wi_fi, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}