Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a VPN connection is active in Android?

Tags:

I have a third party VPN app on my non-rooted Android 4.4 device, and want to write a background service to monitor the VPN connection and alert the user if the VPN connection has been broken.

Is there a way to do this? I couldn't find any way using the VPNService API.

Thanks -D

like image 343
Dan Largo Avatar asked Feb 07 '15 19:02

Dan Largo


People also ask

How do I know if traffic is going through VPN?

You can use a tool like Wireshark to "sniff" the traffic on your local network. Wireshark will allow you to see which traffic is going where based on the source and destination IP addresses. Set up Wireshark on an interface that is between the hosts you want to test.


1 Answers

Using NetworkCapabilities worked for me. You have to loop over all existing networks and check which has VPN_TRANSPORT

ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = cm.getAllNetworks();  Log.i(TAG, "Network count: " + networks.length); for(int i = 0; i < networks.length; i++) {    NetworkCapabilities caps = cm.getNetworkCapabilities(networks[i]);    Log.i(TAG, "Network " + i + ": " + networks[i].toString());   Log.i(TAG, "VPN transport is: " + caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN));   Log.i(TAG, "NOT_VPN capability is: " + caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN));  } 
like image 51
Isaac Madwed Avatar answered Oct 28 '22 09:10

Isaac Madwed