0
我正在嘗試爲我參加的大學建立無線位置足跡系統。使用附加的代碼,我可以通過字符串生成器查看列表中的無線接入點以及與它們相關的各種信息(BSSID,信號強度,加密協議等)。將Android Wifi掃描結果放入分析數組
最終我想將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);
}
}
非常感謝Rich。這正是我所做的,將字符串格式設置爲'sb.append(String.format(「SSID:%s,BSSID:%s,RSSI:%s dBm」,result.SSID,result.BSSID,result)。水平));'但是,我現在需要將這些數據發佈到MySQL數據庫中進行查詢。我應該重新發布我的新代碼還是開始一個新的問題線程? – user2583686