0

我正在尝试为我就读的大学建立一个无线定位足迹系统。使用随附的代码,我可以通过字符串生成器在列表中查看无线接入点以及与其相关的各种信息(BSSID、信号强度、加密协议等)。

最终,我想将数据包装getScanResults();到一个数组中并输出解析的信息,这样我就只有在外部服务器上查询数据库所需的相关信息。(即BSSID & Signal Strength)如何实现?

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);
    }
}
4

1 回答 1

0

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

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

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

有关 ScanResult 的信息,请参阅Android 开发站点

于 2013-07-16T15:50:26.713 回答