How to set up a Wifi-Direct connection between Android and Linux

I worked a lot on this problem and here is the answers that I found:

  • Answer 1.1 There are a lot of possibilities and the correct one depends on what you are trying to achieve. You can find the various possibilities here: http://processors.wiki.ti.com/index.php/OMAP_Wireless_Connectivity_NLCP_WiFi_Direct_Configuration_Scripts

I wanted Linux as a GO and Android as a client, so what worked for me in Linux is:

// Kill current supplicant and avoid its automatic recreation
system("mv /usr/share/dbus-1/system-services/fi.* .");
system("killall udhcpd");
system("wpa_cli -i wlan0 terminate -B");
usleep(300000);

// Start new supplicant
system("wpa_supplicant -Dnl80211 -iwlan0 -c /etc/p2p_supplicant.conf -B");
system("wpa_cli -iwlan0 p2p_group_add");
system("ifconfig p2p-wlan0-0 192.168.1.2");

// Connect with a peer
for (;;) {
  system("wpa_cli -ip2p-wlan0-0 wps_pbc");
  system("udhcpd /etc/udhcpd.conf");
  // Here start your TCP server on a port
  Server server([port]);
  server.startServer();
}

N.B. To destroy P2P connection and restart the normal one I use this script:

#!/bin/bash
wpa_cli -i wlan0 terminate -B
wpa_cli -i p2p-wlan0-0 terminate -B
cp ./fi.* /usr/share/dbus-1/system-services/
service network-manager restart

Here the server listens for a connection and the android client connects to it. I used a very easy TCP server found on the first website and it worked like a charm. It's important to start the udhcpd or you won't get the "connected" icon in Android. For the Android side, I followed what I saw on http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html and it worked perfectly.

  • Answer 1.2 From wpa_cli you can get a lot of different warnings and errors. I saw that simply calling the function (like I did in the previous snippet) made everything work despite the warnings. I ignored them.

  • Answer 1.3 The connection using PIN works, when I tried it I wasn't starting the udhcpd and that crashed my connection every time. It was not due to PIN or PBC.

  • Answer 2.1 Both the server and the client are switching interface. It is a very similar behaviour to what you usually do with sockets. You create a listening socket, somebody connects, you create a new socket to handle that client in a separate thread while the listening socket is still there, waiting for other users. You do the same with interfaces. Starting a wpa_cli that monitors the wlan0 interface and one that monitors the p2p-wlan0-0 interface gives a good idea of what is happening during the change.

  • Answer 2.2 The client "went" to the other interface and is now ready to receive an IP. Start the DHCP and TCP servers!

  • Answer 2.3 Don't use static IP. Only only for the server is ok, the client should use a DHCP IP. Maybe there is a way to handle static IPs, but it was not useful for my objectives.

  • Answer 2.4 Done. That was the problem all along

  • Answer 1.4 This works perfectly even with Android/linux.

Sorry if I made some mistake in my answer, I still don't get the whole picture of WiFi-Direct connections, I simply hope that this can help someone else.


(May not be a complete answer, but right direction)

The issue is on the Linux side. I'm encountering similar issues between a Cyanogenmod (Android 5) and Fedora 22, attempting to use Wifi Direct (Wifi P2P) for Intel's Wireless Display Software for Linux OS (WDS).

Specifically, Intel's connman diverged (written from scratch) back in 2009 from what had been standard in Linux, NetworkManager. Connman appears to be the only implementation of Wifi Direct (Wifi P2P) for Linux, and had been written for the embedded (mobile) market.

Due to an RPM issue, connman remains untested on my system, but the software should provide the dbus communications Linux needs.

Website: https://01.org/connman