Skip to content

Commit a3e4148

Browse files
spkerstenTNorburySander Kersten
authored
Read RSSI (#796)
* Read RSSI of connected device * Kotlin style issues * Address review comments and add tests --------- Co-authored-by: Tyler Norbury <[email protected]> Co-authored-by: Sander Kersten <[email protected]>
1 parent b397004 commit a3e4148

33 files changed

+627
-49
lines changed

example/lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void main() {
2929
return _ble.getDiscoveredServices(deviceId);
3030
},
3131
logMessage: _bleLogger.addToLog,
32+
readRssi: _ble.readRssi,
3233
);
3334
runApp(
3435
MultiProvider(

example/lib/src/ble/ble_device_interactor.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ class BleDeviceInteractor {
66
BleDeviceInteractor({
77
required Future<List<Service>> Function(String deviceId) bleDiscoverServices,
88
required void Function(String message) logMessage,
9+
required this.readRssi,
910
}) : _bleDiscoverServices = bleDiscoverServices,
1011
_logMessage = logMessage;
1112

1213
final Future<List<Service>> Function(String deviceId) _bleDiscoverServices;
1314

15+
final Future<int> Function(String deviceId) readRssi;
16+
1417
final void Function(String message) _logMessage;
1518

1619
Future<List<Service>> discoverServices(String deviceId) async {

example/lib/src/ui/device_detail/device_interaction_tab.dart

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class DeviceInteractionTab extends StatelessWidget {
2828
connectionStatus: connectionStateUpdate.connectionState,
2929
deviceConnector: deviceConnector,
3030
discoverServices: () => serviceDiscoverer.discoverServices(device.id),
31+
readRssi: () => serviceDiscoverer.readRssi(device.id),
3132
),
3233
),
3334
);
@@ -42,12 +43,14 @@ class DeviceInteractionViewModel extends $DeviceInteractionViewModel {
4243
required this.connectionStatus,
4344
required this.deviceConnector,
4445
required this.discoverServices,
46+
required this.readRssi,
4547
});
4648

4749
final String deviceId;
4850
final Connectable connectableStatus;
4951
final DeviceConnectionState connectionStatus;
5052
final BleDeviceConnector deviceConnector;
53+
final Future<int> Function() readRssi;
5154

5255
@CustomEquality(Ignore())
5356
final Future<List<Service>> Function() discoverServices;
@@ -78,6 +81,8 @@ class _DeviceInteractionTab extends StatefulWidget {
7881
class _DeviceInteractionTabState extends State<_DeviceInteractionTab> {
7982
late List<Service> discoveredServices;
8083

84+
int _rssi = 0;
85+
8186
@override
8287
void initState() {
8388
discoveredServices = [];
@@ -91,6 +96,13 @@ class _DeviceInteractionTabState extends State<_DeviceInteractionTab> {
9196
});
9297
}
9398

99+
Future<void> readRssi() async {
100+
final rssi = await widget.viewModel.readRssi();
101+
setState(() {
102+
_rssi = rssi;
103+
});
104+
}
105+
94106
@override
95107
Widget build(BuildContext context) => CustomScrollView(
96108
slivers: [
@@ -118,10 +130,17 @@ class _DeviceInteractionTabState extends State<_DeviceInteractionTab> {
118130
style: const TextStyle(fontWeight: FontWeight.bold),
119131
),
120132
),
133+
Padding(
134+
padding: const EdgeInsetsDirectional.only(start: 16.0),
135+
child: Text(
136+
"Rssi: $_rssi dB",
137+
style: const TextStyle(fontWeight: FontWeight.bold),
138+
),
139+
),
121140
Padding(
122141
padding: const EdgeInsets.only(top: 16.0),
123-
child: Row(
124-
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
142+
child: Wrap(
143+
alignment: WrapAlignment.spaceEvenly,
125144
children: <Widget>[
126145
ElevatedButton(
127146
onPressed: !widget.viewModel.deviceConnected ? widget.viewModel.connect : null,
@@ -135,6 +154,12 @@ class _DeviceInteractionTabState extends State<_DeviceInteractionTab> {
135154
onPressed: widget.viewModel.deviceConnected ? discoverServices : null,
136155
child: const Text("Discover Services"),
137156
),
157+
ElevatedButton(
158+
onPressed: widget.viewModel.deviceConnected
159+
? readRssi
160+
: null,
161+
child: const Text("Get RSSI"),
162+
),
138163
],
139164
),
140165
),

example/lib/src/ui/device_detail/device_interaction_tab.g.dart

Lines changed: 19 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/flutter_reactive_ble/lib/src/reactive_ble.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,12 @@ class FlutterReactiveBle {
398398
Future<void> clearGattCache(String deviceId) =>
399399
_blePlatform.clearGattCache(deviceId).then((info) => info.dematerialize());
400400

401+
/// Reads the RSSI of the of the peripheral with the given device ID.
402+
/// The peripheral must be connected, otherwise a [PlatformException] will be
403+
/// thrown
404+
Future<int> readRssi(String deviceId) async =>
405+
_blePlatform.readRssi(deviceId);
406+
401407
/// Subscribes to updates from the characteristic specified.
402408
///
403409
/// This stream terminates automatically when the device is disconnected.

packages/flutter_reactive_ble/test/connected_device_operation_test.mocks.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ class MockReactiveBlePlatform extends _i1.Mock
143143
_i2.Result<_i2.Unit,
144144
_i2.GenericFailure<_i2.ClearGattCacheError>?>>);
145145
@override
146+
_i4.Future<int> readRssi(String? deviceId) => (super.noSuchMethod(
147+
Invocation.method(
148+
#readRssi,
149+
[deviceId],
150+
),
151+
returnValue: _i4.Future<int>.value(0),
152+
) as _i4.Future<int>);
153+
@override
146154
_i4.Stream<void> connectToDevice(
147155
String? id,
148156
Map<_i2.Uuid, List<_i2.Uuid>>? servicesWithCharacteristicsToDiscover,

packages/flutter_reactive_ble/test/device_connector_test.mocks.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ class MockReactiveBlePlatform extends _i1.Mock
155155
_i2.Result<_i2.Unit,
156156
_i2.GenericFailure<_i2.ClearGattCacheError>?>>);
157157
@override
158+
_i4.Future<int> readRssi(String? deviceId) => (super.noSuchMethod(
159+
Invocation.method(
160+
#readRssi,
161+
[deviceId],
162+
),
163+
returnValue: _i4.Future<int>.value(0),
164+
) as _i4.Future<int>);
165+
@override
158166
_i4.Stream<void> connectToDevice(
159167
String? id,
160168
Map<_i2.Uuid, List<_i2.Uuid>>? servicesWithCharacteristicsToDiscover,

packages/flutter_reactive_ble/test/device_scanner_test.mocks.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ class MockReactiveBlePlatform extends _i1.Mock
143143
_i2.Result<_i2.Unit,
144144
_i2.GenericFailure<_i2.ClearGattCacheError>?>>);
145145
@override
146+
_i4.Future<int> readRssi(String? deviceId) => (super.noSuchMethod(
147+
Invocation.method(
148+
#readRssi,
149+
[deviceId],
150+
),
151+
returnValue: _i4.Future<int>.value(0),
152+
) as _i4.Future<int>);
153+
@override
146154
_i4.Stream<void> connectToDevice(
147155
String? id,
148156
Map<_i2.Uuid, List<_i2.Uuid>>? servicesWithCharacteristicsToDiscover,

packages/flutter_reactive_ble/test/reactive_ble_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,14 @@ void main() {
486486
});
487487
});
488488

489+
test('Read RSSI', () async {
490+
const deviceId = '123';
491+
492+
when(_blePlatform.readRssi(deviceId)).thenAnswer((_) async => -42);
493+
494+
expect(await _sut.readRssi(deviceId), -42);
495+
});
496+
489497
group('ConnecteddeviceStream stream', () {
490498
const update = ConnectionStateUpdate(
491499
deviceId: '123',

packages/flutter_reactive_ble/test/reactive_ble_test.mocks.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ class MockReactiveBlePlatform extends _i1.Mock
146146
_i2.Result<_i2.Unit,
147147
_i2.GenericFailure<_i2.ClearGattCacheError>?>>);
148148
@override
149+
_i4.Future<int> readRssi(String? deviceId) => (super.noSuchMethod(
150+
Invocation.method(
151+
#readRssi,
152+
[deviceId],
153+
),
154+
returnValue: _i4.Future<int>.value(0),
155+
) as _i4.Future<int>);
156+
@override
149157
_i4.Stream<void> connectToDevice(
150158
String? id,
151159
Map<_i2.Uuid, List<_i2.Uuid>>? servicesWithCharacteristicsToDiscover,

0 commit comments

Comments
 (0)