I have an app in which whenever users is in range of a particular Wifi. I want to give a notification to the user he is in the wifi zone and can connect to this wifi and start the app. This wifi is an open wifi.
I tried using broadcast receiver with statically registering in AndroidManifest.xml.
AndroidManifest.xml
<receiver android:name=".WifiReceiver" android:enabled="true" android:exported="true">
<intent-filter android:priority="100">
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
WifiReceiver:
if (_wifiManager == null)
_wifiManager = (WifiManager)context.GetSystemService(Context.WifiService);
if (_wifiManager.IsWifiEnabled)
{
var results = _wifiManager.ScanResults;
if (results.Any(x => x.Ssid == "MyWifiName"))
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.SetSmallIcon(Resource.Drawable.ic_launcher_rounded)
.SetContentText("Your are in wifi zone. Start the app")
.SetContentTitle("My App");
NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
notificationManager.Notify(1,mBuilder.Build());
}
}
Now this works only when either I change the currently connected network or if the wifi state is changed i.e it is turned on or off.
Precisely I want to detect if a new wifi (Open) network becomes available I want to check its name and if it is my wifi I want to show the notification.
I am not inclined in using a service that would be my last resort using an alarm
EDIT: how does android show the notification for open wifi networks are available. The ideal case for me to search if wifi is available is whenever android notifies this.