将Android Wifi Scan的结果放入已解析的数组中

时间:2013-07-15 13:44:14

标签: java android arrays eclipse parsing

我正在为我参加的大学建立无线位置足迹系统。使用附加的代码,我可以通过字符串生成器查看无线访问点以及与它们相关的各种信息(BSSID,信号强度,加密协议等)。

最终我希望将getScanResults();数据包装到一个数组中并输出解析后的信息,这样我只需要在外部服务器上查询数据库所需的相关信息。 (即BSSID和信号强度)如何实现这一目标?

package com.example.wifiscanner4;

import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Button;

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

    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_wifi_tester);

     //enable wifi on application start

            WifiManager wifiManager = (WifiManager) getBaseContext()
                     .getSystemService(Context.WIFI_SERVICE);
             wifiManager.setWifiEnabled(true);

       mainText = (TextView) findViewById(R.id.mainText);
       mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
       receiverWifi = new WifiReceiver();
       registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
       mainWifi.startScan();
       mainText.setText("Starting Scan...");
       scan = (Button)findViewById(R.id.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 onClick(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 {
        public void onReceive(Context c, Intent intent) {
            sb = new StringBuilder();
            wifiList = mainWifi.getScanResults();
            for(int i = 0; i < wifiList.size(); i++){
                sb.append(new Integer(i+1).toString() + ".");
                sb.append((wifiList.get(i)).toString());
                sb.append("\\n");
            }
            mainText.setText(sb);
        }
    }

 // Upon pressing back button to exit app, wireless function set to disable
    public boolean onKeyDown(int keyCode, KeyEvent event){ if(keyCode == KeyEvent.KEYCODE_BACK)
    {
        WifiManager wifiManager = (WifiManager) getBaseContext()
                  .getSystemService(Context.WIFI_SERVICE);
          wifiManager.setWifiEnabled(false);
    this.finish();
    }

    return super.onKeyDown(keyCode, event);
    }
}

1 个答案:

答案 0 :(得分:0)

在接收器代码中,您正在循环一个ScanResult对象列表......但是您通过递增整数来实现此目的。也许试试:

for (ScanResult result : getScanResults()) { ... }

这将为您提供一种查询“结果”并使用“BSSID”和“级别”属性提取数据的方法。

有关ScanResult的信息,请参阅Android Development Site