Creating P2P Connections with Wi-Fi on Android: creating BroadcastReceiver class

I am trying to create Wi-Fi P2P connection on Android. I am referring to the API but I am stuck on creating the BroadcastReceiver class.

It says: "Now create a new BroadcastReceiver class that you'll use to listen for changes to the System's Wi-Fi P2P state. In the onReceive() method, add a condition to handle each P2P state change listed above."

public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
        // Determine if Wifi P2P mode is enabled or not, alert
        // the Activity.
        int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
        if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
        } else {
    } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {

        // The peer list has changed! We should probably do something about
        // that.

    } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {

        // Connection state changed! We should probably do something about
        // that.

    } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
        DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
        fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(


Now I have the following questions regarding this API code snippet:-

1) What is the activity used in the above code.Both mManager and mChannel have been defined before use in the API snippet, but I am unable to find where activity has been defined.

2) To resolve the above issue, I had a class description as follows

class MyBroadcastReceiver(
        val manager: WifiP2pManager,
        val channel: WifiP2pManager.Channel,
        val activity: PlayOverNetworkActivity
): BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action

        when(action) {
            WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION -> {
                // Determine if Wifi P2P mode is enabled or not, alert the activity
                val state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1)
                if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
                } else {

In the above code, I am not finding any method setIsWifiP2pEnabled(). So, do I need to define this method or am I doing this wrong in the first place by passing the activity PlayOverNetworkActivity which is actually the activity initialising the mManager and mChannel objects in its onCreate() method.

NOTE: Though I'm writing the code in Kotlin, both Kotlin and Android are very new to me. So, it'd be nice if you use Kotlin for the answer so that I can follow easily.