How can an Android device programmatically output the IP address of the Linux device to which it is attached?

Hello! Answering this question in many forums, no one could answer me, so PLEASE help!

I have an Android device connected to a Linux device via a USB modem.

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

There is a socket connection between the interface and the internal end with a specific IP: PORT.

Android Gateway is a Linux device. The Linux device is connected to the local network.

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

Sockets are based on a network IP address that is the same for each instance of the front-end and external servers.

I have more than 5 instances of the same interface and external devices connected to the local network.

Is there a way to find the IP address of an external device without its static?

My problem starts when starting all devices at the same time. Sometimes the Android device in instance 1 connects to the Linux device from another instance, and I want to prevent this by not connecting to usb0 from Android, but to eth0 of the DHCP device on Linux. I never know what the IP address will be on the Linux device, but an external application requires IP: PORT to connect.

PORT is defined so that the connection string is {IP + ": 3000"}

I know that I see all the IP addresses on the network with arp or similar, but I need to do this automatically in the front-end application when the application starts.

I want to find the first IP address of the device to which the Android device is connected. This is always the front-end Linux interface.

Or, if it is not possible to block an Android device to connect another instance with IP tables on a Linux device.


PS I know that Android is also Linux, but anyone who wants to answer will know what I'm talking about.

+5
source share
1 answer

Here is a solution describing listening for binding state changes:

First you need to be familiar with BroadcastReceiver. You can find many tutorials detailing how this works (try using Google to listen for connection changes in Android).

To get the Tethering state update, you need to use the Android hidden filter action (see ConnectivityManager) in your BroadcastReceiver class:

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

then register a filter for your BroadcastReceiver:

myApplicationContext.registerReceiver(this, filter);

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

erroredArray / availableArray / activeArray

This is a bit complicated, but you can get information about the status of the binding.

In addition, you can do some reflection in the hidden function of the Android code:

Locate getTetherableIfaces() in the connection manager.

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

0
source

Source: https://habr.com/ru/post/1264063/


All Articles