-4

I've been trying to create an app that will recognize when my phone is connected to a desired network and when it is, then it will add a number to a variable and perform tasks such as start a new app. I have looked around and haven't found a good answer.
Also, I've checked the android docs and I haven't been able to figure out how to do that.
I know that by using the isConnected method I can find out if I'm connected to a WiFi network but it doesn't tell me if it is the one I want.
In this direction, I have read that one can possibly achieve this by finding the mac address or the bssid of the network. , P.s coding on android using AIDE

Thanks in advance!!

Andrei T
  • 2,985
  • 3
  • 21
  • 28

2 Answers2

0

You should start first by finding your mac address from your router, etc.
Next, you filter that on your android device:
1. Can I find the MAC address of my Access point in Android?.
2. Get MAC Address without connect to WiFi.
Once you managed to do that that, next you can perform the operations you want.

Community
  • 1
  • 1
Andrei T
  • 2,985
  • 3
  • 21
  • 28
0

What you're looking for is call broadcast receiver. basically, you create a custom broadcast receiver that will receive message on some actions. you define those action in an intent filter :

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);

then register your receiver on this intent filter :

registerReceiver(wifiWatcher, intentFilter);

and your receiver will implement the onReceive method :

public void onReceive( Context context, Intent intent )

ConnectivityManager conMngr = (ConnectivityManager)this.getSystemService(this.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = conMngr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  String ssid = null;
  NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  if (networkInfo.isConnected()) {
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
    if (connectionInfo != null && !StringUtil.isBlank(connectionInfo.getSSID())) {
      ssid = connectionInfo.getSSID();
    }
  }

Well, do what you want if the correct SSID

if (ssid!=null && ssid.equals(mySSID){
//DO
}

to do that, you need to add those permissions in your manifest :

  <user-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <user-permission android:name="android.permission.ACCESS_WIFI_STATE" />
user2413972
  • 1,355
  • 2
  • 9
  • 25