Java Code Examples for android.net.wifi.WifiManager#calculateSignalLevel()

The following examples show how to use android.net.wifi.WifiManager#calculateSignalLevel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HUDFragment.java    From RobotCA with GNU General Public License v3.0 6 votes vote down vote up
/**
         * Run
         */
        @Override
        public void run() {
            alive = true;

            int rssi, temp;

            while (alive) {

//                Log.d(TAG, "RSSI: " + wifiManager.getConnectionInfo().getRssi());

                rssi = wifiManager.getConnectionInfo().getRssi();
                temp = lastWifiImage;
                lastWifiImage = WifiManager.calculateSignalLevel(rssi, 5);

                if (temp != lastWifiImage)
                    view.post(UPDATE_UI_RUNNABLE);

                try {
                    Thread.sleep(SLEEP);
                }
                catch (InterruptedException e) {
                    // Ignore
                }
            }
        }
 
Example 2
Source File: TaskbarController.java    From Taskbar with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.M)
private Drawable getWifiDrawable() {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo ethernet = manager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
    if(ethernet != null && ethernet.isConnected())
        return getDrawableForSysTray(R.drawable.tb_settings_ethernet);

    NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if(wifi == null || !wifi.isConnected())
        return null;

    WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    int numberOfLevels = 5;

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

    String wifiRes = "tb_signal_wifi_" + level + "_bar";
    int id = getResourceIdFor(wifiRes);

    return getDrawableForSysTray(id);
}
 
Example 3
Source File: HubConnectivityAndPowerFragment.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
private void setWiFiSignalStrenthImage(@IntRange(from = 0, to = 100) int rssi) {
    int level = WifiManager.calculateSignalLevel(rssi, 5) + 1;
    Context context = getContext();
    if (context != null) {
        String packageName = context.getApplicationContext().getPackageName();
        String drawableName = "wifi_white_" + level + "_24x20";
        int id = getResources().getIdentifier(drawableName,"drawable", packageName);

         wifiSignalStrengthImageTV.setCompoundDrawablesWithIntrinsicBounds(
                id,
                0,
                0,
                0
        );
    }
}
 
Example 4
Source File: ConnectionBuddy.java    From android_connectionbuddy with Apache License 2.0 6 votes vote down vote up
/**
 * Get WiFi signal strength.
 */
private ConnectivityStrength getWifiStrength() {
    WifiManager wifiManager = (WifiManager) configuration.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();

    if (wifiInfo != null) {
        int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(),
            ConnectionBuddyConfiguration.SIGNAL_STRENGTH_NUMBER_OF_LEVELS);

        switch (level) {
            case 0:
                return new ConnectivityStrength(ConnectivityStrength.POOR);
            case 1:
                return new ConnectivityStrength(ConnectivityStrength.GOOD);
            case 2:
                return new ConnectivityStrength(ConnectivityStrength.EXCELLENT);
            default:
                return new ConnectivityStrength(ConnectivityStrength.UNDEFINED);
        }
    } else {
        return new ConnectivityStrength(ConnectivityStrength.UNDEFINED);
    }
}
 
Example 5
Source File: SignalColor.java    From jaws with GNU General Public License v3.0 6 votes vote down vote up
public static int getColor(int signal) {

        /* in case the network is out of range */
        if(signal == 0) {
            return PASTEL_RED_DARK;
        }

        int signalPercent = WifiManager.calculateSignalLevel(signal, 5);

        /* carefully chosen pastel colors */
        switch (signalPercent) {
            case 3:  return BOTTLE_GREEN;
            case 2:  return PASTEL_ORANGE;
            case 1:  return PASTEL_RED;
            case 0:  return PASTEL_RED_DARK;
            default: return BOTTLE_GREEN;
        }
    }
 
Example 6
Source File: WifiDrawer.java    From meter with Apache License 2.0 5 votes vote down vote up
/**
 * Parse wifi strength from intent
 */
public float getWifiStrength(Intent intent){
    float level = intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, -1);
    level = WifiManager.calculateSignalLevel((int) level, 100);
    level /= 100.0;
    return level;
}
 
Example 7
Source File: NetworkManagerActivity.java    From privacypolice with GNU General Public License v2.0 5 votes vote down vote up
public void setSignalStrength(int rssi) {
    if (rssi < -999) {
        signalStrength = -1;
    } else {
        // Map the RSSI value to an integer in the range [0,4]
        signalStrength = WifiManager.calculateSignalLevel(rssi, 5);
    }
}
 
