unable to get routers in range in android N

Viewed 47

i want to get all routers in range and show it in list view but facing problem in android n and up. i am able to get all routers and show it in list view in android 5 lollipop and below lollipop but it doesn't works in android N and up. i am doing it in fragments.

Here is my code.

    public class Add_Router extends android.support.v4.app.Fragment {
public Add_Router() {
}

ArrayList<AR_Router> ar_router;
WifiManager mWifiManager;
ListView lv;
ProgressDialog pd;
String adm_token;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frag_addrouter, container, false);
    setHasOptionsMenu(true);
    adm_token = getArguments().getString("adm_token");
    pd = new ProgressDialog(getActivity(), R.style.pdtheme);
    pd.setCancelable(false);
    pd.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
    ar_router = new ArrayList<>();
    lv = (ListView) v.findViewById(R.id.lv);

    //get all wifi devices
    pd.show();
    mWifiManager = (WifiManager) getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    getActivity().registerReceiver(mWifiScanReceiver,
            new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    mWifiManager.startScan();


    return v;
}

private final BroadcastReceiver mWifiScanReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context c, Intent intent) {
        try{
        if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            List<ScanResult> mScanResults = mWifiManager.getScanResults();
            if (!ar_router.isEmpty()) {
                ar_router.clear();
            }
            for (int i = 0; i < mScanResults.size(); i++) {
                String router_name = mScanResults.get(i).SSID.toString();
                String router_mac = mScanResults.get(i).BSSID.toString();
                Log.d("router : ","rout:"+router_name);
                AR_Router router = new AR_Router();
                router.arString_routerName = router_name;
                router.arString_routerMac = router_mac;
                ar_router.add(router);

            }
            CustomAdapter ca = new CustomAdapter(getActivity(), R.layout.cstm_router, R.id.tv_router_Name, ar_router);
            lv.setAdapter(ca);
            pd.dismiss();
        }}catch (Exception e){
            e.printStackTrace();
        }
    }
};

and this is my custom adapter.

     class CustomAdapter extends ArrayAdapter<AR_Router> {
    public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<AR_Router> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View v = inflater.inflate(R.layout.cstm_router, parent, false);
        final TextView router_name = (TextView) v.findViewById(R.id.tv_router_Name);
        final TextView router_mac = (TextView) v.findViewById(R.id.router_mac);

        AR_Router ar_rout = ar_router.get(position);
        router_name.setText(ar_rout.arString_routerName);
        router_mac.setText(ar_rout.arString_routerMac);

        return v;

    }
}

and here is the array class

     //array class
private class AR_Router {
    String arString_routerName;
    String arString_routerMac;
}
1 Answers

try using location ON. Turn your location ON and then try it. Add Permission in manifest file ACCESS_FINE_LOCATION.

Related