下面列出了android.net.wifi.WifiInfo#getLinkSpeed ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Gets Link Speed of Connected WiFi
*
* You need to declare the below permission in the manifest file to use this properly
*
* <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
*
* @return link speed
*/
@RequiresPermission(allOf = {
Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_NETWORK_STATE
})
public final String getWifiLinkSpeed() {
String result = null;
if (PermissionUtil.hasPermission(context, Manifest.permission.ACCESS_WIFI_STATE)) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo == null) {
result = null;
}
if (networkInfo != null && networkInfo.isConnected()) {
final WifiManager wifiManager =
(WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null) {
final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
result = connectionInfo.getLinkSpeed() + " Mbps";
}
}
}
}
}
return CheckValidityUtil.checkValidData(result);
}
@NonNull
WiFiConnection transformWifiInfo(WifiInfo wifiInfo) {
if (wifiInfo == null || wifiInfo.getNetworkId() == -1) {
return WiFiConnection.EMPTY;
}
return new WiFiConnection(
WiFiUtils.convertSSID(wifiInfo.getSSID()),
wifiInfo.getBSSID(),
WiFiUtils.convertIpAddress(wifiInfo.getIpAddress()),
wifiInfo.getLinkSpeed());
}
public static int[] getWifiInfo(){
try{
WifiManager wmgr=(WifiManager) ApplicationLoader.applicationContext.getSystemService(Context.WIFI_SERVICE);
WifiInfo info=wmgr.getConnectionInfo();
return new int[]{info.getRssi(), info.getLinkSpeed()};
}catch(Exception ignore){}
return null;
}
public static int[] getWifiInfo(){
try{
WifiManager wmgr=(WifiManager) ApplicationLoader.applicationContext.getSystemService(Context.WIFI_SERVICE);
WifiInfo info=wmgr.getConnectionInfo();
return new int[]{info.getRssi(), info.getLinkSpeed()};
}catch(Exception ignore){}
return null;
}
/**
* Check if the connection is fast
*/
public int getMaxLimitDownload() {
if (maxLimitDownload > 0) {
return maxLimitDownload;
}
int maxLimit;
if (HelperCheckInternetConnection.connectivityType == -1) {
HelperCheckInternetConnection.detectConnectionTypeForDownload();
}
if (HelperCheckInternetConnection.connectivityType == ConnectivityManager.TYPE_WIFI) {
int linkSpeed = 10;
WifiManager wifiManager = (WifiManager) G.context.getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null) {
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null) {
linkSpeed = wifiInfo.getLinkSpeed(); // Mbps
linkSpeed = (linkSpeed * 1024); // Kbps
//linkSpeed = (linkSpeed * 1024) / 8; // KByte per second
}
}
if (linkSpeed < 100) {
maxLimit = KB_10;
} else if (linkSpeed < 1000) {
maxLimit = KB_30;
} else if (linkSpeed < 1500) {
maxLimit = KB_50;
} else {
maxLimit = KB_100;
}
} else if (HelperCheckInternetConnection.connectivityType == ConnectivityManager.TYPE_MOBILE) {
switch (HelperCheckInternetConnection.connectivitySubType) {
case TelephonyManager.NETWORK_TYPE_CDMA: // ~ 14-64 kbps
case TelephonyManager.NETWORK_TYPE_IDEN: // ~ 25 kbps -> API level 8
case TelephonyManager.NETWORK_TYPE_1xRTT: // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_EDGE: // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_GPRS: // ~ 100 kbps
maxLimit = KB_10;
break;
case TelephonyManager.NETWORK_TYPE_EVDO_0: // ~ 400-1000 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_A: // ~ 600-1400 kbps
maxLimit = KB_30;
break;
case TelephonyManager.NETWORK_TYPE_HSPA: // ~ 700-1700 kbps
case TelephonyManager.NETWORK_TYPE_UMTS: // ~ 400-7000 kbps
maxLimit = KB_50;
break;
case TelephonyManager.NETWORK_TYPE_EHRPD: // ~ 1-2 Mbps -> API level 11
case TelephonyManager.NETWORK_TYPE_HSUPA: // ~ 1-23 Mbps
case TelephonyManager.NETWORK_TYPE_HSDPA: // ~ 2-14 Mbps
case TelephonyManager.NETWORK_TYPE_EVDO_B: // ~ 5 Mbps -> API level 9
case TelephonyManager.NETWORK_TYPE_HSPAP: // ~ 10-20 Mbps -> API level 13
case TelephonyManager.NETWORK_TYPE_LTE: // ~ 10+ Mbps -> API level 11
maxLimit = KB_100;
break;
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
default:
maxLimit = KB_10;
}
} else {
maxLimit = KB_10;
}
maxLimitDownload = maxLimit;
return maxLimit;
}
private void getWiFiInfo()
{
initNetwork();
if (wifiManager != null)
{
final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
fullInfo.setProperty("WIFI_SSID",
String.valueOf(Helperfunctions.removeQuotationsInCurrentSSIDForJellyBean(wifiInfo.getSSID())));
/*
* fullInfo.setProperty("WIFI_LINKSPEED",
* String.valueOf(wifiInfo.getLinkSpeed()));
*/
fullInfo.setProperty("WIFI_BSSID", String.valueOf(wifiInfo.getBSSID()));
fullInfo.setProperty("WIFI_NETWORK_ID", String.valueOf(wifiInfo.getNetworkId()));
/*
* fullInfo.setProperty("WIFI_RSSI",
* String.valueOf(wifiInfo.getRssi()));
*/
final SupplicantState wifiState = wifiInfo.getSupplicantState();
fullInfo.setProperty("WIFI_SUPPLICANT_STATE", String.valueOf(wifiState.name()));
final DetailedState wifiDetail = WifiInfo.getDetailedStateOf(wifiState);
fullInfo.setProperty("WIFI_SUPPLICANT_STATE_DETAIL", String.valueOf(wifiDetail.name()));
if (getNetwork() == NETWORK_WIFI)
{
final int rssi = wifiInfo.getRssi();
if (rssi != -1 && rssi >= ACCEPT_WIFI_RSSI_MIN)
{
int linkSpeed = wifiInfo.getLinkSpeed();
if (linkSpeed < 0) {
linkSpeed = 0;
}
final SignalItem signalItem = SignalItem.getWifiSignalItem(linkSpeed, rssi);
if (this.collectInformation) {
signals.add(signalItem);
}
lastSignalItem.set(signalItem);
signal.set(rssi);
signalType.set(SINGAL_TYPE_WLAN);
// Log.i(DEBUG_TAG, "Signals1: " + signals.toString());
}
}
}
}
/**
* Get current WiFi link speed.
*
* @param context The Context
* @return Link speed of wifi connection
*/
public static int getLinkSpeed(Context context) {
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = manager.getConnectionInfo();
return wifiInfo.getLinkSpeed();
}