android - 连接到特定的接入点android

标签 android android-wifi access-point

您好,我正在尝试使用 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());
    }

}

最佳答案

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

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

关于android - 连接到特定的接入点android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22787505/

相关文章:

android - 使用 broadcastReceiver 检测 android 中的来电和去电

android - 在特定设备上获取 native Android Crash/system/lib64/libc.so (tgkill+8)

android - wifi 提供商 onLocationChanged 从未被调用

java - 通过Android App从本地Wifi接收数据并使用3G实时传输到远程服务器

java - 在 Android Oreo 8.x 中更改 WiFi 热点的 SSID 和密码

android - 无法通过 wifi 连接连接到套接字

android - 主动扫描和被动扫描有什么区别?

linux - 如何通过SLIP、串口向wifi模块发送命令?

Android OAUTH token 存储

c# - 比较 android(java) 和 c# 中的 md5 哈希