0

I am developing an android app where i need to scan the available Wi-Fi network and connect with a specific Wi-fi.I have written the code for it. But I dont get the list of network. It would be very helpful if anyone can help me out with it.

package com.example.wifilist;

import android.Manifest;    
import android.annotation.SuppressLint;import android.app.Dialog;import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends ListActivity {

    WifiManager mainWifiObj;

    WifiScanReceiver wifiReciever;

    ListView list;

    String wifis[];


    EditText pass;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        list=getListView();

        mainWifiObj (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        wifiReciever = new WifiScanReceiver();

        mainWifiObj.startScan();


        // listening to single list item on click

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                // selected item

                String ssid = ((TextView) view).getText().toString();

                connectToWifi(ssid);

                Toast.makeText(MainActivity.this,"Wifi SSID : "+ssid,Toast.LENGTH_SHORT).show();


            }

        });

    }


    protected void onPause() {

        unregisterReceiver(wifiReciever);

        super.onPause();

    }


    protected void onResume() {

        registerReceiver(wifiReciever,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

        super.onResume();

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)

        {

            if(checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED)

            {

                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},87);

            }

        }

    }

    class WifiScanReceiver extends BroadcastReceiver {

        @SuppressLint("UseValueOf")

        public void onReceive(Context c, Intent intent) {

            if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {

                List<ScanResult> wifiScanList = mainWifiObj.getScanResults();

                wifis = new String[wifiScanList.size()];

                for (int i = 0; i < wifiScanList.size(); i++) {

                    wifis[i] = ((wifiScanList.get(i)).toString());

                }

                String filtered[] = new String[wifiScanList.size()];

                int counter = 0;

                for (String eachWifi : wifis) {

                    String[] temp = eachWifi.split(",");


                    filtered[counter] = temp[0].substring(5).trim();//+"\n" + temp[2].substring(12).trim()+"\n" +temp[3].substring(6).trim();//0->SSID, 2->Key Management 3-> Strength


                    counter++;


                }

                list.setAdapter(new ArrayAdapter<String>(getApplicationContext() ,R.layout.list_item, R.id.label, filtered));

            }


        }

    }


    private void finallyConnect(String networkPass, String networkSSID) {

        WifiConfiguration wifiConfig = new WifiConfiguration();

        wifiConfig.SSID = String.format("\"%s\"", networkSSID);

        wifiConfig.preSharedKey = String.format("\"%s\"", networkPass);


        // remember id

        int netId = mainWifiObj.addNetwork(wifiConfig);

        mainWifiObj.disconnect();

        mainWifiObj.enableNetwork(netId, true);

        mainWifiObj.reconnect();


        WifiConfiguration conf = new WifiConfiguration();

        conf.SSID = "\"\"" + networkSSID + "\"\"";

        conf.preSharedKey = "\"" + networkPass + "\"";

        mainWifiObj.addNetwork(conf);

    }

    private void connectToWifi(final String wifiSSID) {

        final Dialog dialog = new Dialog(this);

        dialog.setContentView(R.layout.connect);

        dialog.setTitle("Connect to Network");

        TextView textSSID = (TextView) dialog.findViewById(R.id.textSSID1);


        Button dialogButton = (Button) dialog.findViewById(R.id.okButton);

        pass = (EditText) dialog.findViewById(R.id.textPassword);

        textSSID.setText(wifiSSID);


        // if button is clicked, connect to the network;

        dialogButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                String checkPassword = pass.getText().toString();

                finallyConnect(checkPassword, wifiSSID);

                dialog.dismiss();

            }

        });

        dialog.show();

    }

}
Tom
  • 4,070
  • 4
  • 22
  • 50

1 Answers1

0
  1. WifiManager.startScan() has been deprecated in API level 29 and will be removed.

  2. WifiManager.getScanResults()requires the ACCESS_FINE_LOCATION permission. You only request the ACCESS_COARSE_LOCATION permission

  3. The ability to directly connect to a wifi network has also been deprecated and is not possible with targetSDK set to >= 29.

Alexander Hoffmann
  • 5,104
  • 3
  • 17
  • 23