【问题标题】:Google Glass Wifi ScansGoogle Glass Wifi 扫描
【发布时间】:2013-12-16 16:24:57
【问题描述】:

我目前正在开发一个将在 Google Glass 设备上安装和运行的原生 Android 应用程序。这个应用程序的一个要求是我主动读取一个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

【问题讨论】:

    标签: android google-glass


    【解决方案1】:

    这是我用于扫描 wifi 连接并将其显示在 Google glass 上的列表中的代码。如果 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>
    

    这也是我的代码的输出。

    【讨论】:

    • 对不起@TalhaQ 但这没有回答我的问题。我的问题不是问如何执行 wifi 扫描,而是为什么 wifi 扫描的信号强度结果在 Google Glass 设备上更新得这么慢。查看我的原始帖子,我提供了来自普通 Android 设备和 Glass 设备的扫描结果示例数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 2015-12-06
    • 1970-01-01
    • 2012-03-20
    • 2011-02-28
    相关资源
    最近更新 更多