Sunday 18 October 2020

How to get wifi speed in android programmatically

 To get Wifi or Internet connection speed programmatically in android so let's start how to get it.



First you have to add permissions in Menifest.xml file like below


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>



And then do this code in java file where you want to get connection speed in code. 


        try {
// get Internet connection speed

ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in airplane mode it will be null
NetworkCapabilities nc = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
nc = cm.getNetworkCapabilities(cm.getActiveNetwork());
int downSpeed = nc.getLinkDownstreamBandwidthKbps();
int upSpeed = nc.getLinkUpstreamBandwidthKbps();
txtwifispeed.setText("down speed.."+downSpeed + ".....up speed.."+upSpeed);
}


}catch (Exception e){
e.printStackTrace();
}



Here is the code to get Connection speed programatically.


how to get wifi speed in android programmatically

No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...