Note that there are some explanatory texts on larger screens.

plurals
  1. POWifiManager sets SSID passed as Disabled instead of Connecting
    primarykey
    data
    text
    <p>I have some code that is supposed to take the SSID passed via an extra in the intent and connect to it, <strong>currently it just sets the ones I pass to it as disabled</strong>. All of the SSID's im trying to pass have already been connected to before. I' m not sure if its cause there is already an existing network configuration with the SSID, but it may have something to do with it...</p> <pre><code>package com.wt.checkin; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.os.Bundle; import android.util.Log; public class WifiSwitcher extends BroadcastReceiver { @SuppressWarnings("static-access") @Override public void onReceive(Context arg0, Intent arg1) { Bundle extras = arg1.getExtras(); WifiManager wifiMan = (WifiManager) arg0 .getSystemService(Context.WIFI_SERVICE); if (wifiMan.getWifiState() == 1) { wifiMan.setWifiEnabled(true); try { Thread.currentThread().sleep(2500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } WifiConfiguration tmpConfig = new WifiConfiguration(); tmpConfig.SSID = extras.getString("SSID"); tmpConfig.status = WifiConfiguration.Status.ENABLED; int netId = wifiMan.addNetwork(tmpConfig); wifiMan.enableNetwork(netId, true); } } </code></pre> <p><img src="https://i.stack.imgur.com/0qjwz.png" alt="Disabled"> <strong>Here is the working code:</strong></p> <pre><code>package com.wt.checkin; import java.util.List; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; import android.util.Log; public class WifiSwitcher extends BroadcastReceiver { @SuppressWarnings("static-access") @Override public void onReceive(Context arg0, Intent arg1) { Bundle extras = arg1.getExtras(); WifiManager wifiMan = (WifiManager) arg0 .getSystemService(Context.WIFI_SERVICE); if (wifiMan.getWifiState() == 1) { wifiMan.setWifiEnabled(true); try { Thread.currentThread().sleep(2500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }** WifiConfiguration Config = new WifiConfiguration(); WifiInfo Info = wifiMan.getConnectionInfo(); String SSID = extras.getString("SSID"); Config.SSID = "\"" + SSID+"\""; wifiMan.addNetwork(Config); List&lt;WifiConfiguration&gt; list = wifiMan.getConfiguredNetworks(); for( WifiConfiguration i : list ) { if(i.SSID != null &amp;&amp; i.SSID.equals("\"" + SSID + "\"")) { wifiMan.disconnect(); wifiMan.enableNetwork(i.networkId, true); wifiMan.reconnect(); break; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload