Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had exactly the same problem for a project of mine and it took a while to find a solution.</p> <p>First of all, "connecting to a WiFi" is something very abstract, and it turns out rightly so. In practice, people usually mean all of the following:</p> <ul> <li>authenticated with a WiFi access point</li> <li>associated with the access point</li> <li>got an ip address from the network</li> </ul> <p>All these stages (and several more) are associated with different Andoid events. So, without further ado, here is my (slightly modified) code:</p> <pre><code>public class MyService extends Activity { // or Service //... Other stuff BroadcastReceiver awaitIPAddress = null; private final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) { if (intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE) == SupplicantState.COMPLETED) { //WiFi is associated WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo wi = wifiManager.getConnectionInfo(); if (wi != null) { // Wifi info available (should be, we are associated) if (wi.getIpAddress() != 0) { // Lucky us, we already have an ip address. // This happens when a connection is complete, e.g. after rekeying if (wi.getBSSID().equals("c0:ff:ee:c0:ff:ee")) { // ... Do your stuff here // ... // ... } } else { // No ip address yet, we need to wait... // Battery friendly method, using events if (awaitIPAddress == null) { awaitIPAddress = new BroadcastReceiver() { @Override public void onReceive(Context ctx, Intent in) { WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo wi = wifiManager.getConnectionInfo(); if (wi != null) { if (wi.getIpAddress() != 0) { if (wi.getBSSID().equals("c0:ff:ee:c0:ff:ee")) { // ... Do your stuff here // ... // ... } } } else { ctx.unregisterReceiver(this); awaitIPAddress = null; } } }; // We register a new receiver for connectivity events // (getting a new IP address for example) context.registerReceiver(awaitIPAddress, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } } } } else { // wifi connection not complete, release ip address receiver if registered if (awaitIPAddress != null) { context.unregisterReceiver(awaitIPAddress); awaitIPAddress = null; } } } } }; //... Other stuff @Override public void onCreate() { super.onCreate(); //... Other stuff IntentFilter filter = new IntentFilter(); filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); registerReceiver(receiver, filter); //... Other stuff } //... Other stuff } </code></pre> <p>Also, don't neglect the appropriate permissions to the manifest:</p> <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt; &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt; </code></pre> <p>And I strongly suspect you will also need:</p> <pre><code>&lt;uses-permission android:name="android.permission.INTERNET"/&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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