3

私は現在、Google Glass デバイスにインストールして実行するネイティブ Android アプリケーションに取り組んでいます。このアプリケーションの要件の 1 つは、wifi ネットワークの周波数とレベルの情報を積極的に読み取ることです。

これは通常、Android では次の行で行われます。

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

従来の Android デバイスで実行している場合、ブロードキャスト レシーバー (BROADCAST_RECEIVER_HERE プレースホルダーで指定) は、更新された Wi-Fi スキャン結果を毎秒受信します。Glass で同じアプリケーションを実行すると、結果は 1 秒に 1 回表示されますが、実際の Wi-Fi レベルの値は 4 ~ 5 回のスキャン結果または 5 ~ 6 秒ごとにしか変化しないようです。

これはバグですか、それともバッテリー寿命のために意図的に行われた決定ですか? スキャン結果ごとに更新されたレベルを受け取るように WifiManager を構成する方法はありますか?

編集: ログの例をいくつか示します。各行には、2 つの特定のワイヤレス ネットワークの 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

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
4

1 に答える 1

2

これは、wifi接続をスキャンしてGoogleガラスのリストに表示するために使用しているコードです.wifiがオフの場合は自動的にオンになります.また、wifiを手動でオフにすると自動的に変更されます.マニフェストで必要な権限についても言及してください.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>

また、これは私のコードの出力です。 ここに画像の説明を入力

于 2013-12-16T08:24:57.803 に答える