3

I'm currently working on a native Android application that will be installed and run on a Google Glass device. One of the requirements of this application is that I actively read frequency and level information of a wifi network.

This is commonly done in Android with the following lines:

this.registerReceiver(BROADCAST_RECEIVER_HERE, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
((WifiManager) getSystemService(Context.WIFI_SERVICE)).startScan();

When running on a traditional Android device my broadcast receiver (designated by the BROADCAST_RECEIVER_HERE placeholder) receives updated wifi scan results every second. When running the same application on Glass the results still come in once a second but the actual wifi level values only seem to change every 4-5 scan results or 5-6 seconds.

Is this a bug or a conscious decision made for the sake of battery life? Is there anyway for me to configure WifiManager so that it receives updated levels for each and every scan result?

EDIT: Here are some example logs. Each line includes a time stamp and the strength levels that are being reported in the ScanResult objects for two specific wireless networks.

Running on Android

16:01:05.864: -43.0    -46.0
16:01:07.185: -43.0    -45.0
16:01:08.520: -49.0    -50.0
16:01:09.841: -48.0    -53.0
16:01:11.161: -48.0    -53.0
16:01:12.489: -47.0    -45.0
16:01:13.810: -45.0    -52.0
16:01:15.192: -51.0    -51.0
16:01:16.497: -45.0    -46.0
16:01:17.833: -45.0    -46.0

Running on Glass

16:04:14.089: -42.0    -41.0
16:04:15.097: -42.0    -41.0
16:04:16.097: -42.0    -39.0
16:04:17.152: -42.0    -39.0
16:04:18.183: -42.0    -39.0
16:04:19.222: -42.0    -39.0
16:04:20.238: -42.0    -39.0
16:04:21.246: -42.0    -42.0
16:04:22.253: -43.0    -41.0
16:04:23.253: -43.0    -41.0

1 Answers1

2

Here is code which i am using for scan wifi connections and show them in the list on Google glass.Automatically turn on the wifi if its off.Also changes automatically when you turn off the wifi manually.Also please mentions the required permissions in the manifest.xml.

     public class WifiActivity extends Activity {
     TextView mainText;
     WifiManager mainWifi;
     WifiReceiver receiverWifi;
     List<ScanResult> wifiList;
     StringBuilder sb = new StringBuilder();

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

    mainText = (TextView) findViewById(cw.glasslife.R.id.mainText);

    // Initiate wifi service manager
    mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    // Check for wifi is disabled
    if (mainWifi.isWifiEnabled() == false)
         {   
             // If wifi disabled then enable it
             Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", 
             Toast.LENGTH_LONG).show();

             mainWifi.setWifiEnabled(true);
         } 

    // wifi scaned value broadcast receiver 
    receiverWifi = new WifiReceiver();

    // Register broadcast receiver 
    // Broacast receiver will automatically call when number of wifi connections changed
    registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    mainWifi.startScan();
    mainText.setText("Starting Scan...");
}

 public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, "Refresh");
        return super.onCreateOptionsMenu(menu);
    }

    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        mainWifi.startScan();
        mainText.setText("Starting Scan");
        return super.onMenuItemSelected(featureId, item);
    }

    protected void onPause() {
        unregisterReceiver(receiverWifi);
        super.onPause();
    }

    protected void onResume() {
        registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        super.onResume();
    }

    class WifiReceiver extends BroadcastReceiver {

        // This method call when number of wifi connections changed
        public void onReceive(Context c, Intent intent) {
            sb = new StringBuilder();
            wifiList = mainWifi.getScanResults(); 
            sb.append("\n        Number Of Wifi connections :"+wifiList.size()+"\n\n");

            for(int i = 0; i < wifiList.size(); i++){

                sb.append(new Integer(i+1).toString() + ". ");
                sb.append(wifiList.get(i).SSID);
                sb.append("\n\n");
            }

            mainText.setText(sb);  
        }

    }

          }

this is the xml file. activity_wifi.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
         <TextView
    android:id="@+id/mainText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/wifi_connections" />

         </LinearLayout>

Also this the output of my code. enter image description here

Talha Q
  • 4,350
  • 4
  • 28
  • 40
  • Sorry @TalhaQ but this didn't answer my question. My question wasn't asking HOW to perform wifi scans but WHY the signal strength results from wifi scans were updating so much slower on Google Glass devices. Check out my original post, I provide scan result example data from a normal Android device and a Glass device. – kurib0sShoe Jun 04 '14 at 14:41