BluetoothLe


class BluetoothLe


Entry point for BLE related operations. This class provides a way to perform Bluetooth LE operations such as scanning, advertising, and connection with a respective BluetoothDevice.

Summary

Constants

const Int

Advertise started successfully.

Public constructors

BluetoothLe(context: Context)

Public functions

Flow<Int>
@RequiresPermission(value = "android.permission.BLUETOOTH_ADVERTISE")
advertise(advertiseParams: AdvertiseParams)

Returns a cold Flow to start Bluetooth LE advertising

suspend R
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
<R : Any?> connectGatt(device: BluetoothDevice, block: suspend GattClientScope.() -> R)

Connects to the GATT server on the remote Bluetooth device and invokes the given block after the connection is made.

GattServerConnectFlow

Opens a GATT server.

Flow<ScanResult>
@RequiresPermission(value = "android.permission.BLUETOOTH_SCAN")
scan(filters: List<ScanFilter>)

Returns a cold Flow to start Bluetooth LE scanning.

Constants

const val ADVERTISE_STARTED = 10100: Int

Advertise started successfully.

Public constructors

BluetoothLe

Added in 1.0.0-alpha02
BluetoothLe(context: Context)

Public functions

advertise

Added in 1.0.0-alpha02
@RequiresPermission(value = "android.permission.BLUETOOTH_ADVERTISE")
fun advertise(advertiseParams: AdvertiseParams): Flow<Int>

Returns a cold Flow to start Bluetooth LE advertising

Note that this method may not complete if the duration is set to 0. To stop advertising, in that case, you should cancel the coroutine.

Parameters
advertiseParams: AdvertiseParams

AdvertiseParams for Bluetooth LE advertising.

Returns
Flow<Int>

a cold Flow of ADVERTISE_STARTED if advertising is started.

Throws
androidx.bluetooth.AdvertiseException

if the advertise fails.

kotlin.IllegalArgumentException

if the advertise parameters are not valid.

connectGatt

@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
suspend fun <R : Any?> connectGatt(device: BluetoothDevice, block: suspend GattClientScope.() -> R): R

Connects to the GATT server on the remote Bluetooth device and invokes the given block after the connection is made.

The block may not be run if connection fails.

Parameters
device: BluetoothDevice

a BluetoothDevice to connect to

block: suspend GattClientScope.() -> R

a block of code that is invoked after the connection is made

Returns
R

a result returned by the given block if the connection was successfully finished or a failure with the corresponding reason

Throws
kotlinx.coroutines.CancellationException

if connect failed or it's canceled

openGattServer

Added in 1.0.0-alpha02
fun openGattServer(services: List<GattService>): GattServerConnectFlow

Opens a GATT server.

Only one server at a time can be opened.

Parameters
services: List<GattService>

the services that will be exposed to the clients

scan

Added in 1.0.0-alpha02
@RequiresPermission(value = "android.permission.BLUETOOTH_SCAN")
fun scan(filters: List<ScanFilter> = emptyList()): Flow<ScanResult>

Returns a cold Flow to start Bluetooth LE scanning. Scanning is used to discover advertising devices nearby.

Parameters
filters: List<ScanFilter> = emptyList()

ScanFilters for finding exact Bluetooth LE devices.

Returns
Flow<ScanResult>

a cold Flow of ScanResult that matches with the given scan filter.

Throws
androidx.bluetooth.ScanException

if the scan fails.