如何将 wifi 扫描的结果发送到我创建的 SQL/MySQL 数据库,以使用 JSON 填充值 SSID、BSSID 和 RSSI?到目前为止,我已经能够将这些结果输出到一个列表中(见下面的代码)。
package com.example.wifiscanner8;
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;
public class MainActivity extends Activity {
TextView textStatus;
TextView textState;
WifiManager mainWifi;
WifiReceiver receiverWifi;
List<ScanResult> wifiList;
//StringBuilder sb = new StringBuilder();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//enable wifi on application start
WifiManager wifiManager = (WifiManager) getBaseContext()
.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
textStatus = (TextView) findViewById(R.id.textStatus);
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
receiverWifi = new WifiReceiver();
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);
}
// 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);
}
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 {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
handleScanResultsAvailable();
}
}
private void handleScanResultsAvailable() {
List<ScanResult> results = mainWifi.getScanResults();
int size = results.size();
//StringBuilder sb = new StringBuilder();
for (int i=0; i<size; i++){
ScanResult scanresult = mainWifi.getScanResults().get(i);
String bssid = scanresult.BSSID;
String ssid = scanresult.SSID;
int rssi = scanresult.level;
String rssiString = String.valueOf(rssi);
textStatus.append(bssid + "," + ssid + "," + rssiString);
textStatus.append("\n");
}
// getting JSON Object
// Note that create product url accepts POST method
//JSONObject json = jsonParser.makeHttpRequest(url_wifi_scan_result,
// "POST", reading);
// check log cat from response
// Log.d("Create Response", json.toString());
}
}
}