3

I have two applications that uses VpnService class. But there can be only one VPN connection running at the same time. The existing interface is deactivated when a new one is created and I want the new application not to start vpnservice to avoid old one getting deactivated and its VPN connection. So I want to check if another vpnservice was started before invoke startService().

Is there any api to do this ?

Rakesh patanga
  • 832
  • 9
  • 25
liyustar
  • 33
  • 2
  • 6
  • if answer is useful you could accept my post as answer – Rakesh patanga Aug 27 '14 at 12:19
  • this previous answer looks like what you need: http://stackoverflow.com/questions/24861034/how-to-find-the-status-of-vpn-connection-through-framework-apis-or-any-other-eff?lq=1 – Ilan.b Dec 06 '15 at 08:03

2 Answers2

4

As far as I know you can not check directly.The below is the approach that I use in my application for checking VPN connection.

1)Make a HTTPGet call for http://jsonip.com

                    public static String GetDeviceIp(){

                    HttpGet req = new HttpGet("http://jsonip.com");

                    DefaultHttpClient httpClient = new DefaultHttpClient();

                    HttpResponse response = httpClient.execute(req); 


                    if (response.getStatusLine().getStatusCode() == 200){
                        ipaddress=parseJson(response.getEntity().getContent());

                    }else
                    { ipaddress ="EMPTY" }

                    return ipaddress
          }

2)Parse the json response and extract the Ip address from response

      public static String parseJson(InputStream in)
      {
         String iP;
        try{

        String result;
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = bufferedReader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString(); 

        JSONObject jsonObject = new JSONObject(result);
        iP=jsonObject.getString("ip");
    }catch (Exception e){
        iP="EMPTY";
    }

    return iP;
   }

3)Compare if VPN server Ip and extracted Ip are equal if so then the VPN is on else VPN is off

public static boolean IsVPNON(){
    return GetDeviceIp().equals("VPN-IP address");}
Rakesh patanga
  • 832
  • 9
  • 25
1

While above approach works, it is more or less a hack and requires sending an HTTP request.

Since this seems like a one-off check you want to run before starting another VPN, you can check whether VPN network is part of all available networks.

private boolean isAnyVpnConnected(Context context) {
    ConnectivityManager connectivityManager =
            (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
    Network[] networks = connectivityManager.getAllNetworks();
    if (networks == null) {
        return false;
    }
    for (Network network : networks) {
        NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
        if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN) &&
                !capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)) {
            return true;
        }
    }
    return false;
}
AllThatICode
  • 1,250
  • 13
  • 19
  • I like this approach better than the http hack, but can I tell if the active VPN is _my_ VPN? Like somehow grabbing the active VPN package name? – spartygw Oct 04 '21 at 18:09