Android 如何获取Wifi接入点的连接强度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3437694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:11:31  来源:igfitidea点击:

How to get the connection strength of Wifi access points?

androidwifi

提问by Yin Zhu

I am building an application reading the signal strength of each available Wifi access point.

我正在构建一个应用程序,读取每个可用 Wifi 接入点的信号强度。

I've written code like:

我写过这样的代码:

    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    // Get WiFi status
    WifiInfo info = wifi.getConnectionInfo();
    textStatus.append("\n\nWiFi Status: " + info.toString());

    // List available networks
    List<WifiConfiguration> configs = wifi.getConfiguredNetworks();

However, I have two problems:

但是,我有两个问题:

  1. In debugging, configsonly contains one connection. However, I can see that there are several APs available in the system's wifi setting. I.e. configsis an incomplete list.

  2. I don't know how to get the signal strength in WifiConfiguration.

  1. 在调试时,configs只包含一个连接。但是,我可以看到系统的 wifi 设置中有几个可用的 AP。即configs是一个不完整的列表。

  2. 我不知道如何获得信号强度WifiConfiguration

btw, I am using HTC Hero and Android 1.5.

顺便说一句,我使用的是 HTC Hero 和 Android 1.5。

回答by Sergii Rudchenko

According to the Android API documentation WifiManager.getConfiguredNetworks()does not fill the signal strength paramters. This data only represents the remembered access point settings, not the visible ones.

根据 Android API 文档WifiManager.getConfiguredNetworks()不填充信号强度参数。此数据仅代表记住的接入点设置,而不是可见的设置。

To get actually visible networks you must call WifiManager.startScan()to initiate WiFi radio scanning and WifiManager.getScanResults()after a while to get the scanning results.

要获得实际可见的网络,您必须在一段时间后调用WifiManager.startScan()以启动 WiFi 无线电扫描和WifiManager.getScanResults()以获取扫描结果。

回答by AJit

below code will help to get bar of wifi:

下面的代码将有助于获得 wifi 条:

registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            int state = wifi.getWifiState();
            if(state == WifiManager.WIFI_STATE_ENABLED) {
                List<ScanResult> results = wifi.getScanResults();

                for (ScanResult result : results) {
                    if(result.BSSID.equals(wifi.getConnectionInfo().getBSSID())) {
                        int level = WifiManager.calculateSignalLevel(wifi.getConnectionInfo().getRssi(),
                                result.level);
                        int difference = level * 100 / result.level;
                        int signalStrangth= 0;
                        if(difference >= 100)
                            signalStrangth = 4;
                        else if(difference >= 75)
                            signalStrangth = 3;
                        else if(difference >= 50)
                            signalStrangth = 2;
                        else if(difference >= 25)
                            signalStrangth = 1;
                        tv.setText(tv.getText() + "\nDifference :" + difference + " signal state:" + signalStrangth);

                    }

                }
            }
        }
    }, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));

回答by Asad Rao

mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
receiverWifi = new WifiReceiver();
    registerReceiver(receiverWifi, new IntentFilter(
            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
class WifiReceiver extends BroadcastReceiver {
    public void onReceive(Context c, Intent intent) {

        List<ScanResult> wifiList = mainWifi.getScanResults();
        ArrayList<WifiConnectionBean> m4MessagesList = new ArrayList<WifiConnectionBean>();
        for (int i = 0; i < wifiList.size(); i++) {
            ScanResult scanResult = wifiList.get(i);
            WifiConnectionBean bean = new WifiConnectionBean();
            bean.setConnectionName(scanResult.SSID); // + "--" +
                                                        // scanResult.frequency);
            bean.setDescription(scanResult.capabilities);
            bean.setId(scanResult.SSID);
            bean.setLevel(String.valueOf(scanResult.level));
            bean.setSignalStrength(String.valueOf(scanResult.BSSID));
            m4MessagesList.add(bean);
        }
        if (m4MessagesList == null) {
            Toast.makeText(WifiIdentificationActivity.this,
                    "WifiConnection not available", Toast.LENGTH_SHORT)
                    .show();
        } else {
            String message = "Scanning complete. " + m4MessagesList.size()
                    + " connections found!";
        }
        pd.dismiss();

    }
}

where scanResult.SSID gives the signal strength.

其中 scanResult.SSID 给出了信号强度。

回答by Issac Balaji

private void checkNetworkStrengh() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo Info = cm.getActiveNetworkInfo();
        if (Info == null || !Info.isConnectedOrConnecting()) {
            Log.i(TAG, "No connection");
        } else {
            int netType = Info.getType();
            int netSubtype = Info.getSubtype();

            if (netType == ConnectivityManager.TYPE_WIFI) {
                Log.i(TAG, "Wifi connection");
                WifiManager wifiManager = (WifiManager) getApplication().getSystemService(Context.WIFI_SERVICE);
                List<ScanResult> scanResult = wifiManager.getScanResults();
                for (int i = 0; i < scanResult.size(); i++) {
                    Log.d("scanResult", "Speed of wifi"+scanResult.get(i).level);//The db level of signal 
                }


                // Need to get wifi strength
            } else if (netType == ConnectivityManager.TYPE_MOBILE) {
                Log.i(TAG, "GPRS/3G connection");
                // Need to get differentiate between 3G/GPRS
            }
        }
    }

回答by arshad shaikh

    WifiManager wifiManager = (WifiManager)
    MonitorActivity.this.getSystemService(Context.WIFI_SERVICE);

    wifiManager.addNetwork(conf);

    int numberOfLevels = 5;
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);

    Log.e("level", "" + level);
    // you will get the levels. Using these levels you can calculate strenghts.