BLE select cancel

This commit is contained in:
Ben Allfree
2026-02-22 08:05:19 -08:00
parent b4f79c1aae
commit 5522f9a236
2 changed files with 111 additions and 63 deletions
+49 -8
View File
@@ -686,12 +686,19 @@ class MeshCoreConnector extends ChangeNotifier {
}) async {
if (_state == MeshCoreConnectionState.scanning) return;
try {
_scanResults.clear();
_setState(MeshCoreConnectionState.scanning);
// Ensure any previous scan is fully stopped
try {
await FlutterBluePlus.stopScan();
} catch (_) {}
try {
await _scanSubscription?.cancel();
} catch (_) {}
_scanSubscription = null;
// On iOS/macOS, wait for Bluetooth to be powered on before scanning
if (PlatformInfo.isIOS || PlatformInfo.isMacOS) {
@@ -714,7 +721,8 @@ class MeshCoreConnector extends ChangeNotifier {
await Future.delayed(const Duration(milliseconds: 300));
}
_scanSubscription = FlutterBluePlus.scanResults.listen((results) {
_scanSubscription = FlutterBluePlus.scanResults.listen(
(results) {
_scanResults.clear();
for (var result in results) {
if (result.device.platformName.startsWith("MeshCore-") ||
@@ -724,7 +732,12 @@ class MeshCoreConnector extends ChangeNotifier {
}
}
notifyListeners();
});
},
onError: (Object e) {
debugPrint("scanResults stream error: $e");
stopScan();
},
);
await FlutterBluePlus.startScan(
withServices: [Guid(MeshCoreUuids.service)],
@@ -733,17 +746,36 @@ class MeshCoreConnector extends ChangeNotifier {
);
await Future.delayed(timeout);
} catch (e) {
debugPrint("Scan error: $e");
// On web, suppress common cancellation and chooser errors
if (kIsWeb) return;
if (!PlatformInfo.isWeb) {
rethrow;
}
} finally {
await stopScan();
}
}
Future<void> stopScan() async {
await FlutterBluePlus.stopScan();
await _scanSubscription?.cancel();
_scanSubscription = null;
if (_state == MeshCoreConnectionState.scanning) {
_setState(MeshCoreConnectionState.disconnected);
}
try {
await FlutterBluePlus.stopScan();
} catch (e) {
debugPrint("stopScan error: $e");
}
try {
if (_scanSubscription != null) {
await _scanSubscription!.cancel();
}
} catch (_) {}
_scanSubscription = null;
}
Future<void> connect(BluetoothDevice device, {String? displayName}) async {
@@ -770,11 +802,17 @@ class MeshCoreConnector extends ChangeNotifier {
notifyListeners();
try {
_connectionSubscription = device.connectionState.listen((state) {
_connectionSubscription = device.connectionState.listen(
(state) {
if (state == BluetoothConnectionState.disconnected && isConnected) {
_handleDisconnection();
}
});
},
onError: (Object e) {
debugPrint("connectionState stream error: $e");
if (isConnected) _handleDisconnection();
},
);
await device.connect(
timeout: const Duration(seconds: 15),
@@ -833,6 +871,9 @@ class MeshCoreConnector extends ChangeNotifier {
}
_notifySubscription = _txCharacteristic!.onValueReceived.listen(
_handleFrame,
onError: (Object e) {
debugPrint("onValueReceived stream error: $e");
},
);
_setState(MeshCoreConnectionState.connected);
+10 -3
View File
@@ -45,7 +45,8 @@ class _ScannerScreenState extends State<ScannerScreen> {
connector.addListener(_connectionListener);
_bluetoothStateSubscription = FlutterBluePlus.adapterState.listen((state) {
_bluetoothStateSubscription = FlutterBluePlus.adapterState.listen(
(state) {
if (mounted) {
setState(() {
_bluetoothState = state;
@@ -55,7 +56,11 @@ class _ScannerScreenState extends State<ScannerScreen> {
unawaited(connector.stopScan());
}
}
});
},
onError: (Object e) {
debugPrint("Scanner adapterState stream error: $e");
},
);
}
@override
@@ -107,7 +112,9 @@ class _ScannerScreenState extends State<ScannerScreen> {
if (isScanning) {
connector.stopScan();
} else {
connector.startScan();
unawaited(connector.startScan().catchError((e) {
debugPrint("Scanner screen startScan error: $e");
}));
}
},
icon: isScanning