How to retrieve WiFi IP address programmatically?

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

How to retrieve WiFi IP address programmatically?

Postby mpscholz » Fri Jul 04, 2014 11:20 am

As described in the thread on the RMI access to the brick, I need to set an environment property in my RMI server program running on the brick, containing the IP address that the brick can be accessed by from a client.

In case the connection to the client machine is established by WiFi, though, the actual IP address is not known at programming time.
So I wonder whether there's the possibility to retrieve the WiFi IP address programmatically in the server program?

I have checked the API doc and tried
Code: Select all
LocalWifiDevice.getAccessPoint()


However, though everything is working fine with my WiFi connection to the brick (EV3 control, uploading and starting programs), I get an exception when calling that method in my server code as follows:

Code: Select all
       final EV3 localEV3 = LocalEV3.get();
      if (localEV3 == null) {
         System.out.println("No local EV3 found");
      } else {
         // retrieve WiFi device
         final LocalWifiDevice wifiDevice = localEV3.getWifiDevice();
         if (wifiDevice == null) {
            System.out.println("No WiFi device found");
         } else {

            // display access points presently available
            System.out.println("Available access points:");
            final String[] accessPointNames = wifiDevice
                  .getAccessPointNames();
            for (final String accessPointname : accessPointNames) {
               System.out.println(accessPointname);
            }
            System.out.println("(Press key to continue)");
            Button.waitForAnyPress();

            // display access point presently connected to
            System.out.println("Connected to access point:");
            System.out.println(wifiDevice.getAccessPoint());
         }
      }

      System.out.println("(Press key to continue)");
      Button.waitForAnyPress();


The com.sun.jna.LastErrorException (with errno 19) occurs when calling wifiDevice.getAccessPoint() and happens in NativeWifi.ioctl() called by LocalWifiDevice.getAccessPoint() on line 92 (sorry, do not know how to transport the brick's stack trace to the client machine).

The retrieval of the names of the available access points before works fine.

Why does this exception occur?
And is there a more suited way to retrieve the WiFi IP address?
leJOS Developer
mpscholz
New User
 
Posts: 20
Joined: Fri Jan 18, 2008 1:37 pm
Location: Germany

Re: How to retrieve WiFi IP address programmatically?

Postby gloomyandy » Fri Jul 04, 2014 12:23 pm

Hi, you do not need any leJOS/EV3 specific code to do this (the code you are trying to use is really there to allow you to configure your EV3), it is just pretty much standard Java/Linux/Networking code. basically you need to enumerate the network interfaces and obtain the IP address from them, note that the EV3 has more than one IP address (it has 3 I think), so you need to make sure you use the correct one. The menu system displays the IP address for the USB and WiFi interfaces so you could look at that code:
https://sourceforge.net/p/lejos/ev3/ci/ ... java#l2163

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4010
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: How to retrieve WiFi IP address programmatically?

Postby mpscholz » Fri Jul 04, 2014 2:16 pm

Hi Andy,

Thanks for the hint.
Indeed, by simply using the java.net API here I was able to retrieve the two IP adresses of interest in my server program (the default 10.0.1.1 one and the WiFi one); yet, I am not able to detect which one is the WiFi one (except for using rather crude and unreliable approaches like comparing display names of the associated NetWorkInterface or the likes).

Looks like the API here does not provide a "good" way to do this - the proposals in the web mostly boil down to try to access some "standard" web site over an internet socket created using the InetAddress at hand.
However, I am no expert in Network programming, after all. Maybe someone else here has a good idea how to porgrammatically identify the WiFi IP address in the list.

Otherwise I might let the user select the IP address to use on startup time of the server program (which sounds like the most reliable, but also most circumstantial way to do that).
leJOS Developer
mpscholz
New User
 
Posts: 20
Joined: Fri Jan 18, 2008 1:37 pm
Location: Germany

Re: How to retrieve WiFi IP address programmatically?

Postby gloomyandy » Fri Jul 04, 2014 2:33 pm

Why do you need to identify the WiFi one particularly? Isn't it possible that USB(or Bluetooth) might be being used to connect the EV3? On the EV3 I think that the network interface name for the USB/Bluetooth network will always be the same so you could potentially use that. I'm not sure that there really is a general way of detecting the "best" network, there is nothing to stop you having USB, Bluetooth and WiFi all active at once!
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4010
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: How to retrieve WiFi IP address programmatically?

Postby mpscholz » Fri Jul 04, 2014 3:04 pm

gloomyandy wrote:Why do you need to identify the WiFi one particularly? Isn't it possible that USB(or Bluetooth) might be being used to connect the EV3?


Quite so.
Yet, the idea was just to state a convention for this very app to use WiFi over USB/BT and thus relieve the user from having to make a selection at every start of the server program.

The whole thing popped up due to the requirement for the server program to set up the system property mentioned above - in an ideal world, the server wouldn't have to set that fixed at all.
leJOS Developer
mpscholz
New User
 
Posts: 20
Joined: Fri Jan 18, 2008 1:37 pm
Location: Germany


Return to EV3 Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff