streamBleScanResult method
Provides a broadcast stream emitting lists of BleDiscoveredDevice during a BLE scan.
Each event is a list of devices found or updated in the latest scan interval. Being a broadcast stream, it supports multiple listeners simultaneously. Each listener will receive events emitted after it subscribes.
Returns a Stream of List<BleDiscoveredDevice>
.
Implementation
@override
Stream<List<BleDiscoveredDevice>> streamBleScanResult() {
_bleScanResultStream ??=
bleScanResultEventChannel.receiveBroadcastStream().map((dynamic event) {
// Expecting a List from the platform channel
if (event is List) {
try {
return event
.whereType<Map>() // Filter out any non-map elements defensively
.map((deviceMap) =>
BleDiscoveredDevice.fromMap(Map.from(deviceMap)))
.toList();
} catch (e) {
debugPrint(
"[MethodChannelFlutterP2pConnection] Error parsing BleDiscoveredDevice list: $e, Event: $event");
rethrow;
}
} else {
debugPrint(
"[MethodChannelFlutterP2pConnection] Received non-list event on bleScanResultEventChannel: $event");
throw FormatException(
"Received unexpected data type on bleScanResultEventChannel: ${event.runtimeType}");
}
}).asBroadcastStream();
return _bleScanResultStream!;
}