Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WIFI How To Detect When Specific WIFI Connection is Available

Tags:

android

wifi

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?

like image 608
ECH5030 Avatar asked Feb 19 '12 20:02

ECH5030


People also ask

How do I see available WiFi networks on Android?

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.

How do I know if WiFi is available?

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.

How do I automatically connect to a specific WiFi?

Open Settings. Click on Network & Internet and choose WiFi. Scroll down and tap WiFi Preferences. Select Turn on WiFi automatically.

Does Android automatically switch to strongest WiFi?

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.


2 Answers

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;     } } 
like image 61
petrsyn Avatar answered Sep 28 '22 07:09

petrsyn


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.

  • Sources you should read up and study

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

  • Permissions you should ask for when creating application manifest

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)

  • You should also declare, that your application needs wifi to be available in device, to work properly:

AndroidManifest.xml

<uses-feature android:name="android.hardware.wifi" /> 
like image 37
Marek Sebera Avatar answered Sep 28 '22 08:09

Marek Sebera