fix(#49): attach to radios already connected by another app (BLE systemDevices)

Query FlutterBluePlus.systemDevices on every native platform (was Linux-only)
so a radio already connected to another central -- e.g. Liam's MeshCore app --
appears in the picker and we attach to the existing link. A connected BLE radio
stops advertising, so a scan alone never finds it.
pull/52/head
Strycher 1 month ago
parent a08e6598cb
commit efab80c5d0

@ -143,7 +143,7 @@ class MeshCoreConnector extends ChangeNotifier {
MeshCoreTransportType _activeTransport = MeshCoreTransportType.bluetooth; MeshCoreTransportType _activeTransport = MeshCoreTransportType.bluetooth;
final List<ScanResult> _scanResults = []; final List<ScanResult> _scanResults = [];
final List<ScanResult> _linuxSystemScanResults = []; final List<ScanResult> _systemScanResults = [];
final List<Contact> _contacts = []; final List<Contact> _contacts = [];
final List<Contact> _discoveredContacts = []; final List<Contact> _discoveredContacts = [];
final List<Channel> _channels = []; final List<Channel> _channels = [];
@ -1362,7 +1362,7 @@ class MeshCoreConnector extends ChangeNotifier {
if (_state == MeshCoreConnectionState.scanning) return; if (_state == MeshCoreConnectionState.scanning) return;
_scanResults.clear(); _scanResults.clear();
_linuxSystemScanResults.clear(); _systemScanResults.clear();
_setState(MeshCoreConnectionState.scanning); _setState(MeshCoreConnectionState.scanning);
// Ensure any previous scan is fully stopped. Guard with isScanningNow to // Ensure any previous scan is fully stopped. Guard with isScanningNow to
@ -1401,15 +1401,19 @@ class MeshCoreConnector extends ChangeNotifier {
await Future.delayed(const Duration(milliseconds: 300)); await Future.delayed(const Duration(milliseconds: 300));
} }
if (PlatformInfo.isLinux) { // Surface radios already connected to the OS by us or by another app such
await _loadLinuxSystemDevicesForScan(); // as MeshCore so we can attach to them. A connected BLE radio stops
// advertising, so a scan alone never finds it; systemDevices does. Supported
// on every native platform (Android/iOS/macOS/Linux), not web.
if (!PlatformInfo.isWeb) {
await _loadSystemDevicesForScan();
} }
_scanSubscription = FlutterBluePlus.scanResults.listen((results) { _scanSubscription = FlutterBluePlus.scanResults.listen((results) {
_scanResults _scanResults
..clear() ..clear()
..addAll(results); ..addAll(results);
_mergeLinuxSystemScanResults(); _mergeSystemScanResults();
notifyListeners(); notifyListeners();
}); });
@ -1430,12 +1434,12 @@ class MeshCoreConnector extends ChangeNotifier {
await stopScan(); await stopScan();
} }
Future<void> _loadLinuxSystemDevicesForScan() async { Future<void> _loadSystemDevicesForScan() async {
try { try {
final systemDevices = await FlutterBluePlus.systemDevices([ final systemDevices = await FlutterBluePlus.systemDevices([
Guid(MeshCoreUuids.service), Guid(MeshCoreUuids.service),
]); ]);
_linuxSystemScanResults _systemScanResults
..clear() ..clear()
..addAll( ..addAll(
systemDevices systemDevices
@ -1461,24 +1465,24 @@ class MeshCoreConnector extends ChangeNotifier {
), ),
), ),
); );
_mergeLinuxSystemScanResults(); _mergeSystemScanResults();
notifyListeners(); notifyListeners();
} catch (error) { } catch (error) {
_appDebugLogService?.warn( _appDebugLogService?.warn(
'Failed loading Linux paired/system BLE devices: $error', 'Failed loading system BLE devices: $error',
tag: 'BLE Scan', tag: 'BLE Scan',
); );
} }
} }
void _mergeLinuxSystemScanResults() { void _mergeSystemScanResults() {
if (!PlatformInfo.isLinux || _linuxSystemScanResults.isEmpty) { if (_systemScanResults.isEmpty) {
return; return;
} }
final existingIds = _scanResults final existingIds = _scanResults
.map((result) => result.device.remoteId.str) .map((result) => result.device.remoteId.str)
.toSet(); .toSet();
for (final result in _linuxSystemScanResults) { for (final result in _systemScanResults) {
if (existingIds.contains(result.device.remoteId.str)) { if (existingIds.contains(result.device.remoteId.str)) {
continue; continue;
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.