如何获得可用的wifi网络并将其显示在android中的列表中

时间:2013-09-11 12:19:53

标签: android android-listview android-wifi wifimanager

朋友们,我想找到所有可用的WiFi网络并将其显示在我尝试过的列表中,如下所示。但它不起作用。我已经编辑了我的代码,现在我得到了结果但是得到了我不需要的所有结果。我只需要列表中的wifi网络名称。

public class MainActivity extends Activity {

    TextView mainText;
    WifiManager mainWifi;
    WifiReceiver receiverWifi;
    List<ScanResult> wifiList;
    StringBuilder sb = new StringBuilder();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainText = (TextView) findViewById(R.id.tv1);
        mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        if (mainWifi.isWifiEnabled() == false)
        {  
             // If wifi disabled then enable it
             Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled",
             Toast.LENGTH_LONG).show();
             mainWifi.setWifiEnabled(true);
         } 

         receiverWifi = new WifiReceiver();
         registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
         mainWifi.startScan();
         mainText.setText("Starting Scan...");

     }

     public boolean onCreateOptionsMenu(Menu menu) {
            menu.add(0, 0, 0, "Refresh");
            return super.onCreateOptionsMenu(menu);
     }

     public boolean onMenuItemSelected(int featureId, MenuItem item) {
            mainWifi.startScan();
            mainText.setText("Starting Scan");
            return super.onMenuItemSelected(featureId, item);
     }

     protected void onPause() {
            unregisterReceiver(receiverWifi);
            super.onPause();
     }

     protected void onResume() {
            registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
            super.onResume();
     }

        // Broadcast receiver class called its receive method
        // when number of wifi connections changed

      class WifiReceiver extends BroadcastReceiver {

            // This method call when number of wifi connections changed
            public void onReceive(Context c, Intent intent) {

                sb = new StringBuilder();
                wifiList = mainWifi.getScanResults();
                sb.append("\n        Number Of Wifi connections :"+wifiList.size()+"\n\n");

                for(int i = 0; i < wifiList.size(); i++){

                    sb.append(new Integer(i+1).toString() + ". ");
                    sb.append((wifiList.get(i)).toString());
                    sb.append("\n\n");
                }

                mainText.setText(sb); 
            }

       }
}

3 个答案:

答案 0 :(得分:37)

您需要创建一个BroadcastReceiver来收听Wifi扫描结果:

private final BroadcastReceiver mWifiScanReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context c, Intent intent) {
        if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            List<ScanResult> mScanResults = mWifiManager.getScanResults();
            // add your logic here
        }
    }
}

onCreate()中,您将分配mWifiManager并启动扫描:

mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
registerReceiver(mWifiScanReceiver,
        new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mWifiManager.startScan();
仅当您具有适当的权限时,

getScanResults()才会返回数据。为此,请将以下两行之一添加到AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

另请注意,在API 23+中,必须在运行时请求权限。 (对于实验室环境,您还可以在“设置”中手动授予权限,而不需要编码,但不建议最终用户应用。)

请注意,每次有新的扫描结果可用时,处理扫描结果的代码都会运行,从而更新结果。

答案 1 :(得分:7)

Android 6.0之后

如果您的Android操作系统版本 6.0或更高,则您的应用程序必须在运行时请求以下权限(以下任一项)。

@Override
public void onResume()
{
    super.onResume();

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        if(checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 87);
        }
    }
}

如何在运行时询问权限

只需在活动的onResume方法中添加此代码

即可
wifiManager.startScan();

如果您在运行时没有获得上述许可,会发生什么?

  

Wifi.getScanResults()将返回0结果。

包括我在内的很多人在Nexus 5手机中遇到过这个问题,并将其称为“#34; bug&#34;。

如何扫描和阅读wifi网络? (更深入的理解)

可以通过

请求扫描
public class WifiScanReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
        {
            List<ScanResult> scanResults = wifimanager.getScanResults();
            // Write your logic to show in the list
        }

    }
}
  

