Durai Amuthan.H Durai Amuthan.H - 4 months ago 22
Android Question

How to find the status of VPN connection through framework APIs or any other efficient method?

So far:

I found the following solutions

1)Using Broadcastreceiver

The broadcast receiver is deprecated from ICS

2)Using Ping or Traceroute

Its definitely going to take lots of time and its not effecient

3)Polling for DNS server changes

Its definitely going to take lots of time and its not effecient

4)Using ip-address

Even though it won't take much time depending on the network connection it may vary

My conclusion:

So far all the solutions I found are either not efficient or not so reliable and

My questions:

if VPN is connected in android device then android OS must be aware of it.

Are there any public android framework apis to read it because finding it locally is most efficient and reliable solution ?

Are there any other efficient and reliable ways to achieve it(like integrating a C or C plus plus library with NDK)?

Note:

I couldn't find any custom broadcast senders/AIDL from OpenVPN for Android as well

Answer

You can try to check for tun0 INTERFACE, It is being start afther the establish command.

try {
    for( NetworkInterface intf : Collections.list(NetworkInterface.getNetworkInterfaces())) {

        // Pass over dormant interfaces
        if(!intf.isUp() || intf.getInterfaceAddresses().size() == 0)
            continue;

            if ("tun0".equals(intf.getName())){
                // The VPN is up
                break;
            }
    }
}

this also might work:

(Collections.list(NetworkInterface.getByName("tun0")

Comments