apidocs / com.nimmsta.core.android.bluetooth / DeviceBluetoothService

DeviceBluetoothService

class DeviceBluetoothService : BluetoothGattCallback

Device Bluetooth Service for Android. This service abstracts the Android service to our Task-Based Request-Response-Model. You can read/write characteristics and be notified by a Task instead of a callback where you need to identify the request. It's also possible to manipulate the MTU and be notified about changes. For that a delegate needs to be provided, which also provides events for connection changes and errors.

Author
NIMMSTA Team

Copyright
NIMMSTA GmbH & IBPG 2020

License
NIMMSTA GmbH

Constructors

<init>

Device Bluetooth Service for Android. This service abstracts the Android service to our Task-Based Request-Response-Model. You can read/write characteristics and be notified by a Task instead of a callback where you need to identify the request. It's also possible to manipulate the MTU and be notified about changes. For that a delegate needs to be provided, which also provides events for connection changes and errors.

DeviceBluetoothService(context: Context, bleDeviceAddress: String, delegate: DeviceBluetoothServiceDelegate? = null, autoConnect: Boolean = true, doConnect: Boolean = true)

Properties

bleDevice

Represents the current BLE Device.

val bleDevice: BluetoothDevice

characteristicTimeout

The current characteristic timeout.

var characteristicTimeout: TimeSpan

currentMTU

returns the current MTU value for the connection.

val currentMTU: Int

delegate

var delegate: DeviceBluetoothServiceDelegate?

isBonded

Returns whether a device is paired.

val isBonded: Boolean

isConnected

returns the current state of the connection.

val isConnected: Boolean

lastDisconnectReason

Set by disconnectAndClose method in order to override the reason generated by the onConnectionStateChange method, which is normally BluetoothDisconnectedException.Reason.DEVICE_INITIATED, in some cases BluetoothDisconnectedException.Reason.PAIRING_FAILED.

var lastDisconnectReason: Reason?

protocolLog

val protocolLog: String

supportedGattServices

Retrieves a list of supported GATT services on the connected device. This should be invoked only after BluetoothGatt#discoverServices() completes successfully.

val supportedGattServices: Task<List<BluetoothGattService>>

Functions

connect

(re-)connects to device. When the connection was disconnected you can reconnect with this.

fun connect(autoConnect: Boolean = true): Unit

createBond

Creates a Bond with the BLE Device. Needed for HID.

fun createBond(): Task<Unit>

disconnectAndClose

Disconnects an existing connection or cancels a pending connection. The Disconnect has a TIMEOUT defined in DISCONNECT_TIMEOUT, which is 3 seconds.

fun disconnectAndClose(reason: Reason = BluetoothDisconnectedException.Reason.USER_INITIATED): Task<Unit>

discoverServices

discover services. This method retries discovering services if not already done. It returns a Task based on the promise when the operation is finished and mBluetoothGatt.services gets available.

fun discoverServices(): Task<Unit>

isCharacteristicNotifiable

fun isCharacteristicNotifiable(pChar: BluetoothGattCharacteristic): Boolean

isCharacteristicReadable

fun isCharacteristicReadable(pChar: BluetoothGattCharacteristic): Boolean

isCharacteristicWritable

fun isCharacteristicWritable(pChar: BluetoothGattCharacteristic): Boolean

listenForCharacteristic

Registers a listener for characteristic changes of a BluetoothGattCharacteristic. You will get notified once char changes. Be aware you still need to activate notifications by calling setCharacteristicNotification.

fun listenForCharacteristic(char: BluetoothGattCharacteristic, listener: (gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) -> Unit): Closeable

onCharacteristicChanged

called when characteristic changed on which a notification was registered.

fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic): Unit

onCharacteristicRead

called when characteristic read was executed.

fun onCharacteristicRead(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int): Unit

onCharacteristicWrite

called when characteristic write was executed.

fun onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int): Unit

onConnectionStateChange

called when connection state changes.

fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int): Unit

onMtuChanged

Called when MTU was changed.

fun onMtuChanged(gatt: BluetoothGatt?, mtu: Int, status: Int): Unit

onPhyUpdate

Called when Phy changed.

