Skip to main content

Usage

connect(), status(), and disconnect() are suspend functions, so call them from a coroutine (lifecycleScope, viewModelScope, and so on). All three throw a typed WiDASException on failure; status() is advisory and degrades rather than throwing, but a broken local state store still surfaces as Internal. There's also a fifth suspend function, debugInfo(), which sits outside the flow and exists purely for support.

Implement Broker​

Skip this section if you're issuing through Play Integrity.

Broker is how the SDK reaches your Broker. It has one method:

public interface Broker {
public suspend fun sign(submission: SigningRequestSubmission): IssuanceResponse
}

sign gets a SigningRequestSubmission carrying clientId, partnerUserId, csr, requestedValidity and deviceId. Send them to your Broker, which makes the certificate request and returns the Wi-DAS response. Turn that response into an IssuanceResponse.

requestedValidity is null when your app calls connect() without one. Forward it as it is: with no value, Wi-DAS issues the certificate for your maximum. Fill in your own only if you want a shorter lifetime.

The submission carries no Idempotency-Key. Your Broker makes that, because only your Broker knows which of its calls are retries.

When something fails, throw one of these two exceptions:

  • Your Broker answered with a refusal: throw BrokerRefusedException with the problem title from the response, exactly as it arrived. The SDK maps it to a WiDASException, and matching is exact, so a trimmed or re-cased title lands on the generic IssuanceRejected.
  • No usable answer arrived: throw BrokerUnreachableException, passing the original exception if there is one. The SDK reports it as Network, which your app can retry. That includes a timeout (408), rate limit (429) or server error (5xx) that carries no problem title, such as an error page from a gateway in front of your Broker. A bare IOException you let escape is reported as Network too.

Anything else you throw is reported as Internal, keeping your exception as its cause. Coroutine cancellation passes through untouched.

Here's an implementation over OkHttp, talking to a Broker endpoint you define (here POST /wi-das/certificates) that relays to Wi-DAS and passes its response back:

import com.widas.sdk.Broker
import com.widas.sdk.BrokerRefusedException
import com.widas.sdk.BrokerUnreachableException
import com.widas.sdk.IssuanceResponse
import com.widas.sdk.PasspointSettings
import com.widas.sdk.SigningRequestSubmission
import java.time.Instant
import java.time.format.DateTimeParseException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONException
import org.json.JSONObject

class MyBroker(
private val http: OkHttpClient,
// Your Broker's endpoint, for example https://api.example.com/wi-das/certificates
private val endpoint: String,
// Your own sign-in. The Broker should work out who the user is from this,
// not trust partnerUserId as sent.
private val userAccessToken: suspend () -> String,
) : Broker {

override suspend fun sign(submission: SigningRequestSubmission): IssuanceResponse {
val body = JSONObject()
.put("clientId", submission.clientId)
.put("partnerUserId", submission.partnerUserId)
.put("csr", submission.csr)
.put("requestedValidity", submission.requestedValidity) // left out when null
.put("deviceId", submission.deviceId)
.toString()
.toRequestBody("application/json".toMediaType())

val request = Request.Builder()
.url(endpoint)
.header("Authorization", "Bearer ${userAccessToken()}")
.post(body)
.build()

// An IOException escaping here is reported as Network.
val (status, text) = withContext(Dispatchers.IO) {
http.newCall(request).execute().use { it.code to it.body?.string().orEmpty() }
}

if (status !in 200..299) {
val title = problemTitle(text)
// A timeout, rate limit or server fault with no problem title (a gateway error page, say)
// is temporary. Reporting it as a refusal would tell the app not to retry.
if (title == null && (status >= 500 || status == 408 || status == 429)) {
throw BrokerUnreachableException()
}
throw BrokerRefusedException(title)
}

return try {
toIssuanceResponse(JSONObject(text))
} catch (e: JSONException) {
throw BrokerRefusedException(null) // an answer we couldn't read
} catch (e: DateTimeParseException) {
throw BrokerRefusedException(null)
}
}

private fun problemTitle(text: String): String? =
try {
JSONObject(text).stringOrNull("title")
} catch (e: JSONException) {
null
}

// The Wi-DAS certificate response, as your Broker passed it back.
private fun toIssuanceResponse(issued: JSONObject): IssuanceResponse {
val passpoint = issued.getJSONObject("passpoint")
return IssuanceResponse(
serial = issued.getString("serial"),
clientCertificatePem = issued.getString("certificatePem"),
issuingCaPem = issued.getString("chainPem"),
serverCaPem = issued.getString("serverCaPem"),
passpoint = PasspointSettings(
friendlyName = passpoint.getString("friendlyName"),
caCertName = passpoint.getString("caCertName"),
certName = passpoint.getString("certName"),
realm = passpoint.getString("realm"),
homeFqdn = passpoint.getString("homeFqdn"),
mcc = passpoint.stringOrNull("mcc"),
mnc = passpoint.stringOrNull("mnc"),
rcoi = passpoint.stringOrNull("rcoi"),
),
notAfter = Instant.parse(issued.getString("notAfter")),
deviceId = issued.stringOrNull("deviceId"),
)
}

private fun JSONObject.stringOrNull(name: String): String? =
if (isNull(name)) null else getString(name)
}
Cancellation

If the caller's coroutine is cancelled, the SDK cancels sign too. OkHttp's blocking execute() carries on to the end regardless. If you'd rather abandon the request, cancel the Call when the coroutine is cancelled.

Configure​

Build one WiDAS instance with the application context and reuse it.

import com.widas.sdk.WiDAS

