ConnectivityManager.NetworkCallback calls missing when a VPN is connected/disconnected
Asked Answered
I

1

8

My Android app has registered ConnectivityManager onAvailable and onLost NetworkCallbacks, but they are not called when a VPN (I'm testing with OpenVPN app) is connected or disconnected. They do get called when cellular network is connected/disconnected.

Before I connect the VPN, allNetworks shows one network:

11-17 16:16:22.719 23531 23531 I Baresip : Found network 106 with [ Transports: CELLULAR Capabilities: SUPL&INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN&VALIDATED&NOT_ROAMING&FOREGROUND&NOT_CONGESTED&NOT_SUSPENDED Unwanted: LinkUpBandwidth>=51200Kbps LinkDnBandwidth>=102400Kbps Specifier: <6>]

After I have connected the VPN, there is two networks:

11-17 16:17:17.464 23531 23531 I Baresip : Found network 106 with [ Transports: CELLULAR Capabilities: SUPL&INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN&VALIDATED&NOT_ROAMING&FOREGROUND&NOT_CONGESTED&NOT_SUSPENDED Unwanted: LinkUpBandwidth>=51200Kbps LinkDnBandwidth>=102400Kbps Specifier: <6>]

11-17 16:17:17.466 23531 23531 I Baresip : Found network 225 with [ Transports: VPN Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&VALIDATED&NOT_ROAMING&FOREGROUND&NOT_CONGESTED&NOT_SUSPENDED Unwanted: ]

But when the VPN network becomes available, my app does not get onAvailable (nor any other) ConnectivityManager notification about it. Same problem, when I disconnect the VPN.

Is this an Android bug or is there some other means to get notified about VPN events?

Edit: I searched Google's issue tracker and found exactly the same issue:

https://issuetracker.google.com/issues/138035152

Unfortunately the author did not provide all the information Google asked and they closed the issue.

I created a new bug report, but most likely it will not lead anywhere:

https://issuetracker.google.com/issues/173534857

So help from community would be welcome.

Insert answered 17/11, 2020 at 14:37 Comment(0)
I
8

In order to get the callbacks called also when VPN connectivity changes, NetworkCapabilities.NET_CAPABILITY_NOT_VPN needs to be removed from the builder:

val builder = NetworkRequest.Builder()
                .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)

By default, that capability is included.

Insert answered 3/12, 2020 at 8:29 Comment(2)
So should this always be removed in case the user has a VPN active?Moreira
I remove it when the app starts before network callback is registered.Insert

© 2022 - 2024 — McMap. All rights reserved.