简体   繁体   English

Android - 以编程方式连接到 wifi

[英]Android - connect to wifi programmatically

I would like to connect to WiFi network programmatically.我想以编程方式连接到 WiFi 网络。

Here is my code:这是我的代码:

wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);

WifiConfiguration config = new WifiConfiguration();
config.SSID = "\"" + ssid + "\"";
config.preSharedKey = "\""+ key +"\"";

int netId = wifiManager.addNetwork(config);
wifiManager.saveConfiguration();
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();

When I have wifi enabled on my phone, it works as expected, but the problem is, when wifi is disabled.当我在手机上启用 wifi 时,它会按预期工作,但问题是,当 wifi 被禁用时。 In this case the only result is enabling wifi adapter, but not connecting to the network.在这种情况下,唯一的结果是启用 wifi 适配器,但未连接到网络。 It seems like enabling takes to long so it won't get connected.似乎启用需要很长时间,因此无法连接。 Another strange thing to me is that wifiManager.getConfiguredNetworks() returns null.对我来说另一个奇怪的事情是wifiManager.getConfiguredNetworks()返回 null。 Do you know how to fix that?你知道如何解决这个问题吗?

Thank you谢谢

It seems like enabling takes to long so it won't get connected.似乎启用需要很长时间,因此无法连接。

Yes.是的。 This is because enabling of the network is done async, it happens in parallel, and doesn't occur immediately.这是因为网络的启用是异步完成的,它是并行发生的,并且不会立即发生。 Here are two possible solutions to your problem:以下是您的问题的两种可能解决方案:

1) This is the easiest solution, but not the best. 1) 这是最简单的解决方案,但不是最好的。 Loop as described by another user checking for the scan results to come in. However, you should add a sleep of some sort between every cycle of the loop.循环如另一位用户检查要进入的扫描结果所描述的那样。但是,您应该在循环的每个循环之间添加某种睡眠。 IE you want to wait for 1ms, so as to not eat up all the CPU resources. IE要等待1ms,以免吃光所有的CPU资源。 I am not sure how to do this in Android off the top of my head.我不确定如何在 Android 中做到这一点。 There is another problem with this method.这种方法还有一个问题。 If u are in the GUI thread, you will block all GUI events this way, as you wait for the connection to be established.如果您在 GUI 线程中,您将在等待建立连接时以这种方式阻止所有 GUI 事件。

2) This is the proper solution. 2)这是正确的解决方案。 You can register for broadcast events after the network connection has been established.您可以在建立网络连接后注册广播事件。 Then you will get an event when it finishes.然后当它完成时你会得到一个事件。 From this event you can finish performing whatever operations are needed.从这个事件你可以完成执行任何需要的操作。

Sorry for the rushed answer.抱歉匆忙回答。 I am not an Android pro, so I can't explain the details as to how to do this off the top of my head, but I thought I would at least point you in the right direction.我不是 Android 专业人士,所以我无法解释如何做到这一点的细节,但我想我至少会为您指明正确的方向。

Actually if you connect WiFi more than one time it will solve your issue.实际上,如果您多次连接 WiFi,它将解决您的问题。

One other thing I see if my WiFi is enabled and I connect to a specific WiFi network then it's working.另一件事我看到我的 WiFi 是否已启用并且我连接到特定的 WiFi 网络然后它正在工作。

One other thing when I switch from mobile network to a specific WiFi network then it gives an unstable connection ..for this problem I connect WiFi through a specific WiFi network and then forget the network after a 3 second delay I again connect.另一件事,当我从移动网络切换到特定的 WiFi 网络时,它会导致连接不稳定..对于这个问题,我通过特定的 WiFi 网络连接 WiFi,然后在 3 秒延迟后忘记网络,我再次连接。 Then it works properly.然后它可以正常工作。

I use this code for connecting to WiFi.我使用此代码连接到 WiFi。

And for delay and for got WiFi network I use this code... .......对于延迟和获得 WiFi 网络,我使用此代码......

{ 
   wifi(SSID,PASS); 
   final Handler handler = new Handler();
   handler.postDelayed( 
       new Runnable() 
       { 
          Override public void run() { forgot(); } 
       }, 3000); 
   final Handler handler1 = new Handler();
   handler1.postDelayed(
       new Runnable() 
       {
          Override public void run() {wifi(SSID,PASS); } 
       }, 3000);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM