How do i initialize WifiP2pmanager.channel? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How do i initialize WifiP2pmanager.channel? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

package in.webxstudio.filesharing;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.p2p.WifiP2pManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;




public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

private WifiP2pManager manager;
private WifiP2pManager.Channel channel;
private BroadcastReceiver broadcastReceiver;
private IntentFilter mIntentFilter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    manager=(WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    Log.d(TAG, "onCreate: manager "+manager);
    manager.initialize(this,getMainLooper(),null);
    channel=manager.initialize(this,getMainLooper(),null);
    broadcastReceiver=new WiFiDirectBroadcastReceiver(manager,channel,this);


    mIntentFilter = new IntentFilter();
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

    manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Log.d(TAG, "onSuccess: Peered Discovered");
        }

        @Override
        public void onFailure(int reasonCode) {
            Log.d(TAG, "onFailure: Failure Discovering Peers Status Code "+reasonCode);

        }
    });


}

/* register the broadcast receiver with the intent values to be matched */
@Override
protected void onResume() {
    super.onResume();
    registerReceiver(broadcastReceiver, mIntentFilter);
}
/* unregister the broadcast receiver */
@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(broadcastReceiver);
}
}

I get an error on line number 3 as below..

05-27 20:59:23.807 9498-9498/in.webxstudio.filesharing E/AndroidRuntime: FATAL EXCEPTION: main
    Process: in.webxstudio.filesharing, PID: 9498
    java.lang.RuntimeException: Unable to start activity ComponentInfo{in.webxstudio.filesharing/in.webxstudio.filesharing.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.wifi.p2p.WifiP2pManager$Channel android.net.wifi.p2p.WifiP2pManager.initialize(android.content.Context, android.os.Looper, android.net.wifi.p2p.WifiP2pManager$ChannelListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.wifi.p2p.WifiP2pManager$Channel android.net.wifi.p2p.WifiP2pManager.initialize(android.content.Context, android.os.Looper, android.net.wifi.p2p.WifiP2pManager$ChannelListener)' on a null object reference
        at in.webxstudio.filesharing.MainActivity.onCreate(MainActivity.java:28)

What is wrong in my code?

Answer

For anyone who have the same problem. Please make sure you are running the project with real android device not emulator.

We are here to answer your question about How do i initialize WifiP2pmanager.channel? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji