1
1
part of flutter_ble_lib;
2
2
3
- typedef RestoreStateAction = Function (List <Peripheral > restoreStateIdentifier);
3
+ /// Callback used to inform about peripherals restored by the system.
4
+ ///
5
+ /// iOS-specific.
6
+ typedef RestoreStateAction = Function (List <Peripheral > peripherals);
4
7
8
+ /// Level of details library is to output in logs.
5
9
enum LogLevel { none, verbose, debug, info, warning, error }
6
10
11
+ /// Entry point for library operations, handling allocation and deallocation
12
+ /// of underlying native resources and obtaining [Peripheral] instances.
13
+ ///
14
+ /// The class is a singleton, so there's no need to keep the reference to
15
+ /// the object in one's code.
16
+ ///
17
+ /// Initialising/deinitialising native clients:
18
+ /// ```dart
19
+ /// BleManager bleManager = BleManager();
20
+ /// await bleManager.createClient(); //ready to go!
21
+ /// //your BLE logic
22
+ /// bleManager.destroyClient(); //remember to release native resources when they're no longer needed
23
+ /// ```
24
+ ///
25
+ /// Obtaining [Peripheral] :
26
+ /// ```dart
27
+ /// bleManager.startPeripheralScan().listen((scanResult) {
28
+ /// //Scan one peripheral and stop scanning
29
+ /// print("Scanned Peripheral ${scanResult.peripheral.name}, RSSI ${scanResult.rssi}");
30
+ /// bleManager.stopPeripheralScan(); // stops the scan
31
+ ///});
32
+ ///```
7
33
abstract class BleManager {
8
34
static BleManager _instance;
9
35
@@ -15,41 +41,107 @@ abstract class BleManager {
15
41
return _instance;
16
42
}
17
43
44
+ /// Cancels transaction's return, resulting in [BleError] with
45
+ /// [BleError.errorCode] set to [BleErrorCode.OperationCancelled] being returned
46
+ /// from transaction's Future.
47
+ ///
48
+ /// The operation might be cancelled if it hadn't yet started or be run
49
+ /// normally, eg. writing to
50
+ /// characteristic, but you can dismiss awaiting for the result if,
51
+ /// for example, the result is no longer useful due to user's actions.
18
52
Future <void > cancelTransaction (String transactionId);
19
53
54
+ /// Allocates native resources.
55
+ ///
56
+ /// [restoreStateIdentifier] and [restoreStateAction] are iOS-specific.
57
+ ///
58
+ /// Must return before any other operation can be called.
59
+ ///
60
+ /// ```dart
61
+ /// await BleManager().createClient();
62
+ /// ```
20
63
Future <void > createClient ({
21
64
String restoreStateIdentifier,
22
65
RestoreStateAction restoreStateAction,
23
66
});
24
67
68
+ /// Frees native resources.
69
+ ///
70
+ /// After calling this method you must call again [createClient()] before
71
+ /// any BLE operation.
25
72
Future <void > destroyClient ();
26
73
74
+ /// Starts scanning for peripherals.
75
+ ///
76
+ /// Arguments [scanMode] and [callbackType] are Android-only,
77
+ /// while [allowDuplicates] is iOS-only. Note that [allowDuplicates] set to
78
+ /// false will only result in slower refresh rate for unique peripheral's
79
+ /// advertisement data, not dismissal of it after receiving the initial one.
80
+ /// Refer to each platform's own documentation for more detailed information.
81
+ ///
82
+ /// [uuids] is used to filter scan results to those whose advertised service
83
+ /// match either of the specified UUIDs.
84
+ ///
85
+ /// ```dart
86
+ /// bleManager.startPeripheralScan().listen((scanResult) {
87
+ /// //Scan one peripheral and stop scanning
88
+ /// print("Scanned Peripheral ${scanResult.peripheral.name}, RSSI ${scanResult.rssi}");
89
+ /// bleManager.stopPeripheralScan();
90
+ /// });
91
+ /// ```
27
92
Stream <ScanResult > startPeripheralScan ({
28
93
int scanMode = ScanMode .lowPower,
29
94
int callbackType = CallbackType .allMatches,
30
95
List <String > uuids = const [],
31
96
bool allowDuplicates = false ,
32
97
});
33
98
99
+ /// Finishes the scan operation on the device.
34
100
Future <void > stopPeripheralScan ();
35
101
102
+ /// Sets specified [LogLevel] .
103
+ ///
104
+ /// This sets log level for both Dart and native platform.
36
105
Future <void > setLogLevel (LogLevel logLevel);
37
106
107
+ /// Returns current [LogLevel] .
38
108
Future <LogLevel > logLevel ();
39
109
110
+ /// Enables Bluetooth on Android; NOOP on iOS.
111
+ ///
112
+ /// Passing optional [transactionId] lets you discard the result of this
113
+ /// operation before it is finished.
40
114
Future <void > enableRadio ({String transactionId});
41
115
116
+ /// Disables Bluetooth on Android; NOOP on iOS.
117
+ ///
118
+ /// Passing optional [transactionId] lets you discard the result of this
119
+ /// operation before it is finished.
42
120
Future <void > disableRadio ({String transactionId});
43
121
122
+ /// Returns current state of the Bluetooth adapter.
44
123
Future <BluetoothState > bluetoothState ();
45
124
125
+ /// Returns a stream of changes to the state of the Bluetooth adapter.
126
+ ///
127
+ /// By default starts the stream with the current state, but this can
128
+ /// overridden by passing `false` as [emitCurrentValue] .
46
129
Stream <BluetoothState > observeBluetoothState ({bool emitCurrentValue = true });
47
130
131
+ /// Returns a list of [Peripheral] : on iOS known to system, on Android
132
+ /// known to the library.
133
+ ///
134
+ /// If [peripheralIdentifiers] is empty, this will return an empty list.
48
135
Future <List <Peripheral >> knownPeripherals (List <String > peripheralIdentifiers);
49
136
137
+ /// Returns a list of [Peripheral] : on iOS connected and known to system,
138
+ /// on Android connected and known to the library.
139
+ ///
140
+ /// If [serviceUUIDs] is empty, this will return an empty list.
50
141
Future <List <Peripheral >> connectedPeripherals (List <String > serviceUUIDs);
51
142
}
52
143
144
+ /// State of the Bluetooth Adapter.
53
145
enum BluetoothState {
54
146
UNKNOWN ,
55
147
UNSUPPORTED ,
@@ -59,13 +151,19 @@ enum BluetoothState {
59
151
RESETTING ,
60
152
}
61
153
154
+ /// Mode of scan for peripherals - Android only.
155
+ ///
156
+ /// See [Android documentation] (https://developer.android.com/reference/android/bluetooth/le/ScanSettings) for more information.
62
157
abstract class ScanMode {
63
158
static const int opportunistic = - 1 ;
64
159
static const int lowPower = 0 ;
65
160
static const int balanced = 1 ;
66
161
static const int lowLatency = 2 ;
67
162
}
68
163
164
+ /// Type of scan for peripherals callback - Android only.
165
+ ///
166
+ /// See [Android documentation] (https://developer.android.com/reference/android/bluetooth/le/ScanSettings) for more information.
69
167
abstract class CallbackType {
70
168
static const int allMatches = 1 ;
71
169
static const int firstMatch = 2 ;
0 commit comments