I am pretty sure it's possible as I successfully got same from termux Android app where I installed nmap and ran following commands:

  • Run ifconfig first and get the device IP (xxx.xxx.xxx.x)
  • Run nmap -sn xxx.xxx.xxx.x/24

Note: the device was not rooted and it even works in airplane mode.

Testing device: Android version 11, Samsung Galaxy m30s

I tried official Android docs for usb overview (usb manager, usbdevicce, etc.) and even running command line inside my app but wasn't successful (however I was able to do ssh once I know the ip address through command line).

2

There are 2 best solutions below

4
On

You can get the IP address of the device.

Process p = Runtime.getRuntime().exec("/system/bin/ip address");

This will give you lots of information including loopback,ip4,ip6, broadcast, etc

You can use additional switches to further modify it according to your requirement.

to get all the available switches

open terminal/command prompt Connect your Android Device

Type - ADB devices You should be able to see your device.

Then type ADB shell and press enter

then type /system/bin/ip --help

this will show all the switches for this IP utility.

1
On

You can sent a request to a httpbin server, and obtain the answer from the resonse.

e.g.

Request URL

https://httpbin.org/ip

Response body

{
    "origin": "104.132.33.92"
}