Get WiFi link speed and frequency

| |
Example show how to get WiFi link speed and frequency:


When WiFi OFF


package com.blogspot.android_er.androidwifispeedfrequency;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView textInfo = (TextView)findViewById(R.id.info);
TextView textSpeed = (TextView)findViewById(R.id.speed);
TextView textFreq = (TextView)findViewById(R.id.frequency);

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(wifiManager!=null){
textInfo.setText(wifiManager.toString());
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
textSpeed.setText("Link Speed: " + wifiInfo.getLinkSpeed() + wifiInfo.LINK_SPEED_UNITS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textFreq.setText("Freuency: " + wifiInfo.getFrequency() + wifiInfo.FREQUENCY_UNITS);
}
}else{
textInfo.setText("wifiManager == null!");
}
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
tools_context="com.blogspot.android_er.androidwifispeedfrequency.MainActivity">

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold" />

<TextView
android_id="@+id/info"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_textStyle="italic"/>
<TextView
android_id="@+id/speed"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_textStyle="bold"
android_textSize="30dp"/>
<TextView
android_id="@+id/frequency"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_textStyle="bold"
android_textSize="30dp"/>

</LinearLayout>



uses-permission of "android.permission.ACCESS_WIFI_STATE" is needed in AndroidManifest.xml.

Related Posts by Categories

0 comments:

Post a Comment