val widas = WiDAS.configure(
context = applicationContext,
clientId = "client-acme",
broker = MyBroker(http, brokerEndpoint, signIn::accessToken),
baseUrl = "https://partner-api.wi-das.com/",
)
ParameterRequiredNotes
context✅Any context; the SDK keeps the application context.
clientId✅Your Wi-DAS-issued public client identifier.
broker✅Your Broker adapter.
baseUrl✅The Wi-DAS Certificate API base URL, with a trailing slash. Must be https. The SDK still calls Wi-DAS directly for status().

The instance stays on the brokered route for its lifetime. It never calls Play Integrity, and it doesn't fall back to Play Integrity if your Broker can't be reached.

Configure for Play Integrity instead​

Pass cloudProjectNumber in place of broker and the SDK vouches for each request with Play Integrity, talking to Wi-DAS directly. We'll remove this route in the near future, so use it only for an app that already depends on it. Your app needs Play Integrity set up and a registered identity; see Setup.

val widas = WiDAS.configure(
context = applicationContext,
clientId = "client-acme",
baseUrl = "https://partner-api.wi-das.com/",
cloudProjectNumber = 123456789L,
)

cloudProjectNumber is your Google Cloud project number. It's required for builds that aren't distributed through Google Play, and harmless for those that are.

Everything below works the same on both routes.

Connect​

connect() onboards the user. Call it when status() reports NONE, EXPIRED or REVOKED. requestedValidity is an optional ISO-8601 duration; leave it out (or pass null) to let Wi-DAS clamp to policy. On the brokered route, your Broker forwards it the same way.

lifecycleScope.launch {
try {
val result = widas.connect(user = "user-42", requestedValidity = "P365D")
Log.i("WiDAS", "Onboarded. Serial ${result.serial}, expires ${result.notAfter}")
} catch (e: WiDASException) {
handle(e) // see Error handling
}
}

ConnectionResult carries the certificate serial and its notAfter expiry.

Check status before you connect

connect() works out whether this is a first enrolment, a renewal or a recovery, so you don't track it. Calling it again is safe, but each call issues a new certificate through your Broker (or Play Integrity), so don't call it on every launch. Certificates last about a year: at launch, check status() first and connect only when there's no usable certificate.

status() reads what the SDK stored, so if the user deletes the Wi-Fi profile by hand it still reports VALID. Keep a Connect or Reinstall action in your UI even while connected, so the user can put the profile back; otherwise it comes back at the next renewal.

Status​

status() is advisory: any lookup failure degrades to the locally-stored validity window rather than surfacing, so it's safe to call whenever you're rendering UI. The SDK asks Wi-DAS directly on both routes; your Broker isn't involved. The one failure it does surface is a broken local state store, as Internal.

lifecycleScope.launch {
when (widas.status().state) {
WiDAS.Status.State.NONE -> promptToConnect()
WiDAS.Status.State.VALID -> showConnected()
WiDAS.Status.State.EXPIRED,
WiDAS.Status.State.REVOKED -> widas.connect(user = "user-42") // re-onboard
WiDAS.Status.State.UNKNOWN -> showUnconfirmed() // couldn't confirm; try again later
}
}

Status gives you state (NONE/VALID/EXPIRED/REVOKED/UNKNOWN), the serial, and the expiry. UNKNOWN means a certificate was issued but neither the server nor local state can say whether it's still in date — reported rather than optimistically shown as VALID.

Remember the network is the real authority; status() is for the UI.

Disconnect​

Remove the installed profile when the user asks, or after a REVOKED result.

lifecycleScope.launch {
widas.disconnect()
}

A later connect() just re-runs the usual onboarding.

Diagnostics​

debugInfo() sits outside the flow. It returns the configuration Wi-DAS resolved for your Client alongside what the device holds, as a payload you can send to support when the certificate installs and the device still won't join.

lifecycleScope.launch {
val info = widas.debugInfo() // null unless your Client is in DevMode
Log.d("WiDAS", info?.toJson() ?: "no debug info available")
}

It only answers while your Client has DevMode enabled, which a Wi-DAS operator sets on request, so null is the normal answer in production. A payload or an empty answer are the only two outcomes; it reports no errors of its own. The payload carries no secrets, so you can send it as it stands. Full field reference in Diagnostics.

A complete launch flow​

fun onLaunch(userId: String) {
lifecycleScope.launch {
// Each connect() issues a new certificate, so only connect when there's no usable one.
// If a different user can sign in on this device, see the example app for handling that.
val status = widas.status()
val needsConnect = when (status.state) {
WiDAS.Status.State.NONE,
WiDAS.Status.State.EXPIRED,
WiDAS.Status.State.REVOKED -> true
WiDAS.Status.State.VALID,
WiDAS.Status.State.UNKNOWN -> false // UNKNOWN: show as unconfirmed, check again later
}
if (!needsConnect) {
render(status)
return@launch
}

try {
// First run, renewal or recovery — one call.
widas.connect(user = userId, requestedValidity = "P365D")
} catch (e: WiDASException.Network) {
scheduleRetry() // your Broker or Wi-DAS couldn't be reached
} catch (e: WiDASException.ServiceUnavailable) {
scheduleRetry() // a temporary fault on our side
} catch (e: WiDASException) {
handle(e)
}

render(widas.status()) // reflect real state in the UI
}
}
Play Integrity needs Play Services

On the Play Integrity route, a device without Google Play Services fails connect() with AttestationUnavailable. There's no fallback, so detect this and degrade the UI rather than looping on it. The brokered route doesn't need Play Services.

For the whole thing in a Jetpack Compose app, including the sign-in token, status in the UI and sign-out, see the example app.

Next: Error handling.