-1

如何将 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());




            }

        }
    }
4

1 回答 1

0

您的问题有点不清楚,因为它没有像 Stu Whyte 指出的那样指定数据库所在的位置。如果您在设备上使用本机 sqlite db,那么您需要一个 SQLHelper,请阅读此处

相反,如果您的数据库驻留在服务器上,您需要以某种中间件格式(如 JSON 或 XML)序列化结果,并将数据传输到 Web 服务器,该服务器将管理数据库连接。您可以使用本机包含的 org.apache.http 库,它允许您像普通的 Web 表单一样将数据发布/放入 Web 服务。文档在这里

于 2013-07-19T12:28:38.780 回答