Implement readRSSI on Android and Darwin (#203)

This commit is contained in:
EricKwok
2026-01-06 00:25:19 +08:00
committed by Foti Dim
parent a5b6af6168
commit 1adfa9b485
25 changed files with 456 additions and 91 deletions
+13
View File
@@ -26,6 +26,19 @@ class BleDevice {
Future<BleConnectionState> get connectionState =>
UniversalBle.getConnectionState(deviceId);
/// Read the RSSI value of this connected device.
///
/// Returns the current RSSI value in dBm. This value indicates the signal strength
/// between the device and the connected peripheral. Lower (more negative) values
/// indicate weaker signal, while higher (less negative) values indicate stronger signal.
///
/// **Note**: The device must be connected before reading RSSI.
///
/// Throws [BleException] if:
/// - The device is not connected
/// - Reading RSSI fails
Future<int> readRssi() => UniversalBle.readRssi(deviceId);
/// On web, it returns true if the web browser supports receiving advertisements from this device.
/// The rest of the platforms will always return true.
bool get receivesAdvertisements =>
+22
View File
@@ -340,6 +340,28 @@ class UniversalBle {
);
}
/// Read the RSSI value of a connected device.
///
/// Returns the current RSSI value in dBm. This value indicates the signal strength
/// between the device and the connected peripheral. Lower (more negative) values
/// indicate weaker signal, while higher (less negative) values indicate stronger signal.
///
/// **Note**: The device must be connected before reading RSSI.
///
/// Throws [BleException] if:
/// - The device is not connected
/// - Reading RSSI fails
static Future<int> readRssi(
String deviceId, {
Duration? timeout,
}) async {
return await _bleCommandQueue.queueCommand(
() => _platform.readRssi(deviceId),
timeout: timeout,
deviceId: deviceId,
);
}
/// Check if a device is paired.
///
/// For `Apple` and `Web`, you have to pass a "pairingCommand" with an encrypted read or write characteristic.
@@ -378,6 +378,14 @@ class UniversalBleLinux extends UniversalBlePlatform {
);
}
@override
Future<int> readRssi(String deviceId) async {
throw UniversalBleException(
code: UniversalBleErrorCode.notImplemented,
message: "readRssi is not implemented on Linux platform",
);
}
@override
Future<bool> pair(String deviceId) async {
BlueZDevice device = _findDeviceById(deviceId);
@@ -1106,6 +1106,35 @@ class UniversalBlePlatformChannel {
}
}
Future<int> readRssi(String deviceId) async {
final pigeonVar_channelName =
'dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.readRssi$pigeonVar_messageChannelSuffix';
final pigeonVar_channel = BasicMessageChannel<Object?>(
pigeonVar_channelName,
pigeonChannelCodec,
binaryMessenger: pigeonVar_binaryMessenger,
);
final Future<Object?> pigeonVar_sendFuture =
pigeonVar_channel.send(<Object?>[deviceId]);
final pigeonVar_replyList = await pigeonVar_sendFuture as List<Object?>?;
if (pigeonVar_replyList == null) {
throw _createConnectionError(pigeonVar_channelName);
} else if (pigeonVar_replyList.length > 1) {
throw PlatformException(
code: pigeonVar_replyList[0]! as String,
message: pigeonVar_replyList[1] as String?,
details: pigeonVar_replyList[2],
);
} else if (pigeonVar_replyList[0] == null) {
throw PlatformException(
code: 'null-error',
message: 'Host platform returned null value for non-null return value.',
);
} else {
return (pigeonVar_replyList[0] as int?)!;
}
}
Future<void> setLogLevel(UniversalBleLogLevel logLevel) async {
final pigeonVar_channelName =
'dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.setLogLevel$pigeonVar_messageChannelSuffix';
@@ -133,6 +133,10 @@ class UniversalBlePigeonChannel extends UniversalBlePlatform {
_executeWithErrorHandling(
() => _channel.requestMtu(deviceId, expectedMtu));
@override
Future<int> readRssi(String deviceId) =>
_executeWithErrorHandling(() => _channel.readRssi(deviceId));
@override
Future<bool> isPaired(String deviceId) =>
_executeWithErrorHandling(() => _channel.isPaired(deviceId));
@@ -79,6 +79,8 @@ abstract class UniversalBlePlatform {
Future<int> requestMtu(String deviceId, int expectedMtu);
Future<int> readRssi(String deviceId);
Future<bool> isPaired(String deviceId);
Future<bool> pair(String deviceId);
@@ -283,6 +283,15 @@ class UniversalBleWeb extends UniversalBlePlatform {
);
}
/// `Unimplemented`
@override
Future<int> readRssi(String deviceId) {
throw UniversalBleException(
code: UniversalBleErrorCode.notImplemented,
message: "readRssi is not implemented on Web platform",
);
}
@override
Future<bool> isPaired(String deviceId) {
throw UniversalBleException(