2013-11-04 185 views
3

我目前正在研究將安裝並在Google Glass設備上運行的原生Android應用程序。這個應用程序的一個要求是我主動閱讀wifi網絡的頻率和電平信息。Google Glass Wifi掃描

這是Android通常進行下列行:

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

傳統Android裝置我的廣播接收機(由BROADCAST_RECEIVER_HERE佔位符指定)接收更新的WiFi掃描結果每秒上運行時。當在Glass上運行相同的應用程序時,結果仍然每秒進入一次,但實際的wifi級別值只會在每4-5次掃描結果或5-6秒內發生變化。

這是爲了電池壽命而做出的錯誤還是有意識的決定?無論如何,我可以配置WifiManager,以便它接收每個掃描結果的更新級別?

編輯: 以下是一些示例日誌。每行包含一個時間戳和兩個特定無線網絡的ScanResult對象中報告的強度級別。

運行在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 

運行在玻璃

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 

回答

2

這裏是我使用掃描無線網絡連接,並在列表中顯示它們的代碼谷歌glass.Automatically打開wifi如果它關閉。也會自動改變,當你手動關閉wifi。還請在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); 
     } 

    } 

      } 

這是xml文件。 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> 

而且這個我的代碼的輸出。 enter image description here

+1

+1謝謝,這很有幫助 – sjobe

+0

對不起@TalhaQ但這並沒有回答我的問題。我的問題是沒有詢問如何進行無線掃描,但爲什麼無線掃描的信號強度結果在Google Glass設備上更新得慢得多。看看我原來的帖子,我提供了來自普通Android設備和Glass設備的掃描結果示例數據。 – kurib0sShoe