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.
No comments:
Post a Comment
Comments