Skip to content Skip to sidebar Skip to footer

Android:- Not Able To Get The Available Wifi List Using Scanresult

I'm trying to list available wifi network by ScanResult. Can anyone tell me what's wrong in my code? I'm not getting the desired output. Here's My code: public class Activity_scanw

Solution 1:

Try this code

WifiList.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife; 


publicclassWifiListextendsActivity {

            WifiManager mWifiManager;
            ArrayList<String> mWifiList;

            @BindView(R.id.lst_wifilist)
            ListView lst_wifilist;

            @BindView(R.id.ll_scan)
            LinearLayout ll_scan;

            @OverrideprotectedvoidonCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_wifilist);
                ButterKnife.bind(this);
                mWifiList = newArrayList<>();
                mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                mWifiList.clear();

                scanNetwork();
    }

    privatevoidscanNetwork() {
            registerReceiver(mWifiScanReceiver,
                    newIntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
            mWifiManager.startScan();
        }

        privatefinalBroadcastReceivermWifiScanReceiver=newBroadcastReceiver() {
            @OverridepublicvoidonReceive(Context c, Intent intent) {
                if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
                    List<ScanResult> mScanResults = mWifiManager.getScanResults();
                    // add your logic here
                    mWifiList.clear();
                    for (inti=0; i < mScanResults.size(); i++) {
                        Log.e("Scanned Wifi Name :- ", mScanResults.get(i).SSID);
                        mWifiList.add(mScanResults.get(i).SSID);

                        WifiListAdapteradapter=newWifiListAdapter(WifiList.this, mWifiList);
                        lst_wifilist.setAdapter(adapter);
                    }
                }
            }
        };
    }

AndroidMenifest.xml

<uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE" /><uses-permissionandroid:name="android.permission.INTERNET" /><uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE"/>

Solution 2:

import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.content.ServiceConnection;
    import android.net.Uri;
    import android.net.wifi.ScanResult;
    import android.net.wifi.WifiManager;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;
    import java.util.List;

    publicclassMainActivityextendsActivity {
    privateStringBuilder sb = newStringBuilder();
    List<ScanResult> scanList;
    TextView textView;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
      textView = (TextView)findViewById(R.id.textView);
      getWifiNetworksList();
    }

    privatevoidgetWifiNetworksList(){
        IntentFilter filter = newIntentFilter();
        filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        final WifiManager wifiManager =
                   (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        wifiManager.startScan();
    registerReceiver(newBroadcastReceiver(){
    @OverridepublicvoidonReceive(Context context, Intent intent) {
        sb = newStringBuilder();
        scanList = wifiManager.getScanResults();
        sb.append("\n  Number Of Wifi connections :" + " "    +scanList.size()+"\n\n");
        for(int i = 0; i < scanList.size(); i++){
        sb.append(newInteger(i+1).toString() + ". ");
        sb.append((scanList.get(i)).toString());
        sb.append("\n\n");
    }
      Toast.makeText(getApplicationContext(),""+sb.toString(),Toast.LENGTH_LONG).show();
       textView.setText(sb);
       }
      },filter);
     }
    }

Manifest:

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

EDIT:enter image description here

Solution 3:

Try this code:

Manifest Permission:

android:name="android.permission.ACCESS_WIFI_STATE" android:name="android.permission.CHANGE_WIFI_STATE"

Activity

publicclassMainActivityextendsActivityimplementsOnClickListener {

    Button setWifi;
    ListView listViwProvider;

    WifiManager wifiManager;
    WifiReceiver receiverWifi;
    List<ScanResult> wifiList;
    List<String> listOfProvider;
    ListAdapter adapter;


    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listOfProvider = newArrayList<String>();
        listViwProvider = (ListView) findViewById(R.id.list_view_wifi);
        setWifi = (Button) findViewById(R.id.btn_wifi);

        setWifi.setOnClickListener(this);
        wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

        if (wifiManager.isWifiEnabled() == true) {
            setWifi.setText("ON");
            scaning();
        }else {
            wifiManager.setWifiEnabled(true);
            setWifi.setText("ON");
            scaning();
        }

        listViwProvider.setOnItemClickListener(newAdapterView.OnItemClickListener() {
            @OverridepublicvoidonItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                    ImportDialog action = newImportDialog(MainActivity.this, (wifiList.get(position)).toString());
                    action.showDialog();
            }
        });
    }

    privatevoidscaning() {
        receiverWifi = newWifiReceiver();
        registerReceiver(receiverWifi, newIntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        wifiManager.startScan();

    }

    @OverridepublicvoidonClick(View arg0) {
        if (wifiManager.isWifiEnabled() == true) {
            wifiManager.setWifiEnabled(false);
            setWifi.setText("OFF");
            listViwProvider.setVisibility(ListView.GONE);
        } 
        elseif (wifiManager.isWifiEnabled() == false) {
            wifiManager.setWifiEnabled(true);
            setWifi.setText("ON");
            listViwProvider.setVisibility(ListView.VISIBLE);
            scaning();
        }
    }

    protectedvoidonPause() {
        super.onPause();
        unregisterReceiver(receiverWifi);
    }

    protectedvoidonResume() {
        registerReceiver(receiverWifi, newIntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        super.onResume();
    }

    classWifiReceiverextendsBroadcastReceiver {

        publicvoidonReceive(Context c, Intent intent) {
            wifiList = wifiManager.getScanResults();
            Collections.sort(wifiList, newComparator<ScanResult>() {
                @Overridepublic int compare(ScanResult lhs, ScanResult rhs) {
                    return (lhs.level > rhs.level ? -1
                            : (lhs.level == rhs.level ? 0 : 1));
                }
            });
            listOfProvider.clear();
            String providerName;
            for (int i = 0; i < wifiList.size(); i++) {
                providerName = (wifiList.get(i).SSID).toString()
                        +"\n"+(wifiList.get(i).BSSID).toString();
                listOfProvider.add(providerName);
            }
            adapter = newListAdapter(MainActivity.this, listOfProvider);
            listViwProvider.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    }
}

Adapter

publicclassListAdapterextendsArrayAdapter<String> {
    Activity mActivity;
    List<String> wifiName;
    private LayoutInflater inflater;

    publicListAdapter(Activity a, List<String> wifiName) {
        super(a, R.layout.single_list, wifiName);
        mActivity = a;
        inflater = LayoutInflater.from(mActivity);
        this.wifiName = wifiName;
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
        convertView = inflater.inflate(R.layout.single_list, parent, false);
        TextViewwifiProvider= (TextView) convertView
                .findViewById(R.id.txt_wifi_provider);
        wifiProvider.setText(wifiName.get(position));
        return convertView;
    }

}

Post a Comment for "Android:- Not Able To Get The Available Wifi List Using Scanresult"