streamBleConnectionState method

  1. @override
Stream<BleConnectionState> streamBleConnectionState()
override

Provides a broadcast stream of BleConnectionState updates.

This stream emits events when the connection status to a specific BLE device changes. Being a broadcast stream, it supports multiple listeners simultaneously. Each listener will receive events emitted after it subscribes.

Returns a Stream of BleConnectionState.

Implementation

@override
Stream<BleConnectionState> streamBleConnectionState() {
  _bleConnectionStateStream ??= bleConnectionStateEventChannel
      .receiveBroadcastStream()
      .map((dynamic event) {
    if (event is Map) {
      try {
        return BleConnectionState.fromMap(Map.from(event));
      } catch (e) {
        debugPrint(
            "[MethodChannelFlutterP2pConnection] Error parsing BleConnectionState: $e, Event: $event");
        rethrow;
      }
    } else {
      debugPrint(
          "[MethodChannelFlutterP2pConnection] Received non-map event on bleConnectionStateEventChannel: $event");
      throw FormatException(
          "Received unexpected data type on bleConnectionStateEventChannel: ${event.runtimeType}");
    }
  }).asBroadcastStream();
  return _bleConnectionStateStream!;
}