Question

I'm trying to connect to a WiFi Direct network using Nexus 4.

I have the following code:

public class MainActivity extends ActionBarActivity {

    WifiP2pManager mWifiP2pManager;
    Channel mChannel;
    WiFiDirectBroadcastReceiver mReceiver;
    IntentFilter mIntentFilter;

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



        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);

        mWifiP2pManager = (WifiP2pManager)getSystemService(WIFI_P2P_SERVICE);
        mChannel = mWifiP2pManager.initialize(this, getMainLooper(), null);

        ((ImageButton)findViewById(R.id.imageButton_power)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("p2p", "discoverPeers() called");

                mWifiP2pManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {

                    @Override
                    public void onSuccess() {
                        Log.d("p2p", "discoverPeers() Success");
                    }

                    @Override
                    public void onFailure(int reason) {
                        Log.d("p2p", "discoverPeers() Failure: " + reason);
                    }
                });
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mReceiver = new WiFiDirectBroadcastReceiver(mWifiP2pManager, mChannel, this);
        registerReceiver(mReceiver, mIntentFilter);

    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

}

And the BroadcastReceiver:

public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {

    private WifiP2pManager mManager;
    private Channel mChannel;
    private MainActivity mActivity;

    public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel, MainActivity activity) {
        super();
        this.mManager = manager;
        this.mChannel = channel;
        this.mActivity = activity;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        Log.d("p2p", "Action: " + action);

        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            // Check to see if Wi-Fi is enabled and notify appropriate activity
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            Log.d("p2p", "Requesting for peers");

            if (mManager != null) {
                mManager.requestPeers(mChannel, peerListListener);
            }
        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            // Respond to new connection or disconnections
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            // Respond to this device's wifi state changing
        }
    }

    private WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() {
        @Override
        public void onPeersAvailable(WifiP2pDeviceList peers) {
            Log.d("p2p", "deviceCount = " + peers.getDeviceList().size());
        }
    };
}

When I click the button the discoverPeers() returns Success. However, I never get any of the broadcasts specified in the IntentFilter, when I start/resume the activity I get a few broadcasts:

02-26 16:19:08.684 7656-7656/com.example.app D/p2p﹕ Action: android.net.wifi.p2p.STATE_CHANGED 02-26 16:19:08.684
7656-7656/com.example.app D/p2p﹕ Action: android.net.wifi.p2p.CONNECTION_STATE_CHANGE 02-26 16:19:08.684
7656-7656/com.example.app D/p2p﹕ Action: android.net.wifi.p2p.THIS_DEVICE_CHANGED

But none of them are the one's I registered to the IntentFilter and for some reason I get those 3 although they are not added to the filter.

Edit: It seems I got confused with the name of the strings and they're actual value, I do receive some of the actions I added to the IntentFilter.

Was it helpful?

Solution

Then you are actually getting the correct results although they are not as you expected. When you p2p state change you are supposed to see "android.net.wifi.p2p.CONNECTION_STATE_CHANGE" value. And same goes for other filters. Check this page: http://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.html#WIFI_P2P_CONNECTION_CHANGED_ACTION

OTHER TIPS

not sure if you are still following this however Google has added a note in the WifiP2p reference that sheds some light on this issue. (An issue that I was having as well) Found here: http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html

         // Code for when the discovery initiation is successful goes here.
        // No services have actually been discovered yet, so this method
        // can often be left blank.  Code for peer discovery goes in the
        // onReceive method, detailed below.

From this note it appears that you will get the onSuccess() indication IF the discoverPeers() was successful BUT you will only receive the 'WIFI_P2P_PEERS_CHANGED_ACTION' broadcast indicating that the list of available peers has changed IF there is an available peer to detect. I tested this with two smart phones running a wifiP2p app.

I hope this help, if at least you may find you answer in the google references.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top