Android Tutorial Android Wifi-Direct Tutorial

Android 4+ devices support Wifi Direct (Wifi P2p). Wifi Direct allows you to connect two devices over wireless without an access point. It is similar to Bluetooth with a much extended range and performance.

It is recommended to use Android 4.1+ devices as there are several major known issues with Android 4.04 devices and Wifi Direct.


Establishing a connection over wifi direct is done in two main steps. First you need to discover and make a peer to peer connection. This step ends when you have the IP address of the other device.

In the second step you use the Network library (together with AsyncStreams) to create a connection with the other device IP address.

Creating the peer to peer connection

First we should find the nearby peers by calling manager.DiscoverPeers.
The PeersDiscovered event will be raised when the process completes.
This event includes a list with the discovered devices. Note that this event will also be raised during the connection process so your code should be ready to handle multiple calls of this event.

Once we have a device we can call manager.Connect with the device MAC address. The ConnectionChanged event will be raised.

This event includes the group owner IP address. If the address is "" then the current device is the owner. You should use a ServerSocket to accept incoming connections.

Otherwise you need a Socket to connect to the group owner ip.

The attached example implements a wifi direct connection and then allows you to send a message from one device to the other. The implementation is done with a Service. It is easier to use a Service for such tasks as the service will not be destroyed like the activity.

BTW, I did a small test to check the maximum range until the connection broke. The result was about 40 - 50 meters (tested with Galaxy Nexus and Nexus 4).

The library is available here:


    8.7 KB · Views: 16,050


Active Member
Licensed User
If I look into the variable "device" with the debugger, it seems to be an object with many properties. But just name and mac address are available with getters.


Well-Known Member
Licensed User
I'm trying your demo and I have only one question (until now) :)

The accept connection message is raised only the first time, like bluetooth paring ?


Well-Known Member
Licensed User
I'm trying to understand your tutorial and it is quite clear for me, except the GroupOwnerIp parameter that is passed in the _ConnectionChanged event.

Tell me if I wrong :
1) I understand that it's the server IP. This IP is used by the client to create the socket connection.
2) GroupOwnerIp can assume loopback IP address ( or other network address.

2) I don't know which is the rule that set the GroupOwnerIp IP address.


Well-Known Member
Licensed User
1) Yes.
2) The "server" device GroupOwnerIp address will always be The server should use a ServerSocket to accept incoming connections.

So the ServerSocket can accept multiple connections from other devices ?

Gaver Powers

Licensed User
Has anyone connected to a wifi enabled arduino using this yet?


New Member
Licensed User

how do I see the name of my ” wi-fi direct” when I establish a connection? I can only see the device name found but not mine.

Best Regards,


New Member
Licensed User
This is the configuration of Wi-Fi Direct from my Smartphone, and enables me to rename "Jansenx".
I'm trying to get this name in my application. Is this possible?




Licensed User
Hi Erel,

Help , Could you develop more/detail functions for wifi direct library?

I want to create the code from follow java code . but I can not find those struct...

  1. config.wps = wps;
  2. config.deviceAddress = mConnectingDevice.deviceAddress;
  3. config.groupOwnerIntent = WifiP2pConfig.MIN_GROUP_OWNER_INTENT;
  4. ...
  5. mWifiP2pManager.connect(mWifiP2pChannel, config, new ActionListener() {


Licensed User
Dear Erel,

I want make a wifi display app , I need set Wifi to WFD mode and wait connecting.

There is sample code , I would move to a java project for whole function if B4A can not finish it.

private WifiManager n;
private WifiP2pManager t;
private WifiP2pManager.Channel u;
private Timer g = null;

this.n = ((WifiManager)getSystemService("wifi"));
if (this.n != null)
Log.d("",Get WifiP2pManager instance successfully");
this.t = ((WifiP2pManager)getSystemService("wifip2p"));
if (this.t != null)
Log.d("", "Get WifiP2pManager instance successfully");
this.u = this.t.initialize(this, getMainLooper(), null);
Log.d("", "WifiP2pManager initialization started");

WifiP2pWfdInfo localWifiP2pWfdInfo = new WifiP2pWfdInfo();
this.t.setWFDInfo(this.u, localWifiP2pWfdInfo, new g(this));