SafeBluetooth

Uses coroutines to safely access a bluetooth GATT device with a synchronous API

The BTLE API on android is dumb. You can only have one outstanding operation in flight to the device. If you try to do something when something is pending, the operation just returns false. You are expected to chain your operations from the results callbacks.

This class fixes the API by using coroutines to let you safely do a series of BTLE operations.

Constructors

Link copied to clipboard
constructor(context: Context, device: BluetoothDevice)

Types

Link copied to clipboard
class BLEStatusException(val status: Int, msg: String) : BLEException

A BLE status code based error

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun asyncConnect(autoConnect: Boolean = false, cb: (Result<Unit>) -> Unit, lostConnectCb: () -> Unit)

start a connection attempt.

Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun asyncDiscoverServices(cb: (Result<Unit>) -> Unit)
Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun asyncReadCharacteristic(c: BluetoothGattCharacteristic, cb: (Result<BluetoothGattCharacteristic>) -> Unit)
Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun asyncRequestMtu(len: Int, cb: (Result<Unit>) -> Unit)
Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun asyncWriteCharacteristic(c: BluetoothGattCharacteristic, v: ByteArray, cb: (Result<BluetoothGattCharacteristic>) -> Unit)
Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun asyncWriteDescriptor(c: BluetoothGattDescriptor, cb: (Result<BluetoothGattDescriptor>) -> Unit)
Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun asyncWriteReliable(c: BluetoothGattCharacteristic, cb: (Result<Unit>) -> Unit)
Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
open override fun close()

Close and destroy this SafeBluetooth instance. You'll need to make a new instance before using it again

Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun closeConnection()

Close down any existing connection, any existing calls (including async connects will be cancelled and you'll need to recall connect to use this againt

Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun closeGatt()

Close just the GATT device but keep our pending callbacks active

Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun connect(autoConnect: Boolean = false)
Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun discoverServices()
Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun readCharacteristic(c: BluetoothGattCharacteristic, timeout: Long = timeoutMsec): BluetoothGattCharacteristic
Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun requestMtu(len: Int)
Link copied to clipboard

Attempts an emergency restart of the Bluetooth adapter. This is a workaround for certain BLE stack issues. It checks for necessary permissions (BLUETOOTH_CONNECT on API 31+, BLUETOOTH_ADMIN on older versions) before attempting to disable and then re-enable the adapter.

Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun setNotify(c: BluetoothGattCharacteristic, enable: Boolean, onChanged: (BluetoothGattCharacteristic) -> Unit)
Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun writeCharacteristic(c: BluetoothGattCharacteristic, v: ByteArray, timeout: Long = timeoutMsec): BluetoothGattCharacteristic
Link copied to clipboard
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
fun writeReliable(c: BluetoothGattCharacteristic)