Aamgian 2 Posted June 16, 2020 Hello, How do I know if there is a VPN connection on the device that is active, because my application will ignore if using VPN? I use this code but it doesn't seem to work. function TPlatformConnectivity.IsVPNConnection: Boolean; var LManager: JConnectivityManager; LAllNetworks: TJavaObjectArray<JNetwork>; LCapabilities: JNetworkCapabilities; I: Integer; begin Result := False; LManager := ConnectivityManager; LAllNetworks := LManager.getAllNetworks; for I := 0 to LAllNetworks.Length - 1 do begin LCapabilities := LManager.getNetworkCapabilities(LAllNetworks[I]); if (LCapabilities <> nil) and LCapabilities.hasCapability(TJNetworkCapabilities.JavaClass.TRANSPORT_VPN) then //if (LCapabilities <> nil) and LCapabilities.hasCapability(TJNetworkCapabilities.JavaClass.TRANSPORT_VPN) then begin Result := True; Break; end; end; end; Thanks You Share this post Link to post
Dave Nottage 557 Posted June 16, 2020 That code just checks if the network being examined has VPN capability, not if it is actually available, nor if it is connected. Note that a device can have an active VPN connection as well as other active connections. For guidance, you may wish to look at the Java examples here: https://www.codota.com/code/java/methods/android.net.NetworkCapabilities/hasTransport and here: https://www.codota.com/code/java/methods/android.net.NetworkCapabilities/hasTransport (specifically example 19) Share this post Link to post
Aamgian 2 Posted June 16, 2020 (edited) Thanks Dave, at least the VPN connection on the device can be known this code work i tested with android 9 and 10. function TPlatformConnectivity.IsVPNConnection: Boolean; var LManager: JConnectivityManager; LAllNetworks: TJavaObjectArray<JNetwork>; LCapabilities: JNetworkCapabilities; I: Integer; begin Result := False; LManager := ConnectivityManager; LAllNetworks := LManager.getAllNetworks; for I := 0 to LAllNetworks.Length - 1 do begin LCapabilities := LManager.getNetworkCapabilities(LAllNetworks[I]); if (LCapabilities <> nil) and LCapabilities.hasTransport(TJNetworkCapabilities.JavaClass.TRANSPORT_VPN) then begin Result := True; Break; end; end; end; Edited June 16, 2020 by Aamgian 2 Share this post Link to post