-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
BluetoothManager.kt
85 lines (75 loc) · 2.76 KB
/
BluetoothManager.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
package com.harrysoft.androidbluetoothserial
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import io.reactivex.Single
import java.nio.charset.Charset
interface BluetoothManager : AutoCloseable {
/**
* A collection of paired Bluetooth devices, not restricted to serial devices.
*/
val pairedDevices: Collection<BluetoothDevice>
/**
* A collection of paired Bluetooth devices, not restricted to serial devices.
*/
@Deprecated("Use pairedDevices instead", replaceWith = ReplaceWith("pairedDevices"))
val pairedDevicesList: List<BluetoothDevice> get() = pairedDevices.toList()
/**
* @param mac The MAC address of the device
* you are trying to connect to
* @return An RxJava Single, that will either emit
* a BluetoothSerialDevice or a BluetoothConnectException
*/
fun openSerialDevice(mac: String): Single<BluetoothSerialDevice>
/**
* @param mac The MAC address of the device
* you are trying to connect to
* @param charset The Charset to use for input/output streams
* @return An RxJava Single, that will either emit
* a BluetoothSerialDevice or a BluetoothConnectException
*/
fun openSerialDevice(mac: String, charset: Charset): Single<BluetoothSerialDevice>
/**
* Closes the connection to a device. After calling,
* you should probably set your instance to null
* to avoid trying to read/write from it.
*
* @param mac The MAC Address of the device you are
* trying to close the connection to
*/
fun closeDevice(mac: String)
/**
* Closes the connection to a device. After calling,
* you should probably set your instance to null
* to avoid trying to read/write from it.
*
* @param device The instance of the device you are
* trying to close the connection to
*/
fun closeDevice(device: BluetoothSerialDevice)
/**
* Closes the connection to a device. After calling,
* you should probably set your instance to null
* to avoid trying to read/write from it.
*
* @param deviceInterface The interface accessing the device
* you are trying to close the connection to
*/
fun closeDevice(deviceInterface: SimpleBluetoothDeviceInterface)
/**
* Closes all connected devices
*/
override fun close()
companion object {
/**
* @return A BluetoothManager instance if the device
* has Bluetooth or null otherwise.
*/
@JvmStatic
val instance: BluetoothManager? by lazy {
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (bluetoothAdapter != null) {
BluetoothManagerImpl(bluetoothAdapter)
} else null
}
}
}