Home » Android » networking – Connect 2 or more Android clients directly

networking – Connect 2 or more Android clients directly

Posted by: admin June 15, 2020 Leave a comment

Questions:

I want to connect 2 or more Android devices with p2p connection. As I understand I may use Wi-Fi Direct under one wifi network https://developer.android.com/guide/topics/connectivity/wifip2p.html

But I want to connect Android clients located in different places but possibly with the one IP (vpn or something). How can I do that?

Here is my algorithm

Client_1 register its name and current IP on Server
Client_2 register its name and current IP on Server
Client_1 request registred IP from Server by Client_2 name
Client_2 request registred IP from Server by Client_1 name
Client_1 send message directly to Client_2 IP

This algorithm will not work if Client_1 and Client_2 has the same IP. Or if client is behind NAT.

Is there a ready to use library to connect 2 or more Android devices in such way?

How to&Answers:

You can use Fixed Ip Simcards if you want to do a setup for your personal purpose. Another option is to convert your android phone local private ip into public ip , this can be done via Update No-IP app , this app will give you a direct link to communicate with public ip you have.

I hope my answer would be somewhat helpful to you.

Answer:

Your best bet would be to look into NAT traversal on cellular connections. There are several threads on Stack Overflow that discuss this for example UPnP NAT Traversal for 3G/4G Wireless Data Connection on Android and Android: NAT Traversal?

To sum things up the easiest solution would be to look into using STUN to determine if your application/device is NATed and then using TURN or ICE to try to establish a (not necessarily direct) connection between your devices using NAT traversal. There exist multiple libraries that implement these protocols/methods for example http://www.pjsip.org/ which has Android support and has a pretty good documentation including sample applications for all mentioned techniques.