simulateConnectionStateChange method

void simulateConnectionStateChange(
  1. String deviceAddress,
  2. BleConnectionState state
)

Simulates a connection state change for testing purposes.

This method allows tests to simulate external connection state changes, such as a device connecting or disconnecting without the app initiating the connection. This is useful for testing the observeConnectionState() functionality.

The deviceAddress specifies which device's state to change, and state specifies the new connection state.

This method will:

  1. Update the internal connection state tracking
  2. Emit the new state to any active connection state streams

Implementation

void simulateConnectionStateChange(
  String deviceAddress,
  BleConnectionState state,
) {
  // Update internal state
  _connectionStates[deviceAddress] = state;

  // Emit to any listening streams
  final StreamController<BleConnectionState>? controller =
      _connectionControllers[deviceAddress];
  if (controller != null && !controller.isClosed) {
    controller.add(state);
  }
}