Skip to content

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")
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import 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 press
client.sendButton(lid = 1)
// Send a slider value
client.sendSlider(lid = 2, value = 0.75f)