Connecting to a WiFi through an Android application

为君一笑 提交于 2019-12-06 12:54:35

You need a ScanResult to determine the encryption type of the wifi configuration picked by the user.

The code below works fine in my app, except the EAP encryption type is not handled yet.

And one more thing, the method mWifiManager.reassociate( ) and mWifiManager.reconnect( ) seems work the same for me.

 private void connectWifi( ScanResult sr , String pwd )
    {

    WifiConfiguration config = new WifiConfiguration( );
    config.allowedAuthAlgorithms.clear( );
    config.allowedGroupCiphers.clear( );
    config.allowedKeyManagement.clear( );
    config.allowedPairwiseCiphers.clear( );
    config.allowedProtocols.clear( );
    config.SSID = "\"" + sr.SSID + "\"";

    if( sr.capabilities.contains( "WEP" ) )
    {
        config.hiddenSSID = true;

        if( pwd.matches( "[0-9A-Fa-f]{64}" ) )
        {
        config.preSharedKey = pwd;
        }
        else
        {
        config.preSharedKey = '"' + pwd + '"';
        }

        config.allowedKeyManagement.set( KeyMgmt.NONE );
        config.allowedAuthAlgorithms.set( WifiConfiguration.AuthAlgorithm.OPEN );
        config.allowedAuthAlgorithms.set( WifiConfiguration.AuthAlgorithm.SHARED );
        config.allowedGroupCiphers.set( WifiConfiguration.GroupCipher.CCMP );
        config.allowedGroupCiphers.set( WifiConfiguration.GroupCipher.TKIP );
        config.allowedGroupCiphers.set( WifiConfiguration.GroupCipher.WEP40 );
        config.allowedGroupCiphers.set( WifiConfiguration.GroupCipher.WEP104 );
        config.wepTxKeyIndex = 0;

    }
    else if( sr.capabilities.contains( "PSK" ) )
    {
        config.preSharedKey = "\"" + pwd + "\"";
        config.hiddenSSID = true;
        config.allowedAuthAlgorithms.set( WifiConfiguration.AuthAlgorithm.OPEN );
        config.allowedGroupCiphers.set( WifiConfiguration.GroupCipher.TKIP );
        config.allowedKeyManagement.set( WifiConfiguration.KeyMgmt.WPA_PSK );
        config.allowedPairwiseCiphers.set( WifiConfiguration.PairwiseCipher.TKIP );
        config.allowedGroupCiphers.set( WifiConfiguration.GroupCipher.CCMP );
        config.allowedPairwiseCiphers.set( WifiConfiguration.PairwiseCipher.CCMP );
        config.status = WifiConfiguration.Status.ENABLED;
    }
    else if( sr.capabilities.contains( "EAP" ) )
    {

    }
    else
    {
        config.allowedKeyManagement.set( KeyMgmt.NONE );
    }

    int wcgID = mWifiManager.addNetwork( config );
    mWifiManager.enableNetwork( wcgID , true );
    //  mWifiManager.reconnect( );
    mWifiManager.reassociate( );
    mWifiManager.saveConfiguration( );
    }






private final OnItemClickListener mOnItemClickListener = new OnItemClickListener( )
    {

    @Override
    public void onItemClick( AdapterView< ? > parent , View view , int position , long id )
    {
        final ScanResult sr = sortWifiList.get( position );
        if( !mWifiInfo.getSSID( ).equals( "\"" + sr.SSID + "\"" ) )
        {
        mConnectState = NO_CONNECT;
        refreshConnectViews( );
        }
        connectImage = (ImageView)view.findViewById( R.id.wifi_connected );
        connectTextView = (TextView)view.findViewById( R.id.wifi_info_state );
        connectTextView.setTag( sr );
        //      connectTextView.setVisibility( View.VISIBLE );
        //      connectImage.setVisibility( View.VISIBLE );
        //      connectTextView.setText( R.string.wifi_connecting );
        //      connectImage.setBackground( refreshDrawable );
        //      connectImage.startAnimation( animation );
        //      TextView wifiSsid = (TextView)view.findViewById( R.id.wifi_ssid );
        //      wifiSsid.setPadding( 0 , 0 , 0 , 0 );
        if( !mWifiInfo.getSSID( ).equals( "\"" + sr.SSID + "\"" ) )
        {
        if( hasLocked( sr.capabilities ) )
        {
            mWifiConfiguration = mWifiManager.getConfiguredNetworks( );
            WifiConfiguration wc = hasSaved( sr );
            // saved wifi , connect directly
            if( wc != null )
            {
            connectWifi( wc );
            }
            else
            {
            enterPasswordDialog( view , sr );
            }
        }
        else
        {
            connectWifi( sr , "" );
        }
        }
        autoRefresh = false;
        mScanner.pause( );
    }
    };


    private boolean hasLocked( String capabilities )
    {
    if( capabilities.contains( "WEP" ) )
    {
        return true;
    }
    else if( capabilities.contains( "PSK" ) )
    {
        return true;
    }
    else if( capabilities.contains( "EAP" ) )
    {
        return true;
    }
    return false;
    }

private void connectWifi( WifiConfiguration wc )
{
    int wcgID = mWifiManager.addNetwork( wc );
    mWifiManager.enableNetwork( wcgID , true );
    //  mWifiManager.reconnect( );
    mWifiManager.reassociate( );
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!