I need to detect when I have network connectivity to a SPECIFIC WIFI network.
For example: As soon as you walk into your house, and your phone picks up your home WiFi network, I would like a notification that says "You are not at your home network, would you like to connect to you Home?" But I would like that to only happen when I am at my specific house.
What should I listen for and what tests should I do to make sure it is my specific home network, and not another network?
Return to your Android device's Settings > Wireless & Networks > Wi-Fi panel and tap Wi-Fi Settings. Find your network's name (SSID) on the list of nearby Wi-Fi networks. If your network's name is not on the list, the AP or router may be hiding its SSID. Click Add Network to configure your network name manually.
After your Surface restarts, sign in to Windows. Go to Start, and select Settings > Network & internet > Wi-Fi > Show available networks, and see whether your wireless network name appears in the list of available networks. If you see your wireless network name, select it and select Connect.
Open Settings. Click on Network & Internet and choose WiFi. Scroll down and tap WiFi Preferences. Select Turn on WiFi automatically.
What this feature does is automatically switch between wireless and mobile data, depending upon which has the best connectivity and signal strength. In other words, you might be bouncing between mobile data and wireless networks, but your device will always (automatically) remain on the strongest network.
You can use BroadcastReceiver to find out that wifi network has changed:
BroadcastReceiver broadcastReceiver = new WifiBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); context.registerReceiver(broadcastReceiver, intentFilter);
The BroadcastReceiver may look like this. And to check for specific MAC address see the checkConnectedToDesiredWifi() method bellow.
public class WifiBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION .equals(action)) { SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE); if (SupplicantState.isValidState(state) && state == SupplicantState.COMPLETED) { boolean connected = checkConnectedToDesiredWifi(); } } } /** Detect you are connected to a specific network. */ private boolean checkConnectedToDesiredWifi() { boolean connected = false; String desiredMacAddress = "router mac address"; WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifi = wifiManager.getConnectionInfo(); if (wifi != null) { // get current router Mac address String bssid = wifi.getBSSID(); connected = desiredMacAddress.equals(bssid); } return connected; } }
As long, as we are nothing like code as you need, for free service, I can only recommend you, to read everything possible about Android and its Network/Wifi possibilities, when creating such app.
http://developer.android.com/reference/android/net/wifi/package-summary.html
how to see if wifi is connected in android
How to get my wifi hotspot ssid in my current android system
How to get name of wifi-network out of android using android API?
Get Wifi Interface name on Android
AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
(the last one, only if you want it to detect your location, to prevent unnecessary calls)
AndroidManifest.xml
<uses-feature android:name="android.hardware.wifi" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With