Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine Network Connection Bandwidth (speed) wifi and mobile data

I want to get Network Connection Bandwidth in kbps or mbps. if the device is connected to wifi then it should returns the network bandwidth(speed) as well as mobile data.

it will returns wifi capablity rate but i want exact data transfer rate.

public String getLinkRate() 
{
    WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
    WifiInfo wi = wm.getConnectionInfo();
    return String.format("%d Mbps", wi.getLinkSpeed());
}
like image 256
No Name Avatar asked Dec 18 '14 09:12

No Name


People also ask

How do I view the connection speed for a Wi-Fi or Ethernet?

To view the connection speed for a Wi-Fi or Ethernet adapter, use these steps: Open Settings. Click on Network & Internet. Click on Status. Under the "Change your network settings" section, click the View your network properties option.

When do you need to calculate bandwidth?

When do you need to calculate bandwidth? 1 Estimate how many devices will be connected to your WiFi network simultaneously#N#The majority of mid-high end wireless... 2 Calculate the application bandwidth requirement#N#Your bandwidth requirements also depend on the usage of the Internet... 3 Calculate network bandwidth requirements More ...

What is bandwidth in networking?

Network bandwidth is the capacity of a network communications link to transmit the maximum volume of data from one point to another over a computer network or Internet connection in a given amount of time, usually one second. Bandwidth has the same meaning of capacity, and defines the data transfer rate.

How much bandwidth does Wi-Fi use?

Each Wi-Fi standard is rated according to its maximum theoretical network bandwidth. However, the performance of Wi-Fi networks doesn't match these theoretical maximums. An 802.11b network typically operates no faster than about 50 percent of its theoretical peak, around 5.5 Mbps.


1 Answers

You can't just query for this information. Your Internet speed is determined and controlled by your ISP, not by your network interface or router.

So the only way you can get your (current) connection speed is by downloading a file from a close enough location and timing how long it takes to retrieve the file. For example:

static final String FILE_URL = "http://www.example.com/speedtest/file.bin";
static final long FILE_SIZE = 5 * 1024 * 8; // 5MB in Kilobits

long mStart, mEnd;
Context mContext;
URL mUrl = new URL(FILE_URL);
HttpURLConnection mCon = (HttpURLConnection)mUrl.openConnection();
mCon.setChunkedStreamingMode(0);

if(mCon.getResponseCode() == HttpURLConnection.HTTP_OK) {
    mStart = new Date().getTime();

    InputStream input = mCon.getInputStream();
    File f = new File(mContext.getDir("temp", Context.MODE_PRIVATE), "file.bin");
    FileOutputStream fo = new FileOutputStream(f);
    int read_len = 0;

    while((read_len = input.read(buffer)) > 0) {
        fo.write(buffer, 0, read_len);
    }
    fo.close();
    mEnd = new Date().getTime();
    mCon.disconnect();

    return FILE_SIZE / ((mEnd - mStart) / 1000);
}

This code, when sightly modified (you need mContext to be a valid context) and executed from inside an AsyncTask or a worker thread, will download a remote file and return the speed in which the file was downloaded in Kbps.

like image 159
mittelmania Avatar answered Oct 23 '22 12:10

mittelmania