This repository was archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGattIo.kt
103 lines (88 loc) · 3.52 KB
/
GattIo.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright 2020 JUUL Labs, Inc.
*/
package com.juul.able.gatt
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
import android.bluetooth.BluetoothGattDescriptor
import android.bluetooth.BluetoothGattService
import android.os.RemoteException
import java.util.UUID
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.Flow
/**
* Represents the possible GATT statuses as defined in [BluetoothGatt]:
*
* - [BluetoothGatt.GATT_SUCCESS]
* - [BluetoothGatt.GATT_READ_NOT_PERMITTED]
* - [BluetoothGatt.GATT_WRITE_NOT_PERMITTED]
* - [BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION]
* - [BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED]
* - [BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION]
* - [BluetoothGatt.GATT_INVALID_OFFSET]
* - [BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH]
* - [BluetoothGatt.GATT_CONNECTION_CONGESTED]
* - [BluetoothGatt.GATT_FAILURE]
*/
typealias GattStatus = Int
/**
* Represents the possible [BluetoothGattCharacteristic] write types:
*
* - [BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT]
* - [BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE]
* - [BluetoothGattCharacteristic.WRITE_TYPE_SIGNED]
*/
typealias WriteType = Int
interface GattIo {
/**
* @throws [RemoteException] if underlying [BluetoothGatt.discoverServices] returns `false`.
* @throws [ConnectionLost] if [Gatt] disconnects while method is executing.
*/
suspend fun discoverServices(): GattStatus
val services: List<BluetoothGattService>
fun getService(uuid: UUID): BluetoothGattService?
@FlowPreview
val onCharacteristicChanged: Flow<OnCharacteristicChanged>
/**
* @throws [RemoteException] if underlying [BluetoothGatt.requestMtu] returns `false`.
* @throws [ConnectionLost] if [Gatt] disconnects while method is executing.
*/
suspend fun requestMtu(mtu: Int): OnMtuChanged
/**
* @throws [RemoteException] if underlying [BluetoothGatt.readCharacteristic] returns `false`.
* @throws [ConnectionLost] if [Gatt] disconnects while method is executing.
*/
suspend fun readCharacteristic(
characteristic: BluetoothGattCharacteristic
): OnCharacteristicRead
/**
* @param value applied to [characteristic] when characteristic is written.
* @param writeType applied to [characteristic] when characteristic is written.
* @throws [RemoteException] if underlying [BluetoothGatt.writeCharacteristic] returns `false`.
* @throws [ConnectionLost] if [Gatt] disconnects while method is executing.
*/
suspend fun writeCharacteristic(
characteristic: BluetoothGattCharacteristic,
value: ByteArray,
writeType: WriteType
): OnCharacteristicWrite
/**
* @param value applied to [descriptor] when descriptor is written.
* @throws [RemoteException] if underlying [BluetoothGatt.writeDescriptor] returns `false`.
* @throws [ConnectionLost] if [Gatt] disconnects while method is executing.
*/
suspend fun writeDescriptor(
descriptor: BluetoothGattDescriptor,
value: ByteArray
): OnDescriptorWrite
fun setCharacteristicNotification(
characteristic: BluetoothGattCharacteristic,
enable: Boolean
): Boolean
suspend fun readRemoteRssi(): OnReadRemoteRssi
}
suspend fun GattIo.writeCharacteristic(
characteristic: BluetoothGattCharacteristic,
value: ByteArray
): OnCharacteristicWrite = writeCharacteristic(characteristic, value, WRITE_TYPE_DEFAULT)