I created the following test, looking for the WIFI name and the MACADDRESS of who sends me this WIFI.
I would like to "listen" if there is a specific WIFI, so that when I find it, I will do something.
How to do this?
Here is the code I'm using:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Solicita as permissões
String[] permissoes = new String[]{
Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.CHANGE_WIFI_STATE,
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE,
};
PermissionUtils.validate(this, 0, permissoes);
//Getting the connection info
WifiManager wifiManager = (WifiManager) getContext().getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
//Access point's MAC address
String bssid = wifiInfo.getBSSID();
String ssid = wifiInfo.getSSID();
TextView wifi = (TextView) findViewById(R.id.wifi);
TextView mac = (TextView) findViewById(R.id.mac);
if (bssid != null) {
mac.setText(bssid);
}
if (ssid != null && ssid.equals("specific wifi")) {
doSomething();
wifi.setText(wifiInfo.getSSID());
}
}
private void doSomething() {
//TODO something
}
protected Context getContext(){
return this;
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.bob.getwifitest2">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>