fun onPhyUpdate(gatt: BluetoothGatt?, txPhy: Int, rxPhy: Int, status: Int): Unit

onReadRemoteRssi

Called when RSSI was read

fun onReadRemoteRssi(gatt: BluetoothGatt?, rssi: Int, status: Int): Unit

onServicesDiscovered

called when services were discovered.

fun onServicesDiscovered(gatt: BluetoothGatt, status: Int): Unit

readCharacteristic

Request a read on a given BluetoothGattCharacteristic. The read result is reported asynchronously through the BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int) callback.

fun readCharacteristic(characteristic: BluetoothGattCharacteristic): Task<ByteArray>

readRSSI

Reads the RSSI and returns it. The RSSI is basically the signal strength.

fun readRSSI(): Task<Int>

requestHighPriority

request high connection priority.

suspend fun requestHighPriority(): Boolean?

requestMtu

request a higher MTU. The HS 50 can deal with up to 247 MTU size as of 2020-08-06

fun requestMtu(mtu: Int): Task<Int>

requestNormalPriority

request normal connection priority.

suspend fun requestNormalPriority(): Boolean?

setCharacteristicNotification

Enables or disables notification on a give characteristic.

fun setCharacteristicNotification(characteristic: BluetoothGattCharacteristic, enabled: Boolean, changeDescriptor: Boolean = true): Task<Unit>

startMonitoringRSSI

We monitor the RSSI so that we detect disconnects faster, it works like this: We check the RSSI every RSSI_INVERVAL, e.g 1 second. If RSSI does not change within the interval and is below MIN_DISCONNECT_RSSI, we check in a 30th of the interval again, until the RSSI_TIMEOUT and RSSI_CHECKS limit is reached. Then we disconnect automatically.

fun startMonitoringRSSI(): Task<Unit>

unpairDevice

Remove Bond once bonded.

fun unpairDevice(): Task<Unit>

writeCharacteristic

writes a characteristic. returns a task that indicates if it's finished.

fun writeCharacteristic(characteristic: BluetoothGattCharacteristic, characteristicValue: ByteArray): Task<Unit>

Companion Object Properties

CHARACTERISTIC_TIMEOUT

val CHARACTERISTIC_TIMEOUT: TimeSpan

CLIENT_CHARACTERISTIC_CONFIG

Guid of characteristic config to enable / disable indications.

const val CLIENT_CHARACTERISTIC_CONFIG: String

DISCONNECT_AFTER_FAILED_GATT_ATTEMPTS

const val DISCONNECT_AFTER_FAILED_GATT_ATTEMPTS: Int

DISCONNECT_STATUS_CODE_TIMEOUT

const val DISCONNECT_STATUS_CODE_TIMEOUT: Int

DISCONNECT_TIMEOUT

val DISCONNECT_TIMEOUT: TimeSpan

MAX_LOGS_TO_STORE

const val MAX_LOGS_TO_STORE: Int

MIN_DISCONNECT_RSSI

const val MIN_DISCONNECT_RSSI: Int

MTU_MAX_PACKET_SIZE

const val MTU_MAX_PACKET_SIZE: Int

RETRY_INTERVAL

val RETRY_INTERVAL: TimeSpan

RETRY_TIMEOUT

val RETRY_TIMEOUT: TimeSpan

RSSI_CHECKS

const val RSSI_CHECKS: Int

RSSI_INVERVAL

configuration for RSSI timeout. So if RSSI is not changing for at least 2.5 seconds and 10 checks (executed every 100ms), then we consider a disconnect.

val RSSI_INVERVAL: TimeSpan

RSSI_TIMEOUT

val RSSI_TIMEOUT: TimeSpan

STATE_CONNECTED

const val STATE_CONNECTED: Int

STATE_CONNECTING

const val STATE_CONNECTING: Int

STATE_DISCONNECTED

const val STATE_DISCONNECTED: Int

STATE_DISCONNECTING

const val STATE_DISCONNECTING: Int

Companion Object Functions

getBondedDevices

Returns a list of disconnected DeviceBluetoothService instances for NIMMSTA devices that already have a connection to the Android device. This can be used to disconnect bonds for those devices.

fun getBondedDevices(context: Context): List<DeviceBluetoothService>