Skip to content

Commit

Permalink
feat(android): Clear the gatt cache before discover devices (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwiky authored Oct 31, 2024
1 parent 968c6a6 commit ec37c4b
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions library/src/androidMain/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import android.bluetooth.le.*
import android.content.Context
import android.os.Build
import android.os.ParcelUuid
import android.os.RemoteException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import java.nio.ByteBuffer
import java.util.*


actual class BlueFalcon actual constructor(
private val log: Logger,
private val context: ApplicationContext
Expand All @@ -31,12 +33,12 @@ actual class BlueFalcon actual constructor(

actual fun connect(bluetoothPeripheral: BluetoothPeripheral, autoConnect: Boolean) {
log.info("connect")
bluetoothPeripheral.bluetoothDevice.connectGatt(
context,
autoConnect,
mGattClientCallback,
transportMethod
)
bluetoothPeripheral.bluetoothDevice.connectGatt(
context,
autoConnect,
mGattClientCallback,
transportMethod
)
}

actual fun disconnect(bluetoothPeripheral: BluetoothPeripheral) {
Expand Down Expand Up @@ -284,6 +286,23 @@ actual class BlueFalcon actual constructor(
fun gattForDevice(bluetoothDevice: BluetoothDevice): BluetoothGatt? =
gatts.firstOrNull { it.device == bluetoothDevice }

/**
* Clears the internal cache and forces a refresh of the services from the
* remote device.
* @hide
*/
private fun refresh(gatt: BluetoothGatt): Boolean {
try {
val refresh = gatt.javaClass.getMethod("refresh")
refresh.invoke(gatt)
} catch (e: RemoteException) {
log.error("Could not execute refresh", e)
return false
}

return true
}

override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) {
super.onConnectionStateChange(gatt, status, newState)
log.info("onConnectionStateChange")
Expand All @@ -292,6 +311,7 @@ actual class BlueFalcon actual constructor(
//BluetoothProfile#STATE_DISCONNECTED} or {@link BluetoothProfile#STATE_CONNECTED}
if (newState == STATE_CONNECTED) {
addGatt(bluetoothGatt)
refresh(bluetoothGatt)
bluetoothGatt.readRemoteRssi()
bluetoothGatt.discoverServices()
delegates.forEach {
Expand Down

0 comments on commit ec37c4b

Please sign in to comment.