connect to specific access point android
Asked Answered
A

2

9

Hi i'm trying to connect my app to a specific access point using the wifimanager api. as it stands I have a list of all access point in my area,from this list i am storing them in a array and picking which one to connect to . but at this stage is dose not connect . can someone help me .

( this is a open network i'm trying to connect to . ) here is my code:

public void WifiConfiguration(){
    try {
        ScanResult networkData = getIntent().getParcelableExtra("networkData");

        WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        String networkPassWord = "";

        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + networkData.SSID + "\"";
        conf.BSSID = "\"" + networkData.BSSID + "\"";
        conf.hiddenSSID = true;
        conf.wepKeys[0] = "\"" + networkPassWord + "\""; 
        conf.wepTxKeyIndex = 0; 
        conf.status = WifiConfiguration.Status.ENABLED;        
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

        conf.preSharedKey = "\""+ networkPassWord +"\"";

        //conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

        Log.d(TAG, "Initialising WIFI Manager");

        int id = wifiManager.addNetwork(conf);
        Log.d(TAG, "conf.SSID: "+conf.SSID);
        Log.d(TAG, "id: "+id);
        wifiManager.disconnect();
        wifiManager.enableNetwork(id, true);
        wifiManager.reconnect();       

        Log.d(TAG, "Should be connected....");

    } catch (Exception e) {

        Log.d(TAG, e.toString());
    }

}
Abydos answered 1/4, 2014 at 13:33 Comment(5)
When you test the connection state?Champlin
I am looking at the number of users on the network and there is none, so the app is not making a connectionAbydos
@Abydos If you got the answer you should write your answer below, or close the question.Prelusive
@Abydos what was the answer?Ditto
@Abydos what is the answer?Musjid
D
0
public void connect(Context context,String ssid,String password ){
WifiManager mWifiManager = 
(WifiManager)context.getSystemService(context.WIFI_SERVICE);
WifiConfiguration wifiConf = null;
WifiConfiguration savedConf = null;


Log.d(TAG, "password :" + password);
//existing configured networks
List<WifiConfiguration> list = mWifiManager.getConfiguredNetworks();

if(list!=null) {
    for( WifiConfiguration i : list ) {
        if (i.SSID != null && i.SSID.equals(ssid)) {
            Log.d(TAG, "existing network found: " + i.networkId + " " + i.SSID);
            savedConf = i;
            break;
        }
    }
}

if(savedConf!=null) {
    Log.d(TAG, "coping existing configuration");
    wifiConf = savedConf;
   
} else {
    Log.d(TAG, "creating new configuration");
    wifiConf = new WifiConfiguration();
    
}

wifiConf.SSID = String.format("\"%s\"", ssid);
wifiConf.preSharedKey = String.format("\"%s\"", password);

int netId;

if(savedConf!=null) {
    netId = mWifiManager.updateNetwork(wifiConf);
    Log.d(TAG, "configuration updated " + netId);
} else {
    netId = mWifiManager.addNetwork(wifiConf);
    Log.d(TAG, "configuration created " + netId);
}

mWifiManager.saveConfiguration();
mWifiManager.disconnect();
mWifiManager.enableNetwork(netId, true);
mWifiManager.reconnect();  }
Depressant answered 1/4, 2014 at 13:33 Comment(0)
B
0
    conf.SSID = "\"" + networkData.SSID + "\"";
    conf.BSSID = "\"" + networkData.BSSID + "\"";

The BSSID doesn't need to be enclosed in double quotes, only the SSID.

Bromidic answered 10/6, 2014 at 21:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.