Tips & Tricks: WiFi Direct

Hi Android dev’s,

We are back in business at least on the website. We still are having some technical problems with our hardware.

Today we would like to share some information on WiFi Direct with Android.

What is WiFi Direct?
WiFi Direct is a direct connection between two WiFi devices, which at least one is WiFi Direct certified, without any access point in between. It can do peer to peer connections over WiFi, because it will start up an soft AP (Software Access Point). When the connection is established it can do similar things as bluetooth: share files, print files and play together. It has a larger range then bluetooth.

How to implement it in Android?
From Android 4.0 (API Level 14) it is possible to use WiFi Direct in applications.
Everything we need to do from within the application is coming from: WifiP2pManager. With WifiP2pManager you can do initialization, discover, connect and more.

To start you first need to add the following permissions to your Android manifest.

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

After the right permissions are added you need to add a simple broadcast receiver, so your application will receive the events from the Android system.

/**
 * A BroadcastReceiver that notifies of important Wi-Fi p2p events.
 */
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
 
    private WifiP2pManager manager;
    private Channel channel;
    private MyWiFiActivity activity;
 
    public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
            MyWifiActivity activity) {
        super();
        this.manager = manager;
        this.channel = channel;
        this.activity = activity;
    }
 
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
 
        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
            if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
                // Wifi Direct is enabled
            } else {
                // Wi-Fi Direct is not enabled
            }
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            // request available peers from the wifi p2p manager. This is an
            // asynchronous call and the calling activity is notified with a
            // callback on PeerListListener.onPeersAvailable()
            if (manager != null) {
                manager.requestPeers(channel, myPeerListListener);
            }
        } 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
        }
    }
}

Then in your activity add the following:

WifiP2pManager mManager;
Channel mChannel;
BroadcastReceiver mReceiver;
...
@Override
protected void onCreate(Bundle savedInstanceState){
    ...
    mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    mChannel = mManager.initialize(this, getMainLooper(), null);
    Receiver = 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);
    ...
}

Don’t forget to register and unregister the receiver in onResume() and onPause().

This is the first part you have to do, after this you can use the WifiP2pManager to discover peers and to connect to them.
This is done with the following code to discover peers:

manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
    @Override
    public void onSuccess() {
        ...
    }
 
    @Override
    public void onFailure(int reasonCode) {
        ...
    }
});

And for connecting to peers with the following code:

//obtain a peer from the WifiP2pDeviceList
WifiP2pDevice device;
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = device.deviceAddress;
manager.connect(channel, config, new ActionListener() {
 
    @Override
    public void onSuccess() {
        //success logic
    }
 
    @Override
    public void onFailure(int reason) {
        //failure logic
    }
});

We hope these tips and tricks will help you to start with WiFi Direct on Android.

Kind regards,
PullesSon – Android

Tagged , , , , , , . Bookmark the permalink.

Leave a Reply