I have a code which tells me the signal strength from the wifi. Now, I wished that it should refresh every 5 seconds and tell me the new signal strength.
It gives a set of signal strength 5 times and every time it gives me the same signal strength. I dont know why.
Once install on my android device it gives me the signal strength which is same as the answer of signal strength when it was installed first. So, whenever I run the application on the android device, I get the same answer.
The code is :
I get the answer in dbm.
public class MainActivity extends Activity {
protected static final long TIME_DELAY = 5000;
TextView mTextView;
Handler handler=new Handler();
int count =0; String data ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text_id);
handler.post(updateTextRunnable);
}
@Override
public void onResume() {
super.onResume();
// Register the scan receiver
registerReceiver(wifiReciever);
}
@Override
public void onPause() {
super.onPause();
// Register the scan receiver
unregisterReceiver(wifiReciever);
}
Runnable updateTextRunnable = new Runnable() {
public void run() {
if (count < 5) {
StringBuilder sb = new StringBuilder();
WifiManager mainWifiObj;
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
for (ScanResult result : wifiScanList) {
if (result.SSID.equals("Khosla ka Ghosla")) {
sb.append(""+result.level);
}
if (result.SSID.equals("panny")) {
sb.append(""+result.level);
}
if (result.SSID.equals("ferbora")) {
sb.append(""+result.level);
}
}
}
}
WifiScanReceiver wifiReciever = new WifiScanReceiver();
registerReceiver(wifiReciever, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
count++; mTextView.setText("getting called " +count + sb);
} else {
}
//----------------code here to send values to java server---
handler.postDelayed(this, TIME_DELAY);
}
};
}
Please help me. Thanks in advance.