2013年8月4日 星期日

How to connect to particular SSID programmatically in android

http://stackoverflow.com/questions/14119557/connect-to-the-specified-configured-network-programmatically

the key is:
        mWifiMgr.disconnect();
        mWifiMgr.enableNetwork(wifiConfig.networkId, true);
        mWifiMgr.reconnect();

and determining if the connection to the AP is established:
                ConnectivityManager connectivityManager = (ConnectivityManager) 
                        MyController.mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
                
                if (connectivityManager != null){
                    networkInfo = connectivityManager.getActiveNetworkInfo();
                }
                
                if (networkInfo != null && networkInfo.isConnected()){
                    Log.v(TAG, "CONNECTED!! detailed state = " + networkInfo.getDetailedState());
                    break;
                }
                Log.v(TAG, "not connected!! reason = " + networkInfo.getReason());