BluetoothLe


public final 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

static final int

Advertise started successfully.

Public constructors

Public methods

final @NonNull Flow<@NonNull Integer>
@RequiresPermission(value = "android.permission.BLUETOOTH_ADVERTISE")
advertise(@NonNull AdvertiseParams advertiseParams)

Returns a cold Flow to start Bluetooth LE advertising

final @NonNull R
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
<R extends Object> connectGatt(
    @NonNull BluetoothDevice device,
    @ExtensionFunctionType @NonNull SuspendFunction1<@NonNull GattClientScope, @NonNull R> block
)

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

final @NonNull GattServerConnectFlow

Opens a GATT server.

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

Returns a cold Flow to start Bluetooth LE scanning.

Constants

public static final int ADVERTISE_STARTED = 10100

Advertise started successfully.

Public constructors

BluetoothLe

Added in 1.0.0-alpha02
public BluetoothLe(@NonNull Context context)

Public methods

advertise

Added in 1.0.0-alpha02
@RequiresPermission(value = "android.permission.BLUETOOTH_ADVERTISE")
public final @NonNull Flow<@NonNull Integeradvertise(@NonNull AdvertiseParams advertiseParams)

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
@NonNull AdvertiseParams advertiseParams

AdvertiseParams for Bluetooth LE advertising.

Returns
@NonNull Flow<@NonNull Integer>

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")
public final @NonNull R <R extends Object> connectGatt(
    @NonNull BluetoothDevice device,
    @ExtensionFunctionType @NonNull SuspendFunction1<@NonNull GattClientScope, @NonNull R> block
)

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
@NonNull BluetoothDevice device

a BluetoothDevice to connect to

@ExtensionFunctionType @NonNull SuspendFunction1<@NonNull GattClientScope, @NonNull R> block

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

Returns
@NonNull 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
public final @NonNull GattServerConnectFlow openGattServer(@NonNull List<@NonNull GattService> services)

Opens a GATT server.

Only one server at a time can be opened.

Parameters
@NonNull List<@NonNull GattService> services

the services that will be exposed to the clients

scan

Added in 1.0.0-alpha02
@RequiresPermission(value = "android.permission.BLUETOOTH_SCAN")
public final @NonNull Flow<@NonNull ScanResultscan(@NonNull List<@NonNull ScanFilter> filters)

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

Parameters
@NonNull List<@NonNull ScanFilter> filters

ScanFilters for finding exact Bluetooth LE devices.

Returns
@NonNull Flow<@NonNull ScanResult>

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

Throws
androidx.bluetooth.ScanException

if the scan fails.