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

I want to connect two devices using Wifi-Direct.
One is a pc with Linux (during my test I'm using Linux Mint 17.2), the other is an Android smartphone (Samsung Galaxy S3 with Android 4.3. Not rooted and don't want to root).
Reading a number of guides on the internet, I decided to use wpa_supplicant in Linux and to create an app for Android.
This is one of the best guides I found for wpa_supplicant:
https://web.archive.org/web/20210114180304/https://processors.wiki.ti.com/index.php/OMAP_Wireless_Connectivity_NLCP_WiFi_Direct_Configuration_Scripts
When I run iw list I see that the "Supported interface modes:" allows P2P_client and P2P_GO.
My wpa_supplicant.conf contains:
ctrl_interface=/var/run/wpa_supplicant
update_config=1
ap_scan=1
device_name=My-pc
device_type=1-0050F204-1
driver_param=use_p2p_group_interface=1
p2p_go_intent=15
p2p_go_ht40=1
The app is very similar to this example (it's from a book) https://github.com/retomeier/Wrox-ProfessionalAndroid-4E/blob/9741a8b62005d49519b1decfea21e7213fdd94a3/Snippets_ch18/app/src/main/java/com/professionalandroid/apps/myapplication/WiFiDirectActivity.java
It works as expected. When I try it with two Android devices, they can see each other and connect (it appear the Wifi-Direct icon).
Now I am trying to connect Android and Linux, but I guess I don't really understand how wpa_supplicant works.
I also saw this question that is very similar to what I asked, but it doesn't really answer my doubts...
Direct Wifi Communication between android smartphone and other devices
My problem is that I can't understand how to do the handshake between the two devices and how to set up the network.
Wpa_supplicant is my problem. I run:
sudo wpa_supplicant -wlan0 -Dnl80211 -c/etc/wpa_supplicant.conf
sudo wpa_cli
But I had a lot of problems:
The device_name (and other parameters) weren't set as I specifiend in the .conf file
wpa_supplicant continues to try to connect to all the networks
p2p_find was really slow...
so I went into /usr/share/dbus-1/system-services and moved away the two files
fi.epitest.hostap.WPASupplicant.service
fi.w1.wpa_supplicant1.service
Now I can
sudo killall wpa_supplicant
and it really dies, disconnecting me from Wifi without reconnecting the following second.
Now I can launch wpa_supplicant and the three problems are resolved.
I go into wpa_cli, launch "p2p_find" and at the same time, I enable the search from within the app.
Android and Linux can now see each other.
P2P-DEVICE-FOUND 00:11:22:44:88:ff p2p_dev_addr=00:11:22:44:88:ff
pri_dev_type=10-0050F204-5 name='GT-I9300' config_methods=0x188
dev_capab=0x24 group_capab=0x0 vendor_elems=1 new=0
With "p2p_peers" I can see the cellphone MAC.
> aa:bb:cc:dd:ee:ff
Here I can't go ahead. I tried various possibilities, but none of them worked. My objective is to have Linux as Group Owner.
Question 1.1: Which is the correct way to handshake the two devices?
Question 1.2: When I try p2p_connect I often get "Michael MIC failure detected" between the results, what does it means in this contest?
Question 1.3: Android currently tries to connect with PBC. There is a way to connect with PIN?
Seeing that I couldn't connect Android/Linux, I tried to do some practice with Linux/Linux (Linux Mint as GO, Ubuntu as Client), following examples from internet like the ones in the first link.
I have a number of problems even here.
In Mint I try
p2p_connect MAC_UBUNTU pin auth
in Ubuntu I try
p2p_connect MAC_MINT PIN_GENERATED_BY_MINT
When I write the psp_connect in Mint, it creates a new interface p2p_wlan0 _0, and it returns
P2P-DEVICE-LOST p2p_dev_addr=MAC_UBUNTU
then the p2p_connect launched from Ubuntu fails.
Question 2.1 Why it switches interface when creating the group?
Question 2.2 What is the correct way to handle this change? After the change I can't p2p_find Ubuntu anymore (I have to wait some time or restart everything)
Question 2.3 Should Ubuntu change it's interface?
Question 2.3 In the new interface I should set the IP for both Ubuntu and Mint... how should I do this with static IPs?
Question 2.4 If I want to set up for example a DHCP server and client?
Question 1.4 And with Android(client) / Linux(GO)?

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

Did you simply tried to activated your hotspot option on Android? Then connect with the Ubuntu device, check the connected IP on the Android hotspot options and give it to your app.
It work for me for remote controling my Ubuntu system.

Related

Access ionic dev server from phone

I want to create a mobile browser app.
I am using ionic within wsl within windows 11.
Now when I do ionic serve --external I want to access the web app from my phone browser.
I am on a public train wifi, so maybe that's why I can't connect?
This is the output from ionic (extract):
[INFO] Development server running!
Local: http://localhost:8100
External: http://172.21.17.144:8100
My phone is on the train network as well. I can access the external ip from my laptop (host), but not from the phone. The phone browser gives timeout message, when I try to connect to the external ip.
I also tried to connect via hotspot. I created a hotspot in windows 11. Connected to it from the android phone. Unfortunately I couldn't connect to my laptop via the hotspot network either (ip: 192.168.37.1). Also I didn't see network adapter that was hosting the hotspot in wsl via ip a.
Please, how would you connect to your dev server, that is running on your laptop, from your phone, without too much headache?
Please, how would you connect to your dev server, that is running on your laptop, from your phone, without too much headache?
There are several options. The first is definitely the easiest, if it works for you.
WSL1
In general, networking is "easier" on WSL1, so if the tools you are using work there, it's okay to keep using a WSL1 instance for them. You can have a separate WSL2 instance if you'd like for tools that work better there.
WSL1 runs as a syscall translation layer in Windows, and as such it shares the same network interface as Windows.
WSL2, on the other hand, is a virtualized environment, using a virtual NIC behind a virtual Hyper-V switch. The WSL2 network is NAT'd behind/inside the Windows network, which is why you can't see it from other devices on the network.
WSL2 does provide a feature known as "local forwarding" that allows the Windows host itself to access WSL2 through the localhost address. However, that doesn't extend to other devices on the network.
So if you do need to use WSL2, there are other solutions:
SSH Reverse Tunnel
My personal preference at the moment, since it simplifies forwarding and firewall rules. See my "Option 2" in this Ask Ubuntu answer for instructions.
There's some one-time setup that might take 10-15 minutes, but after that it's a one-liner to connect the ports before you run start the server.
Bridged network interface
You mention that you are on Windows 11. As long as this is a Pro version or higher (not Home), you could use a new feature in the WSL Preview release to create a bridged Hyper-V switch. I have not tested it myself, but you can find details in this blog post.
Traditional port-forwarding method
The method recommended in the Microsoft Docs is currently to use Windows port forwarding through the netsh interface portproxy command to route the traffic to WSL2. The problem is that the WSL2 address is dynamically assigned each time it restarts), so you have to determine or parse the correct address after each reboot.

