无法以编程方式连接到特定的WiFi [英] failed to connect to specific WiFi in android programmatically

查看:370
本文介绍了无法以编程方式连接到特定的WiFi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码来检测和连接到特定的WiFi ssid当我按下一个按钮在Android。下面是代码。任何帮助将不胜感激。

I'm using the following code to detect and connect to specific WiFi ssid when I press a button in android. Below is the code. Any help will be appreciated.


ssid: - myHotspot&密码: - 12345678


ssid :- "myHotspot" & password:- "12345678"


 Button b1 = (Button) findViewById(R.id.button); <br>
        b1.setOnClickListener(new View.OnClickListener() {
            <br><br>@Override
            <br>public void onClick(View v) {

                wifiConfiguration.SSID = "\"myHotspot\"";
                wifiConfiguration.preSharedKey ="\"12345678\"";
                WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);

                int netId = wifiManager.updateNetwork(wifiConfiguration);
               if (wifiManager.isWifiEnabled()) { //---wifi is turned on---
                    //---disconnect it first---
                    wifiManager.disconnect();

                } else { //---wifi is turned off---
                    //---turn on wifi---

                    wifiManager.setWifiEnabled(true);
                }
                wifiManager.enableNetwork(netId, true);
                wifiManager.reconnect();

            }
        });




我遇到的主要问题是,我的手机连接到ssid,2-3秒后,它失去连接,并连接到我的家庭无线路由器(有互联网连接)


The main problem I'm getting is that my phone gets connected to the ssid and after 2-3 seconds it loses the connection and gets connected to my home Wifi router (which has internet connectivity)



注意: - 我尝试连接的ssid只是一个没有任何互联网连接的本地热点。

如果我尝试使用addNetwork(wifiConfiguration),它会创建多个同名的网络。所以现在我该如何解决这个问题?


Note:- The ssid I'm trying to connect is just a local hotspot without any internet connection.
and if I try with "addNetwork(wifiConfiguration)" it creates multiple networks of the same name. so Now how do I resolve this ?!

推荐答案

我认为这里的问题是你立即尝试 enableNetwork 调用 wifiManager.setWifiEnabled(true)。一般来说,打开wifi会花5-10秒,取决于设备,直到 wifiManager.enableNetwork 的任何呼叫将丢失。因此,您连接到所需网络的呼叫正在丢失,一旦wifi打开,您的设备就会连接到它记住的最后一个网络。

I think the problem here is you try to enableNetwork immediately after the call to wifiManager.setWifiEnabled(true). Generally, switching on wifi will take 5-10 seconds depending on the device, until then any call to wifiManager.enableNetwork will be lost. Hence, your call to connect to the desired network is getting lost and as soon as the wifi is switched on, your device connects to the last network it remembers.

尝试创建一个循环,你可以检查 wifiManager.isWifiEnabled()== true 并保持循环,直到它返回true( Thread.sleep / code>显然和在AsyncTask或单独的线程)。只有在尝试调用 enableNetwork 之后。

Try to create a loop where you keep checking if wifiManager.isWifiEnabled() == true and keep looping until it returns true (with Thread.sleep() obviously and doing this in an AsyncTask or separate Thread). Only after that try to call enableNetwork.

这篇关于无法以编程方式连接到特定的WiFi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