连接到特定的Android接入点

9

你好,我正在尝试使用wifimanager api将我的应用程序连接到特定的接入点。目前,我拥有我所在区域内的所有接入点列表,我将它们存储在一个数组中,并选择要连接的接入点。但是目前为止,它还没有连接成功。请问有人可以帮助我吗?

(我正在尝试连接的是一个开放网络。)以下是我的代码:

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());
    }

}

你什么时候测试连接状态? - atok
我正在查看网络上的用户数量,但没有任何用户,因此应用程序无法建立连接。 - user3292394
我自己找到了答案。 感谢你们的所有帮助。 - user3292394
1
如果您已经得到答案,应该在下方编写您的答案,或关闭问题。 - ThomasW
1
@user3292394,答案是什么? - injecteer
@user3292394 答案是什么? - Kapil Jituri
2个回答

0
    conf.SSID = "\"" + networkData.SSID + "\"";
    conf.BSSID = "\"" + networkData.BSSID + "\"";

BSSID 不需要用双引号括起来,只有 SSID 需要。


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();  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接