Search code examples
androidbroadcastreceiverandroid-wifi

Toast android phone with welcome greeting message when a wireless network is available


I am building an android app which once installed helps automating some of my home controls. As the first simple feature of the app, i want the phone to toast me with welcome message whenever i enter my home. I thought i could implement this by putting in a BroadCastReceiver with action name "android.net.wifi.WIFI_STATE_CHANGED" and i assumed, whenever a new network is available, the broadcast receiver will be notified and i could check the SSID to see if i have arrived home.

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.myfirstapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name="com.example.myfirstapp.WifiNetworksAvailableBroadcastReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>

    <activity
        android:name="com.example.myfirstapp.WifiTester"
        android:label="@string/title_activity_wifi_tester" >
    </activity>
</application>

</manifest>

My WifiNetworksAvailableBroadcastReceiver looks like:

public class WifiNetworksAvailableBroadcastReceiver extends BroadcastReceiver {

protected static final String TAG = "MyApp";

@Override
public void onReceive(Context context, Intent intent) {     
            WifiManager mMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    Log.i(TAG, "------");
    for(final ScanResult result:mMgr.getScanResults()) {
        if(result.SSID.equals("<MY_HOME_WIFI_SSID>")) {
            Toast.makeText(context, "Welcome home", Toast.LENGTH_LONG).show();
                    }
    }
    Log.i(TAG, "======");
}
} 

Now with the app installed. I enter my home but i do not get the toast. I tried running the app in the background but still i did not get the toast. Could anyone give an direction? Is my approach correct?


Solution

  • Try to change the receiver with the event SCAN_RESULTS_AVAILABLE_ACTION. Your receiver will be called every time a new wifi scan has been done, then you can check if your SSID is present in the list of available ones calling getScanResults() and eventually show the toast.

    PS. Have you registered your receiver?