Debug Android application remotely over same vpn

I've developed an Android application which worked fine on my side but failed (not crash) on client side.
I do the research but still can't figure how to do.
From this post Debugging android device over the Internet
I know it can achieved by using abd, but it required both pc connect on the same network, which are not suitable for my case where my client are connect on difference network.
This post suggest another solution which is port forward, but warmed not recommended as this was pretty dangerous.
The following post suggest method of adb -a -P 5037 nodaemon server but I'm not really understand does it work for my situation as i getting error of could not install *smartsocket* listener: cannot bind to 0.0.0.0:5037: Only one usage of each socket address (protocol/network address/port) is normally permitted. (10048)
My question is how do remotely debug (get the logcat) of client application under difference network but was able to connect on the same VPN. Thanks.
Even i had faced the similar problem and this blog talks on same thing and there is some solution already out there which can be used for working on remote android devices, it looks paid one though.

How can I get my android emulator use my computer's VPN interface?

My company requires VPN to connect to our dev systems and for the first time I'm trying to work on an android app from home. Turns out the emulator doesn't want to use the VPN interface so even though I have connectivity to our dev systems on my VPN-connected laptop the emulator that's running on it does not.
I have thought of 3 solutions to this:
run a VPN client on the emulator, but I was hoping for a simpler solution than that
setting up a proxy server on my local machine
forwarding a port on my local machine
2 and 3 can probably work, but I'd still like to know if there is an easy way to get the emulator to use the VPN interface without a workaround.
I'm running OS X 10.7 and I've already tried adjusting the interface priority order with network>>set service order.
I suppose I can dust off the old dell and try it there, but I would expect the same results.
First start your vpn connection and then restart the emulator, now the emulator should use the host vpn connection.
it's mostly because of the DNS issue, according to the android doc:
At startup, the emulator reads the list of DNS servers that your system is currently using. It then stores the IP addresses of up to four servers on this list and sets up aliases to them on the emulated addresses 10.0.2.3, 10.0.2.4, 10.0.2.5 and 10.0.2.6 as needed.
I encountered this issue and tried something on dns settings but I could not solve it. As #machado said in the comment section below you should boot the emulator via "Cold Boot Now" option. I hope this helps.
What I do on my mac is just make sure 'send all traffic over vpn' is checked.
You'll find this setting after you click advanced while having your vpn connection selected in network preferences.
Works well for my situation, but may not depending on the type of VPN you're connecting to.
I had the same issue and solved it by connecting to cisco vpn after the android emulator has started and connected to the internet.
Works for me in windows 7
I setup a vpn connection on Windows. Then I started the emulator. The emulator and everything on my pc started to use that vpn connection.

