2016-12-21 161 views
0

我正在編寫一個Android應用程序。在我的應用程序中,我希望它掃描WiFi網絡,然後通過檢查SSID連接到專用接入點(AP)。然後連接到AP後,建立一個TCP連接。Android:如何掃描WiFi,連接到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網絡。但是,通過使用Google搜索,我發現在廣播接收器中執行異步任務是一種糟糕的做法,因爲在接收器完成時異步任務可能會被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; 

} 

回答

0

將來自廣播接收機的標誌傳遞迴主要活動並讓主要活動處理任務。