1

I have a virtual router on my laptop that my raspberry pi is connected to. It runs a couple of java processes that I need to keep track off, so if it disconnects I want to make it stop working, so:

while(true){
    if(WifiChecker())
        return;
    //do rest
}

Is there a way to accomplish this? If possible I would also like something easy that makes it reconnect to my virtual wifi, though that is not necessary.

javabrett
  • 7,020
  • 4
  • 51
  • 73
HenkKaasman
  • 55
  • 1
  • 11
  • It might look like `if(WifiChecker.isConnected())` but besides it should be possible. For more help we'd most probably need more information. – Thomas May 27 '15 at 12:53
  • The easiest way is just try to ping/connect to google.com if that fails you probably don't have an internet connection (or google is down of course). – User404 May 27 '15 at 13:15

1 Answers1

1

Just try pinging the IP Address of that virtual router or alternatively some address on the internet (Google or similar big sites are great for this since they're probably not going to be down).

You can use Java sockets for that, but you will need root access as pointed out in the most upvoted answer of this thread about pinging with java.

Alternatively you can use a ProcessBuilder to start the ping utility itself and examine its output and/or error code.

Community
  • 1
  • 1
mhlz
  • 3,497
  • 2
  • 23
  • 35