Example 8
Source File: StatusUpdateService.java    From deskcon-android with GNU General Public License v3.0 5 votes vote down vote up
private int getWifiStrength() {
       try {
     	   WifiManager wifiManager = (WifiManager) getApplicationContext()
          		.getSystemService(Context.WIFI_SERVICE);
           int rssi = wifiManager.getConnectionInfo().getRssi();
           int level = WifiManager.calculateSignalLevel(rssi, 10);
           int percentage = (int) ((level/10.0)*100);
           return percentage;
       }
       catch (Exception e) {
          return -1;
       }
}
 
Example 9
Source File: Wifi.java    From Multiwii-Remote with Apache License 2.0 5 votes vote down vote up
@Override
public int getStrength() {
      try {
         int rssi = wifiManager.getConnectionInfo().getRssi();
         int level = WifiManager.calculateSignalLevel(rssi, 10);
         int percentage = (int) ((level / 10.0) * 100);
         Log.d("aaa", "WiFI RSSI=" + String.valueOf(percentage));
         return percentage;

      } catch (Exception e) {
         return 0;
      }
   }
 
Example 10
Source File: TitleView.java    From android-tv-launcher with MIT License 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	WifiManager wifiManager = (WifiManager) context
			.getSystemService(context.WIFI_SERVICE);
	WifiInfo wifiInfo = wifiManager.getConnectionInfo();
	if (wifiInfo.getBSSID() != null) {
		// wifi信号强度
		int signalLevel = WifiManager.calculateSignalLevel(
				wifiInfo.getRssi(), 4);
		if (signalLevel == 0) {
			imgNetWorkState.setImageDrawable(context.getResources()
					.getDrawable(R.drawable.wifi_1));

		} else if (signalLevel == 1) {
			imgNetWorkState.setImageDrawable(context.getResources()
					.getDrawable(R.drawable.wifi_2));

		} else if (signalLevel == 2) {
			imgNetWorkState.setImageDrawable(context.getResources()
					.getDrawable(R.drawable.wifi_3));

		} else if (signalLevel == 3) {
			imgNetWorkState.setImageDrawable(context.getResources()
					.getDrawable(R.drawable.networkstate_on));
		}
		if (d)
			Toast.makeText(context, "wifi level" + signalLevel,
					Toast.LENGTH_SHORT).show();
	}
}
 
Example 11
Source File: GameTitleView.java    From android-tv-launcher with MIT License 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    WifiManager wifiManager = (WifiManager) context
            .getSystemService(context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo.getBSSID() != null) {
        // wifi信号强度
        int signalLevel = WifiManager.calculateSignalLevel(
                wifiInfo.getRssi(), 4);
        if (signalLevel == 0) {
            imgNetWorkState.setImageDrawable(context.getResources()
                    .getDrawable(R.drawable.wifi_1));
        } else if (signalLevel == 1) {
            imgNetWorkState.setImageDrawable(context.getResources()
                    .getDrawable(R.drawable.wifi_2));

        } else if (signalLevel == 2) {
            imgNetWorkState.setImageDrawable(context.getResources()
                    .getDrawable(R.drawable.wifi_3));

        } else if (signalLevel == 3) {
            imgNetWorkState.setImageDrawable(context.getResources()
                    .getDrawable(R.drawable.networkstate_on));
        }
        if (d)
            Toast.makeText(context, "wifi level" + signalLevel,
                    Toast.LENGTH_SHORT).show();
    }
}
 
Example 12
Source File: WifiIconData.java    From Status with Apache License 2.0 5 votes vote down vote up
@Override
public void register() {
    super.register();

    int level = WifiManager.calculateSignalLevel(wifiManager.getConnectionInfo().getRssi(), 5);
    if (level > 0)
        onIconUpdate(level); //temporary fix, cannot determine if wifi is enabled without BroadcastReceiver for some reason
}
 
Example 13
Source File: NetworkStatus.java    From FriendlyDemo with Apache License 2.0 4 votes vote down vote up
private boolean isOnGoodWIFIConnection(@NonNull NetworkInfo netInfo) {
    return (netInfo.getType() == TYPE_WIFI)
            && (WifiManager.calculateSignalLevel(wifiManager.getConnectionInfo().getRssi(), 4) > 0);
}
 
Example 14
Source File: HomeActivity.java    From apollo-DuerOS with Apache License 2.0 4 votes vote down vote up
private void updateNetworkStatus() {
    NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
    if (info != null) {
        LogUtil.i(TAG, "getTypeName(): " + info.getTypeName());
        LogUtil.i(TAG, "getSubtypeName(): " + info.getSubtypeName());
        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            if (mWifiManager.getConnectionInfo() != null) {
                int rssi = mWifiManager.getConnectionInfo().getRssi();
                int level = WifiManager.calculateSignalLevel(rssi, 4);
                switch (level) {
                    case 0:
                        mNetworkImg.setImageResource(R.drawable.wifi4);
                        break;
                    case 1:
                        mNetworkImg.setImageResource(R.drawable.wifi3);
                        break;
                    case 2:
                        mNetworkImg.setImageResource(R.drawable.wifi2);
                        break;
                    case 3:
                        mNetworkImg.setImageResource(R.drawable.wifi);
                        break;
                    default:
                        break;
                }
                LogUtil.i(TAG, "Wifi Level " + level);
            }
        } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            switch (info.getSubtype()) {
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN:
                    mNetworkImg.setImageResource(R.drawable.net_e);
                    break;
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B:
                case TelephonyManager.NETWORK_TYPE_EHRPD:
                case TelephonyManager.NETWORK_TYPE_HSPAP:
                    mNetworkImg.setImageResource(R.drawable.net_3g);
                    break;
                case TelephonyManager.NETWORK_TYPE_LTE:
                    mNetworkImg.setImageResource(R.drawable.net_4g);
                    break;
                default:
                    mNetworkImg.setImageResource(R.drawable.net_none);
                    break;
            }
        }
    } else {
        mNetworkImg.setImageResource(R.drawable.net_none);
    }
}
 
Example 15
Source File: LocationUtil.java    From wifi_backend with GNU General Public License v3.0 4 votes vote down vote up
public static Location weightedAverage(String source, Collection<Location> locations) {
    Location rslt;

    if (locations == null || locations.size() == 0) {
        return null;
    }

    int num = locations.size();
    double totalWeight = 0;
    double latitude = 0;
    double longitude = 0;
    double accuracy = 0;

    // For variance calculation
    double m2_lat = 0;
    double m2_lon = 0;

    int samples = 0;

    for (Location value : locations) {
        if (value != null) {
            samples++;

            // We weight our average based on a linear value based on signal strength.
            int dBm = value.getExtras().getInt(Configuration.EXTRA_SIGNAL_LEVEL);
            double wgt = (WifiManager.calculateSignalLevel(dBm, 100)+1)/100.0;

            if (DEBUG) {
                Log.i(TAG,
                        String.format("Using with weight=%f signal=%d accuracy=%f " +
                                        "latitude=%f longitude=%f",
                                wgt,
                                value.getExtras().getInt(Configuration.EXTRA_SIGNAL_LEVEL),
                                value.getAccuracy(), value.getLatitude(),
                                value.getLongitude()));
            }

            double temp = totalWeight + wgt;
            double delta = value.getLatitude() - latitude;
            double rVal = delta * wgt / temp;

            if (DEBUG) {
                Log.i(TAG, "lat: delta="+delta+", R="+rVal);
            }

            latitude += rVal;
            m2_lat += totalWeight * delta * rVal;

            delta = value.getLongitude() - longitude;
            rVal = delta * wgt / temp;

            if (DEBUG) {
                Log.i(TAG, "lon: delta="+delta+", R="+rVal);
            }

            longitude += rVal;
            m2_lon += totalWeight * delta * rVal;

            double thisAcc = value.getAccuracy();
            accuracy += thisAcc * thisAcc;

            if (DEBUG) {
                Log.i(TAG, "accuracy="+Math.sqrt(accuracy)/samples);
            }

            totalWeight = temp;

            if (DEBUG) {
                Log.i(TAG, "Location est (lat="+ latitude + ", m2="+m2_lat+"), " +
                        "(lon="+ latitude + ", m2="+m2_lon+"), " +
                        "(acc="+ accuracy + ")");
                Log.i(TAG, "wgt="+wgt+", totalWeight="+totalWeight);
            }
        }
    }

    accuracy = Math.sqrt(accuracy)/samples;
    Bundle extras = new Bundle();
    extras.putInt("AVERAGED_OF", num);

    if (DEBUG) {
        Log.i(TAG, "Final Location is (lat="+ latitude + ", lng=" + longitude + ", acc=" + accuracy+")");
    }

    rslt = LocationHelper.create(source, latitude, longitude, (float) accuracy, extras);

    rslt.setTime(System.currentTimeMillis());

    if (DEBUG) {
        Log.i(TAG, rslt.toString());
    }

    return rslt;
}
 
