user1310025
user1310025

Reputation: 91

Connect two devices through Wi-fi Direct

I am trying to connect 2 Android devices through Wi-fi Direct. In my application I am hard coding the MAC address of the other device and calling the method connect. I am assuming that Wi-Fi Direct is on in both the devices. Here is the code I am using:

package com.abc;

import android.app.Activity;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.WpsInfo;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.os.Bundle;
import android.widget.Toast;

public class WiFiDirectActivity extends Activity {
/** Called when the activity is first created. */
protected WifiP2pManager manager;
protected Channel channel;
public WifiP2pConfig config ;
protected final IntentFilter intentFilter = new IntentFilter();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    intentFilter.addAction   (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    intentFilter
            .addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
    manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    channel = manager.initialize(this, this.getMainLooper(), null);
    config = new WifiP2pConfig();
    config.deviceAddress = "78:d6:f0:ab:d9:da";
    config.groupOwnerIntent = 0;
    config.wps.setup = WpsInfo.PBC;

    manager.connect(channel, config, new WifiP2pManager.ActionListener(){
        @Override
        public void onSuccess() {
            Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_LONG);

        }

        @Override
        public void onFailure(int reason) {
            Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG);

        }
    });




}

}

but it is not connecting. What is wrong with my implementation?

Upvotes: 4

Views: 3895

Answers (2)

Diaph
Diaph

Reputation: 136

register a BroadcastReceiver in onResume() and override it. remember to unregister it in onPause()

private class WiFiDirectBroadcastReceiver extends android.content.BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            //TODO
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            //TODO
        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            //TODO
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            //TODO
        }
    }
}

then try to call discoverPeers() first

mWifiP2pManager.discoverPeers(Channel mChannel, ActionListener mActionListener);

if discoverPeers() does find peers, action WIFI_P2P_PEERS_CHANGED_ACTION will be triggered.

we can call requestPeers() in WIFI_P2P_PEERS_CHANGED_ACTION in the BroadcastReceiver

mWifiP2pManager.requestPeers(Channel mChannel, WifiP2pManager.PeerListListener);

so our BroadcastReceiver now looks like this

private class WiFiDirectBroadcastReceiver extends android.content.BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            //TODO
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            mWifiP2pManager.requestPeers(mChannel , pl);
        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            //TODO
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            //TODO
        }
    }
}

to implement WifiP2pManager.PeerListListener, you need to override onPeersAvailable(WifiP2pDeviceList peers)

in onPeersAvailable(), the parameter wifiP2pDeviceList means the peers you discovered

we need a UI object to let us choose which device to connect, so I use spinner here.

also you can use listView or something else.

private List<WifiP2pDevice> mPeers = new ArrayList<WifiP2pDevice>();
spinnerAdapter = new WiFiPeerListAdapter(this, R.layout.row_devices, mPeers);
...
@Override
public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
    mPeers.clear();
    mPeers.addAll(wifiP2pDeviceList.getDeviceList());
    spinnerAdapter.notifyDataSetChanged();
}

finally we can connect to a device

WifiP2pDevice device = spinnerAdapter.getItem((int) mSpinner.getSelectedItemId());
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = device.deviceAddress;
config.wps.setup = WpsInfo.PBC;
mWifiP2pManager.connect(mChannel, config, mActionListener);

after two device connected, BroadcastReceiver action WIFI_P2P_CONNECTION_CHANGED_ACTION will be triggered. so we can do something here.

our BroadcastReceiver now looks like

private class WiFiDirectBroadcastReceiver extends android.content.BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            //TODO
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            mWifiP2pManager.requestPeers(mChannel , pl);
        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
            if (networkInfo != null) {
                Log.d(TAG,networkInfo.toString());
                if (networkInfo.isConnected()) {
                    mWifiP2pManager.requestConnectionInfo(mChannel, WifiP2pManager.ConnectionInfoListener);
                }
            }
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            //TODO
        }
    }
}

btw, the log in action WIFI_P2P_CONNECTION_CHANGED_ACTION will get something like this

NetworkInfo: type: WIFI_P2P[], state: UNKNOWN/IDLE, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true, simId: 0

now we need to implement WifiP2pManager.ConnectionInfoListener and override its abstract method onConnectionInfoAvailable(WifiP2pInfo info) for requestConnectionInfo()

private WifiP2pInfo p2pInfo;
@Override
public void onConnectionInfoAvailable(final WifiP2pInfo info) {
    p2pInfo = info;
    mWifiP2pManager.requestGroupInfo(mChannel, WifiP2pManager.GroupInfoListener);
}

again we need to implement WifiP2pManager.GroupInfoListener and override onGroupInfoAvailable(WifiP2pGroup group)

@Override
public void onGroupInfoAvailable(WifiP2pGroup wifiP2pGroup) {
    String log;
    if(wifiP2pGroup.isGroupOwner()) {
        log = "I am GO";
    }else{
        log = "I am not GO";
    }
    Log.d(TAG, log);
}

now we almost got every info about these two devices

enjoy it

Upvotes: 1

carlosvin
carlosvin

Reputation: 979

I have a similar code working, the main differences are:

  • I get the device address calling before to discovery peers (If you do that then you have to add WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION to intent filter group)
  • I don't set the config.groupOwnerIntent

WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = this.address;
config.wps.setup = WpsInfo.PBC;

Upvotes: 2

Related Questions