简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM