Skip to main content

Server Locations

Fetching Locations​

Fetch the list of available VPN servers after authentication:

val locations = vpnManager.createLocationService()
locations.fetchLocations() // suspend

Only locations serving the user's selected VPN protocol come back — a location that serves only Lightway is not offered while OpenVPN is selected. The selection is read once per call and nothing observes it, so call fetchLocations() again after setSelectedProtocol(...). A consumer without KapeVPN supplies the protocol directly; see VPN Locations.

Locations are exposed as StateFlows either as a flat list or as a grouping by continent:

// All locations
locations.allLocations // StateFlow<List<KapeServerLocation>>

// Grouped by continent
locations.locationsByContinent // StateFlow<Map<Continent, List<KapeServerLocation>>>
// e.g. {Continent.Europe: [...], Continent.Americas: [...]}

viewModelScope.launch {
locations.allLocations.collect { servers -> /* update UI */ }
}

Each KapeServerLocation provides:

PropertyTypeDescription
idStringLocation ID (pass to connect(...))
nameStringHuman-readable name (e.g. "Frankfurt")
countryStringISO 3166-1 alpha-2 country code
continentStringContinent or region name
latitudeDoubleLatitude coordinate
longitudeDoubleLongitude coordinate
ratingKapeLocationRating?Quality rating (smart locations only)
pingMsUInt?Latest measured ping, in milliseconds (smart locations only)

KapeLocationRating values: Excellent, Good, Ok, Degraded, Poor, Unknown. The enum is Comparable which means that higher ratings sort greater.

Smart Locations​

Smart locations are the SDK's recommended servers for the current user. The rating combines several signals: ping latency (min, avg, median, p90), short HTTP download and upload measurements, and geographic distance. Results are cached per network. When the user reconnects to a known WiFi network, cached results are returned immediately without re-running the full measurement.

Cached results are also tied to the active subscription's plan. After an upgrade, a downgrade or a Dedicated IP claim, hasSmartLocations() reports false and the next fetchSmartLocations() measures afresh rather than ranking the previous plan's locations.

Fetch smart locations after fetchLocations():

locations.fetchSmartLocations()
locations.smartLocations // StateFlow<List<KapeServerLocation>> — sorted by best rating, each with pingMs
locations.smartLocationPings // StateFlow<Map<String, UInt>> — ping of every measured location, by location id

The measurement covers many more locations than it ranks. smartLocationPings holds the ping of every one of them, so a location list can show a ping on rows that did not make the ranking.

To read the stored ranking without measuring — for example on launch, before a scan has run:

when (val result = locations.fetchCachedSmartLocations()) {
is FetchCachedSmartLocationsResult.Success -> result.locations // empty when nothing is stored
is FetchCachedSmartLocationsResult.SdkError -> { /* result.error */ }
}

It returns the ranking stored for the current plan and network, with pingMs, and never starts a measurement. It updates nothing.

If the measurement fails and nothing is cached for the current plan and network, fetchSmartLocations() returns FetchSmartLocationsResult.SdkError rather than an empty list, and smartLocations keeps its previous value. Retry the call, or fall back to the full location list.

To disable HTTP tests (recommended on cellular to save data and reduce measurement time):

locations.fetchSmartLocations(extendedTest = false)

extendedTest applies to this path only — a progress-tracked scan (below) measures latency and never runs the HTTP tests.

When you need a good location quickly rather than a list to choose from — "connect me now" — run the measurement in fast mode:

locations.fetchSmartLocations(forceRefresh = true, fastMode = true)

Fast mode stops widening the search once one location has answered well, instead of five, and runs no HTTP tests, so the ranking can hold fewer locations. extendedTest is ignored, and so is fastMode itself on a progress-tracked scan. The result is stored like any other: until the next measurement, cached reads (fetchCachedSmartLocations(), forceRefresh = false, an Auto connect) return it.

To receive per-location progress updates while the measurement runs:

locations.fetchSmartLocations { progress ->
println("${progress.locationId}: ${progress.pingMs ?: 0} ms")
}
info

Call fetchLocations() before fetchSmartLocations(). Smart locations are resolved by matching against allLocations, which is scoped to the selected protocol — so a highly-rated location that does not serve that protocol is not recommended. With a narrow protocol selection smartLocations can come back empty even though the rating ran.

A speed test shares the SDK's single ping/Smart Location slot. Starting one cancels a ping or Smart Location operation in progress.

Network Monitoring​

KapeNetworkHelper (part of KapeCore) keeps the SDK aware of the current network and exposes the WiFi identity for display in your UI. It also enables per-network SmartLocation caching. When the user reconnects to a known network, cached results are returned immediately.

Setup​

Create one instance and call start() once after the SDK handle is ready. Typically in your Application class or a top-level view model:

import com.kape.platformsdk.core.KapeNetworkHelper

val networkHelper = KapeNetworkHelper(handle)
networkHelper.start()

KapeNetworkHelper uses ConnectivityManager to detect network changes. On each change it reads the current SSID and BSSID (via WifiManager), pushes them into the SDK configuration, and signals a network change so the SDK can invalidate or restore cached SmartLocation results for that network.

Call stop() when the helper is no longer needed:

networkHelper.stop()

Observing network info​

KapeNetworkHelper exposes two StateFlows:

PropertyTypeDescription
ssidStateFlow<String?>WiFi network name. null on cellular or when the location permission is missing.
bssidStateFlow<String?>WiFi access point MAC address. null on cellular or when the location permission is missing.
viewModelScope.launch {
networkHelper.ssid.collect { ssid -> /* show ssid ?: "–" */ }
}

Runtime permission​

On Android, reading the SSID and BSSID requires the ACCESS_FINE_LOCATION runtime permission (and NEARBY_WIFI_DEVICES on API 33+). These are declared and requested by your app, not by the SDK — the SDK does not bundle a location permission in its library manifest, to avoid forcing a sensitive permission on every integrator.

<!-- In your app's AndroidManifest.xml -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES" />

Request the permission at runtime before calling start(). Without it, ssid and bssid are always null. The helper still functions — network-change signals are sent to the SDK — but per-network caching has no benefit since all networks appear identical.

warning

Do not instantiate KapeNetworkHelper inside KapeVpnService. The service does not need SmartLocation caching and runs under a tighter resource budget.