Bertrand Jul
Bertrand Jul

Reputation: 1

Wifi Scanner doesn't work with android nougat (7.0)

I want to scan all wifi when the wifi is turning on but it doesn't work with android 7.0 and I have only this phone for test (I think it's a android version problem).

How can I fix my problem?

Code :

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.WifiManager;
import android.os.Bundle; 
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



public class DetectWifi extends Activity implements View.OnClickListener
{
  WifiManager wifi;
  ListView lv;
  TextView textStatus;
  Button buttonScan;
  int size = 0;
  List<ScanResult> results;

  String ITEM_KEY = "key";
  ArrayList<HashMap<String, String>> arraylist = new   ArrayList<HashMap<String, String>>();
  SimpleAdapter adapter;

  /* Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detectwifi_page);

    textStatus = (TextView) findViewById(R.id.textStatus);
    buttonScan = (Button) findViewById(R.id.buttonScan);
    buttonScan.setOnClickListener(this);
    lv = (ListView)findViewById(R.id.list);

    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    if (wifi.isWifiEnabled() == false)
    {
        Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
        wifi.setWifiEnabled(true);
    }
    this.adapter = new SimpleAdapter(DetectWifi.this, arraylist, R.layout.dialog_wifi, new String[] { ITEM_KEY }, new int[] { R.id.list_value });
    lv.setAdapter(this.adapter);

    registerReceiver(new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context c, Intent intent)
        {
            results = wifi.getScanResults();
            size = results.size();
        }
    }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}

public void onClick(View view)
{
    arraylist.clear();
    wifi.startScan();

    Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show();
    try
    {
        size = size - 1;
        while (size >= 0)
        {
            HashMap<String, String> item = new HashMap<String, String>();
            item.put(ITEM_KEY, results.get(size).SSID + "  " + results.get(size).capabilities);

            arraylist.add(item);
            size--;
            adapter.notifyDataSetChanged();
        }
    }
    catch (Exception e)
    { }
 }
}

Thanks a lot.

Upvotes: 0

Views: 2033

Answers (2)

TheRealChx101
TheRealChx101

Reputation: 1544

In addition to having the WiFi enabled, you also must have location services enabled in order to access the scan results. If you only have the WiFi enabled, you'll get an empty scan results list.

Upvotes: 1

Oleg
Oleg

Reputation: 97

For Android 7 the permissions ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION are neccessary to get the Wi-Fi scan results. You also have to turn on location at device.

Upvotes: 1

Related Questions