0

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.

  1. It gives a set of signal strength 5 times and every time it gives me the same signal strength. I dont know why.

  2. 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.

smith willy
  • 73
  • 10

1 Answers1

0

You are going about this all wrong.

mainWifiObj.getScanResults(); gets the last-updated scan results. These are not updated right away. You need to make a call (which you do) to register a receiver to listen for changes in the wifi signals. The receiver's onReceive method will automatically be called every time the signal strength changes - so you don't need to call your Runnable over and over.

Just handle all the code from List<ScanResult> wifiScanList = mainWifiObj.getScanResults();, onward in the onReceive method of WifiScanReceiver, and register this receiver in onResume, and unregister it in onPause.

You will also benefit from having a look at the source code for the WiFi Settings app on Android, which is available here.

Phil
  • 35,852
  • 23
  • 123
  • 164