Android:如何扫描WiFi,连接到AP然后建立TCP连接?

时间:2016-12-21 17:25:15

标签: java android sockets networking tcp

我正在写一个Android应用程序。在我的应用程序中,我希望它扫描WiFi网络,然后通过检查SSID连接到专用接入点(AP)。然后在连接到AP后,建立与它的TCP连接。

上面我按顺序描述了3个任务。我知道如何独立完成每项任务,但我不知道按顺序完成它们的正确方法。赞赏的例子。

目前我的代码是每当用户按下按钮时开始扫描WiFi网络:

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled() == false) {
    wifiManager.setWifiEnabled(true);
    Toast.makeText(MainActivity.this, "WiFi is enabled", Toast.LENGTH_SHORT).show();
}
wifiManager.startScan();

我使用广播接收器检查扫描完成的时间,然后调用方法(ReconnectAP())将WiFi连接到AP。我还使用广播接收器来检查它何时连接到AP,然后我想执行异步任务来建立TCP网络。然而,通过谷歌搜索,我了解到在广播接收器中执行异步任务是一种不好的做法,因为当接收器完成时,异步任务可以被Android杀死。

private String networkSSID = "The_AP_SSID";

// In onCreate of MainActivity, register the broadcast receiver:
registerReceiver(WiFireceiver, new IntentFilter(
            WifiManager.NETWORK_STATE_CHANGED_ACTION));
registerReceiver(WiFireceiver, new IntentFilter(
            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

// Implement my broadcast receiver class:
BroadcastReceiver WiFireceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {

            Log.d(TAG, "WiFiReceiver received broadcast, action is SCAN_RESULTS_AVAILABLE_ACTION");
            Log.d(TAG, "Start to check if need to reconnect to Charger AP");
            ReconnectAP();

        }

        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {

            Log.d(TAG, "WiFiReceiver received broadcast, action is NETWORK_STATE_CHANGED_ACTION");

            NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            // If the connection type is WiFi...
            if (netInfo != null && ConnectivityManager.TYPE_WIFI == netInfo.getType()) {

                // If it is connected now....
                if (netInfo.isConnected()) {

                    Log.d(TAG, "WiFi connected");

                    // Get WiFi connection information, eg SSID
                    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                    String ssid = wifiInfo.getSSID();

                    Log.d(TAG, "WiFi connected SSID is : " + ssid);

                    if(ssid.equals(networkSSID)) {
                        // Start async task to establish a TCP connection:
                        new ConnectTCPAsynTask().execute();

                    }
                } else {
                    Log.d(TAG, "WiFi disconnected");
                }
            }
        }
    }
};

int ReconnectAP() {

    Log.d(TAG, "In ReconnectAP( )");

    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    String ssid = wifiInfo.getSSID();

    Log.d(TAG, "Original connected AP SSID: " + ssid);

    if (!ssid.equals(networkSSID)) {

        Log.d(TAG, "It is not equal to the dedicated AP SSID, 
                    we will disconnect the current WiFi connection, 
                    and then connect to our dedicate AP");

        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration i : list) {
            if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {

                Log.d(TAG, "Start disconnect and reconnect to " + networkSSID);
                wifiManager.disconnect();
                wifiManager.enableNetwork(i.networkId, true);
                wifiManager.reconnect();

                return 1;
            }
        }
        Log.d(TAG,"Reach here if cannot find the target SSID");
        return -1;
    }
    Log.d(TAG, "Reach here if we already connected to the dedicated AP");
    return 0;

}

1 个答案:

答案 0 :(得分:0)

将广播接收器中的标志传回主活动,让主活动处理任务。