6
votes

Hi! Asked this question on many forums and nobody could answer me so PLEASE help!

I have Android device connected to Linux device via USB tethering.

On Android device I have front-end(html, js, css) browser app and on Linux device I have back-end(node.js).

There is a socket connection between front-end and back-end with defined IP:PORT.

Androids gateway is Linux device. Linux device is connected to local network.

When Android device is connected to Linux device, Linux device initializes new network interface called usb0 with static IP address.

Sockets are based on that network IP that is same on every instance of front-end+back-end devices.

I have more than 5 instances of same front-end+back-end devices connected to local network.

Is there a way to somehow find the back-end device IP address without setting it static?

My problem starts when starting all devices at the same time. Sometimes Android device in instance 1 connects to Linux device from other instance and I want to prevent that by not connection to usb0 with Android but to eth0 DHCP of Linux device. I can never know which IP address will be on Linux device but front-end app needs a IP:PORT to connect.

PORT is defined so the string for connecting is {IP + ":3000"}

I know I can see all the IP addresses on network with arp or similar but I need to do that automatic in front-end app on app start up.

I want to find first device IP address that android device is connected to. That is always a front-ends pair Linux device.

Or if it is not possible to prevent Android device to be able to connect another instance with IP tables on Linux device.


P.S I know that Android is also Linux but someone who wants to answer will know what I'm talking about.

1
Using which programming style? Android Java? Or do you want to do this from C/C++ code? - selbie
I think what you are asking is: How can the Android device programatically infer the IP address of the Linux device it's tethered to? Is that correct? If so, do you want to do this in Java or C ? - selbie
I'm using Java to establish tether on app start (modifed cordova plugin). App is hibrid and saves socket address in javascript. So I need somehow to tell the App to connect to specific IP - Avoid

1 Answers

0
votes

Here is a solution describing how to listen for tethering state changes:

First you need to be familiar with BroadcastReceiver. You can find a lot of tutorials describing in great detail how this works (try googling how to listen for connectivity changes in Android).

In order to get the Tethering state update, you need to use a hidden filter action of Android (see ConnectivityManager) and in your BroadcastReceiver class:

IntentFilter filter = new IntentFilter("android.net.conn.TETHER_STATE_CHANGED");

then register the filter to your BroadcastReceiver:

myApplicationContext.registerReceiver(this, filter);

In your onReceive(final Context context, final Intent intent) method, the Intent.extras information contains 3 arrays filled with the corresponding tethered network interface:

erroredArray / availableArray / activeArray

It's a little bit tricky but you can get the tethering status information.

In addition, you can do some reflexion on a hidden function of Android code:

Search for getTetherableIfaces() in the Connectivity Manager.

Here is a link: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/net/ConnectivityManager.java#L1604