Communication between Android Emulators

I wanted to know if it is possible the comunication between two Android Emulators started on two different machines networked.
For example
- Emulator A run on machine M1
- Emulator B run on machine M2
Can A and B exchange messages?
If yes, what ip address do I use?
Thank you very much
Deborah
You might know this already. As of android-sdk-tools_r7 it should at least be possible to connect two emulators to each other. Whether is works between physical machines is not something I know. It might be possible to set up masquerading between the two hosts for this specific broadcast.
From the android commit
Enable interconnection of emulators
This patch adds a -shared-net-id option to the emulator which
joins the emulator in a shared network.
If the option is given the emulator is started with an additional
network interface bound to a multicast socket. This multicast socket
emulates a network hub, interconnecting emulators.
If the -shared-net-id option is not given, nothing changes.
To connect two emulators, use the -shared-net-id <number> command line option when starting the emulator. They will then directly share everything sent to or read from ip 10.1.2.<number> on the emulator.
Another approach would be to enable port forwarding between the emulators using adb, and then set up masquerading for those ports on the networked machines.
I'm not really sure that either of these solutions match the use case that you're asking after. If not, adding more details to the original question might help. :)
Disclaimer: I have not tried the first approach, and the second I've only used to make sure that an emulator could bypass a nasty corporate firewall and HTTP proxy so that we could test the android browser. The principle should be sound though.
Update:
Nope, I played around with it a whole lot and found no nice solutions. I am not good enough at iptables to set up a valid tunnel that way that works for the server end.
Regards,
Mikael
An emulator normally has outbound access to the development machine's LAN or internet connection, so the problem is on the inbound side.
You can use an emulator or ADB port forward to send traffic into the emulator, but possibly only locally originating traffic on the development machine's loopback interface, and not external traffic. If that is indeed the case, the simplest thing would probably be to write or configure a port forwarder that listens on the development machine's external interface and forwards to the port that is forwarded into the emulator.

Connecting to an Android emulator on another machine

I need to test my android app 24h a day and I have 3 ubuntu boxes to do it.
I would like to connect my development system (MOTODEV Studio 1.3) to a Remote Device.
The interface asks me for an IP address and port for the machine to connect to.
But it doesn't connect. I found that the adb sever on the remote machine doesn't seem to listen to external ports, only internal loopback, making machine-to-machine connections impossible.
Is this true? If so, why there is a section on Device to connect to Android Remote Device on MOTODEV Studio?
I've found an explanation and a workaround in this post (http://rxwen.blogspot.com/2009/11/adb-for-remote-connections.html) but it seems complicated and the post's links don't work, so I'm confused whether there is a solution to do that.
It seems so simple..... I can't believe there is not a way.
The Remote device feature of MOTODEV Studio is intended for TCP/IP connections to a remote phone. It requires a lot of manual intervention at this point to get things working and it's an area where we're definitely in need of better documentation.
I don't have the full workflow in front of me, but the steps you need go like this...
Connect phone to remote host.
Using adb shell on remote host, set the phone to connect over TCP/IP rather than USB. You'll need to find the IP address of the phone manually. If you're using a Motorola phone and the Motorola drivers, it's probably 192.168.16.x
Set up port forwarding from the remote host to the phone for the debug ports (probably 5554 or 5555).
From the local host, set up the address of the remote host in the IP field.
When choosing "Run as" or "Debug As" from the local host, you need to choose the remote device as your target.
We intend to make this workflow more automated in the future, but for now you have to do this manually. It's my observation that remote debugging of embedded systems is still slow and prone to problems with latency and line quality, regardless of whether we're talking about phones or reference boards.
Good luck.

Categories

Resources