Example 16
Source File: HomeActivity.java    From apollo-DuerOS with Apache License 2.0 4 votes vote down vote up
private void updateNetworkStatus() {
    NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
    if (info != null) {
        LogUtil.i(TAG, "getTypeName(): " + info.getTypeName());
        LogUtil.i(TAG, "getSubtypeName(): " + info.getSubtypeName());
        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            if (mWifiManager.getConnectionInfo() != null) {
                int rssi = mWifiManager.getConnectionInfo().getRssi();
                int level = WifiManager.calculateSignalLevel(rssi, 4);
                switch (level) {
                    case 0:
                        mNetworkImg.setImageResource(R.drawable.ic_wifi4);
                        break;
                    case 1:
                        mNetworkImg.setImageResource(R.drawable.ic_wifi3);
                        break;
                    case 2:
                        mNetworkImg.setImageResource(R.drawable.ic_wifi2);
                        break;
                    case 3:
                        mNetworkImg.setImageResource(R.drawable.ic_wifi);
                        break;
                    default:
                        break;
                }
                LogUtil.i(TAG, "Wifi Level " + level);
            }
        } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            switch (info.getSubtype()) {
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN:
                    mNetworkImg.setImageResource(R.drawable.ic_net_e);
                    break;
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B:
                case TelephonyManager.NETWORK_TYPE_EHRPD:
                case TelephonyManager.NETWORK_TYPE_HSPAP:
                    mNetworkImg.setImageResource(R.drawable.ic_net_3g);
                    break;
                case TelephonyManager.NETWORK_TYPE_LTE:
                    mNetworkImg.setImageResource(R.drawable.ic_net_4g);
                    break;
                default:
                    mNetworkImg.setImageResource(R.drawable.ic_net_none);
                    break;
            }
        }
    } else {
        mNetworkImg.setImageResource(R.drawable.ic_net_none);
    }
}
 
Example 17
Source File: HomeActivity.java    From apollo-DuerOS with Apache License 2.0 4 votes vote down vote up
private void updateNetworkStatus() {
    NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
    if (info != null) {
        LogUtil.i(TAG, "network type name: " + info.getTypeName());
        LogUtil.i(TAG, "network subType name: " + info.getSubtypeName());
        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            if (mWifiManager.getConnectionInfo() != null) {
                int rssi = mWifiManager.getConnectionInfo().getRssi();
                int level = WifiManager.calculateSignalLevel(rssi, 4);
                switch (level) {
                    case 0:
                        mNetworkImg.setImageResource(R.drawable.ic_wifi4);
                        break;
                    case 1:
                        mNetworkImg.setImageResource(R.drawable.ic_wifi3);
                        break;
                    case 2:
                        mNetworkImg.setImageResource(R.drawable.ic_wifi2);
                        break;
                    case 3:
                        mNetworkImg.setImageResource(R.drawable.ic_wifi);
                        break;
                    default:
                        break;
                }
                LogUtil.i(TAG, "Wifi Level " + level);
            }
        } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            switch (info.getSubtype()) {
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN:
                    mNetworkImg.setImageResource(R.drawable.ic_net_e);
                    break;
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B:
                case TelephonyManager.NETWORK_TYPE_EHRPD:
                case TelephonyManager.NETWORK_TYPE_HSPAP:
                    mNetworkImg.setImageResource(R.drawable.ic_net_3g);
                    break;
                case TelephonyManager.NETWORK_TYPE_LTE:
                    mNetworkImg.setImageResource(R.drawable.ic_net_4g);
                    break;
                default:
                    mNetworkImg.setImageResource(R.drawable.ic_net_none);
                    break;
            }
        }
    } else {
        mNetworkImg.setImageResource(R.drawable.ic_net_none);
    }
}