From 4f1a055c4304cfac9063f25345278af44571b269 Mon Sep 17 00:00:00 2001 From: Travis Wyatt Date: Thu, 3 Dec 2020 22:11:33 -0800 Subject: [PATCH] Add requestConnectionPriority --- core/src/main/java/gatt/CoroutinesGatt.kt | 11 +++++++++++ core/src/main/java/gatt/GattConnection.kt | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/core/src/main/java/gatt/CoroutinesGatt.kt b/core/src/main/java/gatt/CoroutinesGatt.kt index 5a9e7c0..27d9487 100644 --- a/core/src/main/java/gatt/CoroutinesGatt.kt +++ b/core/src/main/java/gatt/CoroutinesGatt.kt @@ -44,6 +44,10 @@ class CoroutinesGatt internal constructor( override val services: List get() = bluetoothGatt.services override fun getService(uuid: UUID): BluetoothGattService? = bluetoothGatt.getService(uuid) + override fun requestConnectionPriority( + priority: ConnectionPriority + ): Boolean = bluetoothGatt.requestConnectionPriority(priority.intValue) + override suspend fun disconnect() { try { Able.info { "Disconnecting $this" } @@ -157,3 +161,10 @@ class CoroutinesGatt internal constructor( override fun toString(): String = "CoroutinesGatt(device=${bluetoothGatt.device})" } + +private val ConnectionPriority.intValue: Int + get() = when (this) { + ConnectionPriority.Low -> BluetoothGatt.CONNECTION_PRIORITY_LOW_POWER + ConnectionPriority.Balanced -> BluetoothGatt.CONNECTION_PRIORITY_BALANCED + ConnectionPriority.High -> BluetoothGatt.CONNECTION_PRIORITY_HIGH + } diff --git a/core/src/main/java/gatt/GattConnection.kt b/core/src/main/java/gatt/GattConnection.kt index faceb53..b2b5953 100644 --- a/core/src/main/java/gatt/GattConnection.kt +++ b/core/src/main/java/gatt/GattConnection.kt @@ -37,11 +37,15 @@ typealias GattConnectionStatus = Int */ typealias GattConnectionState = Int +enum class ConnectionPriority { Low, Balanced, High } + interface GattConnection { @FlowPreview val onConnectionStateChange: Flow + fun requestConnectionPriority(priority: ConnectionPriority): Boolean + suspend fun disconnect(): Unit }