2

I am able to connect successfully with the android device with the following wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=root
update_config=1
device_name=DIRECT-Pi
persistent_reconnect=1

network={ ssid="DIRECT-Pi" bssid=fc:ar:8e:71:b6:35 proto=RSN key_mgmt=WPA-PSK pairwise=CCMP auth_alg=OPEN mode=3 disabled=2 }

I started wpa_cli in interactive mode. Then adding a persistent group with p2p_group_add cmd as follows

p2p_group_add  persistent=0

enabling the pin by wps pin as follows

wps_pin any 12345670

with this I was able to connect a android device with Respberry pi automatically (with out giving pin in android device repeatedly).

But when I restart the pi and run the same command p2p_group_add persistent=0 not able to connect device with the preexisting persistent group.

Ingo
  • 42,107
  • 20
  • 85
  • 197
arayan
  • 21
  • 2

1 Answers1

3

[EDITED AFTER FINDING A SOLUTION]

To summarize the issue, when restarting wpa_supplicant on the Raspberry Pi and reinvoking a previously defined persistent group, the P2P Clients (e.g., Android phones) are no more able to rejoin the saved group with fast reconnection and a new enrolling process is started instead, so that the saved group on the Android phones remains useless.

The problem occurs because, to improve security, the WLAN device driver of the Raspberry Pi 802.11 wireless interface uses randomized MAC addresses by default when starting network interfaces. Anyway, when using Wi-Fi Direct P2P persistent groups, MAC addresses shall not vary in order to avoid breaking the group restart.

A new version of wpa_supplicant now supports p2p_device_random_mac_addr=2, which allows correctly managing persistent groups on a Raspberry Pi, by reusing a previously saved MAC address.

How to compile, install and use this option is described here.

See also:


The following note shortly describes how to use a Wi-Fi Direct P2P persistent group.

A Wi-Fi Direct persistent group can be for instance created by manually editing the wpa_supplicant.conf configuration shown in the question, defining it as the first network profile (n. 0).

Add p2p_device_random_mac_addr=2 to wpa_supplicant configuration file.

Use the following commands to start the persistent group via wpa_cli:

wpa_cli -i p2p-dev-wlan0
set config_methods keypad

set config_methods keypad means that you want to enter a PIN with your smartphone and confirm it with your Rpi.

p2p_group_add persistent=0
interface

interface lists the available interfaces; you need to find the one starting with p2p-wlan...

interface <group_name>

For instance, interface p2p-wlan0-0.

set config_methods keypad

Then, on the Android client:

Settings > Wi-Fi > Wi-Fi Direct; as soon as the SSID appears, select it and enter the PIN (say for instance 00000000, 8 digits).

Immediately after receiving the WPS-ENROLLEE-SEEN event, on that newly created interface issue the following:

wps_pin any 00000000

After some seconds, the device gets connected.

Alternatively:

wpa_cli -i p2p-wlan0-0 wps_pin any 00000000

Notice the used interface name.

When the connection is activated, a new saved group appears on the Wi-Fi Direct Android panel, to allow fast reconnection. In fact, if you now disconnect the link on the Android phone and try to reconnect it, including Android reboot and Raspberry Pi reboot, the connection becomes immediately active without password request (i.e., reusing the same saved group).

ircama
  • 178
  • 1
  • 7
  • Very helpful, thanks for posting this. Just for clarity, which version of Android was this for? – Jortstek Nov 07 '21 at 21:16
  • 1
    Any Android version supporting Wi-Fi Direct works. I tested Android 11, 10, 9, 8, 7 and 6 smartphones. Wi-Fi Direct is present in most smartphones with at least Android 4.0 (API level 14); notice anyway that only recent Android versions support the local saving of persistent groups. With some Android 6 and 7 devices (depending on the ROM), enrolment is always needed when connecting a persistent group. Additional notes here. – ircama Nov 09 '21 at 06:40
  • 1
    A persistent P2P-GO group can provide AP capabilities; also, Wi-Fi Direct does not interfere with the routing table, so you might have cellular and WiFi concurrently active with Android. The connection to a persistent group is not automatic with the Android phone: you need to click on the persistent group to connect to the Ubuntu GO. Anyway, a reconnection to the group is done without repeating the WPS authorization process. – ircama Nov 11 '21 at 01:07
  • (Deleted my last comment cos I can't edit it and there was a glaring mistake which made it confusing. I meant I do want it to reconnect automatically.) @ircama Just to confirm, when you say you need to "click on the persistent group" in Android but "without repeating" the authorization process, that means click on it every time you lose and regain connectivity, i.e. every time the two devices come back in range of each other? I've seen this question asked elsewhere but never seen a clear answer. If it really does require constant Bluetooth-style pairing, that is going to be a deal-breaker. – Jortstek Nov 11 '21 at 10:35
  • 2
    Once the P2P persistent group is formed, it is possible to connect to it both using WiFi Direct (which requires a confirmation when connecting through the Android user interface) and also in Infrastructure AP mode, which is the same way as for traditional infrastructure-mode Access Points.

    That said, It is not very clear to me your use case, whether you interface WiFi direct from the Android user interface or from an App and why you need to opt for WiFi Direct over infrastructure AP mode networking. I suggest you some simple tests, which will be much clearer to you than many comments.

    – ircama Nov 12 '21 at 07:52
  • Believe me, I have been testing, and even got it working except automatic reconnection. My use case is for $ANDROID to reconnect without interaction and simultaneously to both (1) $ROUTER in AP mode and (2) $UBUNTU in whatever mode will allow an SSH connection. Is that clearer? I know that I can connect the two via the IPs provided by $ROUTER but I want to do it directly for occasions when the router is not available. If I understand your latest comment (thanks), it sounds like this will be impossible, since jAP mode would be required and Android cannot connect to two APs at once. Right? – Jortstek Nov 12 '21 at 08:42
  • PS Would be absolutely prepared to use an Android app to bypass the stock wifi-direct interface if this could make the wifi-direct reconnection automatic. I could not find one when I looked. Might it be an option? – Jortstek Nov 12 '21 at 08:52
  • The Android SDK should enable developing an App that manages WiFi Direct without interaction. – ircama Nov 14 '21 at 04:33
  • Indeed. The main one is called "Netshare no-root-tethering" I am trying it right now and it appears to work great. It creates an AP over WiFi Direct and maintains the connection with a permanent notification. This is all I want. But it is not FOSS and has data limits. So I will not keep it and instead go back to Bluetooth or USB. WiFi Direct has been such a disappointing experience. – Jortstek Nov 14 '21 at 22:36
  • @Jorstek: You can write an Android app that scans for a Wifi service, and re-establishes the connection automatically. If it's your service, and your app, this is entirely doable. Not sure if you can do it from a service in the background. – Robin Davies Apr 07 '22 at 02:43
  • Spent three weeks tearing my hair out over this. I'm not thanking you (since that would be contrary to StackExchange policy). Just letting you know that your answer made a difference. – Robin Davies Apr 07 '22 at 02:48