0

I want to get the wlan0 inet address through code in Kotlin for my android app. I have tried using WifiManager.connectionInfo.ipAddress, however that returned on both wifi and hotspot. However, if I run ifconfig in termux, an IP exists. I also tried using InetAddress class but that doesn't seem to have anything that works for my purpose.

I tried the solutions in this post: How to invoke external command from within Kotlin code?. Again, these returned nothing when passing in ifconfig.

Kanishka
  • 11
  • 1

1 Answers1

0

You need to go though the available interfaces and look for wlan0

    val interfaces = NetworkInterface.getNetworkInterfaces()
    val list = interfaces.toList()
    //go through the available interfaces
    for (interface in list) {
        if (interface.name.equals("wlan0")) {
            //look into the interface's ipaddresses (ipv4,ipv6)
            interface.inetAddresses.toList().get(0).hostAddress
        }
    }
dkoukoul
  • 61
  • 2
  • 4