Show the updated Wi-Fi and Bluetooth status in Android

Hi, Everyone Wish you happy new year 2017. Here we going to see about the show the updated Wi-Fi and Bluetooth status using broadcast receiver.

Add the permissions below in AndroidManifest.xml
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.BLUETOOTH" />

In your Activity, Add this,

/* Wifi Status*/
    private BroadcastReceiver myWifiReceiver
            = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            NetworkInfo networkInfo = (NetworkInfo) arg1.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                DisplayWifiState();
            }
        }
    };

/*Bluetooth Status*/
private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                        BluetoothAdapter.ERROR);
                switch (state) {
                    case BluetoothAdapter.STATE_OFF:
                        txt_bluetooth.setText("Bluetooth off");
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
                        txt_bluetooth.setText("Turning Bluetooth off...");
                        break;
                    case BluetoothAdapter.STATE_ON:
                        txt_bluetooth.setText("Bluetooth on");
                        break;
                    case BluetoothAdapter.STATE_TURNING_ON:
                        txt_bluetooth.setText("Turning Bluetooth on...");
                        break;
                }
            } else {
                txt_bluetooth.setText("Bluetooth off");
            }
        }
    };

/*Method for Display wifi status*/
private void DisplayWifiState() {

        ConnectivityManager myConnManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo myNetworkInfo = myConnManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        WifiManager myWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
        String macAddress, IPaddress, ssId, BssId, speed, rssi, connected;

        macAddress = myWifiInfo.getMacAddress();

        if (myNetworkInfo.isConnected()) {
            int myIp = myWifiInfo.getIpAddress();
            connected = " CONNECTED ";

            int intMyIp3 = myIp / 0x1000000;
            int intMyIp3mod = myIp % 0x1000000;

            int intMyIp2 = intMyIp3mod / 0x10000;
            int intMyIp2mod = intMyIp3mod % 0x10000;

            int intMyIp1 = intMyIp2mod / 0x100;
            int intMyIp0 = intMyIp2mod % 0x100;

            IPaddress = String.valueOf(intMyIp0)
                    + "." + String.valueOf(intMyIp1)
                    + "." + String.valueOf(intMyIp2)
                    + "." + String.valueOf(intMyIp3);

            ssId = myWifiInfo.getSSID();
            BssId = myWifiInfo.getBSSID();

            speed = String.valueOf(myWifiInfo.getLinkSpeed()) + " " + WifiInfo.LINK_SPEED_UNITS;
            rssi = String.valueOf(myWifiInfo.getRssi());
            txt_wifi.setText(connected + "\n" + " Mac Address : " + macAddress + "\n" + " IP Address : " + IPaddress + "\n"
                    + " SSID : " + ssId + "\n" + " BSSID : " + BssId + "\n" + " Speed : " + speed + "\n" + " RSSI : " + rssi);
        } else {
            connected = " DIS-CONNECTED! ";
            txt_wifi.setText(connected);
        }

    }

In your OnCreate(), just add this,
//For Wifi
 DisplayWifiState();
 this.registerReceiver(this.myWifiReceiver,
                new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

//For Bluetooth
this.registerReceiver(bluetoothReceiver,  new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));

To Unregister the status:
public void onDestroy() {
   super.onDestroy();
   unregisterReceiver(bluetoothReceiver);
 }

Need Code, Just Visit my Github

Comments

Popular posts from this blog

Flutter Bloc - Clean Architecture

What's new in android 14?

Dependencies vs Dev Dependencies in Flutter