开发者

How to get the connection strength of Wifi access points?

开发者 https://www.devze.com 2023-01-10 14:29 出处:网络
I am building an application reading the signal strength of each available Wifi access point. I\'ve written code like:

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

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, configs onl开发者_运维技巧y contains one connection. However, I can see that there are several APs available in the system's wifi setting. I.e. configs is an incomplete list.

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

btw, I am using HTC Hero and Android 1.5.


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.

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.


below code will help to get bar of 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));


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.


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
            }
        }
    }


    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.
0

精彩评论

暂无评论...
验证码 换一张
取 消