简体   繁体   English

Wifi扫描器不适用于android牛轧糖(7.0)

[英]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). 我想在wifi开启时扫描所有wifi,但不适用于android 7.0,并且我只有这部手机进行测试(我认为这是android版本的问题)。

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. 对于Android 7,必须具有权限ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATION才能获取Wi-Fi扫描结果。 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. 除了启用WiFi,还必须启用定位服务才能访问扫描结果。 If you only have the WiFi enabled, you'll get an empty scan results list. 如果仅启用WiFi,则将获得一个空白的扫描结果列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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