Android Question BroadcastReceiver for checking WiFi connectivity

Harish Kumar Arya

Member
Licensed User
Longtime User
I would like to register a broadcast receiver for checking wifi connection in my app (within main activity). I came across a sample of java implementation of the same but having hard time transforming it to B4A. Can someone please help me out with this:

Manifest XML:

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

JAVA code

this.registerReceiver(this.mConnReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {

NetworkInfo currentNetworkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

if (currentNetworkInfo.isConnected()) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();

Toast.makeText(getApplicationContext(), "Broadcast Connected:" + wifiInfo.getSSID(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Broadcast Not Connected", Toast.LENGTH_LONG).show();
}
}
};
 
Top