boolean startScan()返回   根据扫描是否已开始的事实立即生效或错误   成功与否。
  但是它启动了一个异步事件   发送意图( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION )时   扫描完成。由于扫描结果(异步事件结果)不会立即可用,您必须使用BroadcastReceiver注册您的活动。

以下是使用BroadcastReceiver读取这些结果(异步)的代码段。

typealias PlacesCompletion = ([GooglePlace]) -> Void
typealias PhotoCompletion = (UIImage?) -> Void


    class GoogleDataProvider {
        private var photoCache: [String: UIImage] = [:]
        private var placesTask: URLSessionDataTask?
        private var session: URLSession {
            return URLSession.shared
        }

        let appDelegate = UIApplication.shared.delegate as! AppDelegate

        func fetchPlacesNearCoordinate(_ coordinate: CLLocationCoordinate2D, radius: Double, types:[String], completion: @escaping PlacesCompletion) -> Void {
            var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(radius)&rankby=prominence&sensor=true&key=\(appDelegate.APP_ID)"

            let typesString = types.count > 0 ? types.joined(separator: "|") : "food"
            urlString += "&types=\(typesString)"
            urlString = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ?? urlString

            guard let url = URL(string: urlString) else {
                completion([])
                return
            }

            if let task = placesTask, task.taskIdentifier > 0 && task.state == .running {
                task.cancel()
            }

            DispatchQueue.main.async {
                UIApplication.shared.isNetworkActivityIndicatorVisible = true
            }

            placesTask = session.dataTask(with: url) { data, response, error in
                var placesArray: [GooglePlace] = []
                defer {
                    DispatchQueue.main.async {
                        UIApplication.shared.isNetworkActivityIndicatorVisible = false
                        completion(placesArray)
                    }
                }
                guard let data = data,
                    let json = try? JSON(data: data, options: .mutableContainers),
                    let results = json["results"].arrayObject as? [[String: Any]] else {
                        return
                }
                results.forEach {
                    let place = GooglePlace(dictionary: $0, acceptedTypes: types)
                    placesArray.append(place)
                    if let reference = place.photoReference {
                        self.fetchPhotoFromReference(reference) { image in
                            place.photo = image
                        }
                    }
                }
            }
            placesTask?.resume()
        }


        func fetchPhotoFromReference(_ reference: String, completion: @escaping PhotoCompletion) -> Void {
            if let photo = photoCache[reference] {
                completion(photo)
            } else {
                let urlString = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=200&photoreference=\(reference)&key=\(appDelegate.APP_ID)"
                guard let url = URL(string: urlString) else {
                    completion(nil)
                    return
                }

                DispatchQueue.main.async {
                    UIApplication.shared.isNetworkActivityIndicatorVisible = true
                }

                session.downloadTask(with: url) { url, response, error in
                    var downloadedPhoto: UIImage? = nil
                    defer {
                        DispatchQueue.main.async {
                            UIApplication.shared.isNetworkActivityIndicatorVisible = false
                            completion(downloadedPhoto)
                        }
                    }
                    guard let url = url else {
                        return
                    }
                    guard let imageData = try? Data(contentsOf: url) else {
                        return
                    }
                    downloadedPhoto = UIImage(data: imageData)
                    self.photoCache[reference] = downloadedPhoto
                    }
                    .resume()
            }
        }
    }

You can see this demo on BroadcastReceiver

相关链接

Scan result returns an empty list in Android 6.0

答案 2 :(得分:6)

  class WifiReceiver extends BroadcastReceiver {

       public void onReceive(Context c, Intent intent) {

          sb = new StringBuilder();
          wifiList = mainWifi.getScanResults();

            for (int i = 0; i < wifiList.size(); i++){
                sb.append(new Integer(i+1).toString() + ".");
                sb.append((wifiList.get(i)).SSID);
                sb.append("\n");
            }

          mainText.setText(sb);

        }

  }