1

I am trying to detect if the WiFi is connected or not by listening to the "SUPPLICANT_CONNECTION_CHANGE_ACTION" as shown below in the code. But the problem is when i run the App i receive no notificatio from the broadCast Receiver i am registered to!!

why that is happening and how to solve it?

code:

IntentFilter intentFilter2 = new IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ConnectivityModule();
}

protected void ConnectivityModule() {
    // TODO Auto-generated method stub
     Log.d(TAG, "@interNetConnectivityModule: called");
    registerReceiver(SupplicantReceiver, intentFilter2);
}

BroadcastReceiver SupplicantReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        final String action = intent.getAction();

        if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
            SupplicantState supplicantState = (SupplicantState)intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);

            if (supplicantState == (SupplicantState.COMPLETED)) { 
                Log.d(TAG, "@SupplicantReceiver: connected");
            }

            if (supplicantState == (SupplicantState.DISCONNECTED)) {
                Log.d(TAG, "@SupplicantReceiver: not connected");
            }
        }
    }
};
rmaik
  • 1,076
  • 3
  • 15
  • 48

2 Answers2

0

Here is Example :

Main Activity

   import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.View;

public class MainActivity extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
   }
   // broadcast a custom intent. 
   public void broadcastIntent(View view)
   {
      Intent intent = new Intent();
      intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
      sendBroadcast(intent);
   }
}

My Broadcast Receiver :

 import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
   }

}

This is how you should declare Broadcast Receiver in your Manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.helloworld"
   android:versionCode="1"
   android:versionName="1.0" >
   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="15" />
   <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
       <activity
           android:name=".MainActivity"
           android:label="@string/title_activity_main" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER"/>
           </intent-filter>
       </activity>
       <receiver android:name="MyReceiver">
          <intent-filter>
          <action android:name="com.tutorialspoint.CUSTOM_INTENT">
          </action>
          </intent-filter>
      </receiver>
   </application>
</manifest>

Main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <Button android:id="@+id/btnStartService"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/broadcast_intent"
   android:onClick="broadcastIntent"/>

</LinearLayout>
Bhaskar
  • 911
  • 6
  • 18
0

Your receiver looks correctly registered (at runtime, not based on Manifest, but whatever, should be good anyway).

I'm guessing.. have you tried putting logs in onReceive method like

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    Log.d(TAG, "Reached this point, receiver is working");
    final String action = intent.getAction();

    if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
        SupplicantState supplicantState = (SupplicantState)intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);

        if (supplicantState == (SupplicantState.COMPLETED)) { 
            Log.d(TAG, "@SupplicantReceiver: connected");
        }

        if (supplicantState == (SupplicantState.DISCONNECTED)) {
            Log.d(TAG, "@SupplicantReceiver: not connected");
        }

        Log.d(TAG, "Checking for an error in retrieving data!");
        boolean myTest = intent.getBooleanExtra(EXTRA_SUPPLICANT_CONNECTED, false);

        if (myTest) Log.d(TAG, "This way it worked!"); 
          else Log.d(TAG, "This does not mean it didn't work at all! Might just be the correct value as a DISCONNECTED status");

    } else Log.d(TAG, "Bizzarre error, action received was different from the one receiver was registered to! [ " + action + " ] ");

}

My guess is that you might be trying to retrieve the information in a wrong way, and with logs so strictly defined you can't see enough of what happens, but the receiver works fine

FrancescoC
  • 1,058
  • 10
  • 19
  • thanks, I tried your answer but stil the same thing again, when i turn the wifi on and off, i receive no notification – rmaik Mar 13 '15 at 11:48
  • i tried to make a new Android project with the code given plus my enhanced logs. no permissions set in the manifest. result is that the receiver works, but the logs you initially used (SupplicantState.COMPLETED and DISCONNECTED) never fire off, because supplicantState is always null. instead, each of my logs are being fired correctly – FrancescoC Mar 13 '15 at 12:06