Implement isScanning api (#188)

* Implement isScanning api

* Improve IOS scanning api

* Update Readme and Changelog
This commit is contained in:
Rohit Sangwan
2025-11-12 19:27:44 +05:30
committed by GitHub
parent c3b97e414f
commit 34b39597fd
23 changed files with 193 additions and 12 deletions
+8
View File
@@ -88,6 +88,14 @@ class UniversalBle {
);
}
/// Check if currently scanning for devices.
/// Returns `true` if scanning is active, `false` otherwise.
static Future<bool> isScanning() async {
return await _bleCommandQueue.queueCommand(
() => _platform.isScanning(),
);
}
/// Connect to a device.
/// It is advised to stop scanning before connecting.
/// It throws error if device connection fails.
@@ -125,6 +125,12 @@ class UniversalBleLinux extends UniversalBlePlatform {
}
}
@override
Future<bool> isScanning() async {
await _ensureInitialized();
return _activeAdapter?.discovering == true;
}
@override
Future<BleConnectionState> getConnectionState(String deviceId) async {
BlueZDevice? device = _devices[deviceId] ??
@@ -1,4 +1,4 @@
// Autogenerated from Pigeon (v26.0.5), do not edit directly.
// Autogenerated from Pigeon (v26.1.0), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import, no_leading_underscores_for_local_identifiers
@@ -633,6 +633,36 @@ class UniversalBlePlatformChannel {
}
}
Future<bool> isScanning() async {
final String pigeonVar_channelName =
'dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.isScanning$pigeonVar_messageChannelSuffix';
final BasicMessageChannel<Object?> pigeonVar_channel =
BasicMessageChannel<Object?>(
pigeonVar_channelName,
pigeonChannelCodec,
binaryMessenger: pigeonVar_binaryMessenger,
);
final Future<Object?> pigeonVar_sendFuture = pigeonVar_channel.send(null);
final List<Object?>? 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 bool?)!;
}
}
Future<void> connect(String deviceId) async {
final String pigeonVar_channelName =
'dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.connect$pigeonVar_messageChannelSuffix';
@@ -57,6 +57,10 @@ class UniversalBlePigeonChannel extends UniversalBlePlatform {
Future<void> stopScan() =>
_executeWithErrorHandling(() => _channel.stopScan());
@override
Future<bool> isScanning() =>
_executeWithErrorHandling(() => _channel.isScanning());
@override
Future<BleConnectionState> getConnectionState(String deviceId) async {
int state = await _executeWithErrorHandling(
@@ -43,6 +43,8 @@ abstract class UniversalBlePlatform {
Future<void> stopScan();
Future<bool> isScanning();
Future<void> connect(String deviceId, {Duration? connectionTimeout});
Future<void> disconnect(String deviceId);
@@ -20,6 +20,7 @@ class UniversalBleWeb extends UniversalBlePlatform {
final Map<String, StreamSubscription> _connectedDeviceStreamList = {};
final Map<String, StreamSubscription> _characteristicStreamList = {};
final Map<String, List<_UniversalWebBluetoothService>> _serviceCache = {};
bool _isScanning = false;
@override
Future<BleConnectionState> getConnectionState(String deviceId) async {
@@ -83,6 +84,7 @@ class UniversalBleWeb extends UniversalBlePlatform {
PlatformConfig? platformConfig,
}) async {
try {
_isScanning = true;
FlutterWebBluetooth.instance.isAvailable;
BluetoothDevice device = await FlutterWebBluetooth.instance.requestDevice(
_getRequestOptionBuilder(scanFilter, platformConfig?.web),
@@ -101,6 +103,8 @@ class UniversalBleWeb extends UniversalBlePlatform {
throw WebBluetoothGloballyDisabled(message: error);
}
rethrow;
} finally {
_isScanning = false;
}
}
@@ -146,6 +150,9 @@ class UniversalBleWeb extends UniversalBlePlatform {
_disposeAdvertisementWatcher();
}
@override
Future<bool> isScanning() async => _isScanning;
@override
Future<void> setNotifiable(
String deviceId,