Android (Kotlin)
A minimal Kotlin integration using OkHttp (included in most Android projects).
Add OkHttp to your build.gradle.kts:
implementation("com.squareup.okhttp3:okhttp:4.12.0")VoltaClient
Section titled “VoltaClient”import okhttp3.*import okhttp3.MediaType.Companion.toMediaTypeimport okhttp3.RequestBody.Companion.toRequestBodyimport org.json.JSONObject
class VoltaClient( private val apiUrl: String, private val apiKey: String, private val layoutId: String) { private val http = OkHttpClient() private val json = "application/json".toMediaType() private var token: String? = null
// Step 1: Discover controls fun getLayoutInfo(): JSONObject { val request = Request.Builder() .url("$apiUrl/layout/$layoutId") .header("x-api-key", apiKey) .build() val response = http.newCall(request).execute() return JSONObject(response.body!!.string()) }
// Step 2: Get a token fun connect() { val body = JSONObject().put("layoutId", layoutId) .toString().toRequestBody(json) val request = Request.Builder() .url("$apiUrl/token") .header("x-api-key", apiKey) .post(body) .build() val response = http.newCall(request).execute() token = JSONObject(response.body!!.string()).getString("token") }
// Step 3: Send actions fun sendAction(lid: Int, type: String, value: Any? = null) { val body = JSONObject().apply { put("layoutId", layoutId) put("lid", lid) put("type", type) if (value != null) put("value", value) }.toString().toRequestBody(json)
val request = Request.Builder() .url("$apiUrl/action") .header("Authorization", "Bearer ${token!!}") .post(body) .build() http.newCall(request).execute() }
// Convenience methods fun sendButton(lid: Int) = sendAction(lid, "Button") fun sendSlider(lid: Int, value: Float) = sendAction(lid, "Slider", value) fun sendToggle(lid: Int, value: Boolean) = sendAction(lid, "Toggle", value) fun sendIndex(lid: Int, value: Int) = sendAction(lid, "Index", value)}// Run on a background thread (e.g. with coroutines)val client = VoltaClient( apiUrl = "https://your-api-url.execute-api.eu-west-2.amazonaws.com", apiKey = "your-api-key", layoutId = "your-layout-id")
client.connect()val layout = client.getLayoutInfo()
// Send a button pressclient.sendButton(lid = 1)
// Send a slider valueclient.sendSlider(lid = 2, value = 0.75f)