Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Latest commit

 

History

History
45 lines (35 loc) · 1.1 KB

README.md

File metadata and controls

45 lines (35 loc) · 1.1 KB

Throw

Adds extension functions that throw exceptions on failures for various BLE operations.

fun connect(context: Context, device: BluetoothDevice) = launch {
    val gatt = device.connectGattOrThrow(context)

    try {
        gatt.discoverServicesOrThrow()

        val characteristic = gatt
            .getService("F000AA80-0451-4000-B000-000000000000".toUuid())!!
            .getCharacteristic("F000AA83-0451-4000-B000-000000000000".toUuid())
        val value = gatt.readCharacteristicOrThrow(characteristic)
        println("value = $value")
    } finally {
        gatt.disconnect(timeout = 30_000L)
    }
}

private suspend fun Gatt.disconnect(timeout: Long) {
    withContext(NonCancellable) {
        withTimeoutOrNull(timeout) {
            disconnect()
        }
    }
}

Setup

Gradle

Maven Central

repositories {
    jcenter() // or mavenCentral()
}

dependencies {
    implementation "com.juul.able:throw:$version"
}