FakeCentralMethodChannel class

A fake implementation of CentralPlatformInterface designed for use in unit and widget tests.

This class simulates Bluetooth interactions without requiring actual hardware or platform channels. It allows developers to run tests on Flutter apps that use Bluetooth features without depending on physical BLE devices or native platform code.

Usage

To use in a test, override the singleton instance of CentralPlatformInterface:

setUp(() {
  CentralPlatformInterface.instance = FakeCentralMethodChannel();
});

You can then add fake devices and services to simulate Bluetooth interactions:

// Set the fake implementation before running tests
final FakeCentralMethodChannel fake = CentralPlatformInterface.instance as FakeCentralMethodChannel;

// Add a fake device to the scan results
fake.addFakeDevice(BleDevice(name: 'Test Device', address: '00:11:22:33:44:55', rssi: -60));

// Set fake services for the fake device
fake.setServices('00:11:22:33:44:55', [
  BleService(
    serviceUuid: '180D',
    characteristics: [
      BleCharacteristic(uuid: '2A37', address: '00:11:22:33:44:55'),
    ],
  ),
]);

// Set a mock read value for a specific characteristic
fake.setMockReadValue('2A37', utf8.encode('42'));
Inheritance

Constructors

FakeCentralMethodChannel()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

addFakeDevice(BleDevice device) → void
Adds a mock BLE device to the scan result pool.
checkBluetoothAdapterStatus() Future<BluetoothStatus>
Checks the status of the host device's Bluetooth adapter and returns a BluetoothStatus to communicate the current status of the adapter.
override
connect({required String deviceAddress}) Stream<BleConnectionState>
Initiates a connection to a BLE peripheral and returns a Stream representing the connection state.
override
disconnect(String deviceAddress) Future<void>
Terminates the connection to a BLE peripheral. Initiates a connection to a BLE peripheral and returns a Stream representing the connection state.
override
discoverServices(String deviceAddress) Stream<List<BleService>>
Triggers the service discovery process manually.
override
emitCurrentBluetoothStatus() Stream<BluetoothStatus>
Emits the current Bluetooth adapter status to the Dart side.
override
emitCurrentPermissionStatus() Stream<BluetoothPermissionStatus>
Emits the current Bluetooth permission status whenever it changes.
override
getConnectedDevices(List<String> serviceUUIDs) Future<List<ConnectedBleDevice>>
Returns a list of BLE device identifiers that are currently connected to the host device.
override
getCurrentConnectionState(String deviceAddress) Future<BleConnectionState>
Returns the current connection state for the Bluetooth device with the specified address.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
readCharacteristic({required BleCharacteristic characteristic, required Duration timeout}) Future<BleCharacteristicValue>
Reads the value of a specified Bluetooth characteristic.
override
requestBluetoothPermissions() Future<BluetoothPermissionStatus>
Requests Bluetooth permissions from the user.
override
setConnectionState(String deviceAddress, BleConnectionState state) → void
Sets the simulated connection state for a given BLE device.
setMockReadValue(String characteristicUuid, List<int> value) → void
Sets a mock read value for a specific characteristic UUID.
setServices(String deviceAddress, List<BleService> services) → void
Sets mock services for a given device address.
simulateConnectionStateUpdate(String deviceAddress, BleConnectionState state) → void
Simulates a connection state update by emitting a new value on the connection stream.
startScan({List<ScanFilter>? filters, ScanSettings? settings}) Stream<BleDevice>
Starts a scan for nearby BLE devices and returns a Stream of BleDevice instances representing the BLE devices that were discovered. On the Flutter side, listeners can be added to this stream so they can respond to Bluetooth devices being discovered, for example by presenting the list in the user interface or enabling controllers to find and connect to specific devices.
override
stopScan() Future<void>
Stops an ongoing Bluetooth scan or, if no scan is running, does nothing.
override
subscribeToCharacteristic(BleCharacteristic characteristic) Stream<BleCharacteristicValue>
Subscribes to a Bluetooth characteristic to listen for updates.
override
toString() String
A string representation of this object.
inherited
unsubscribeFromCharacteristic(BleCharacteristic characteristic) → void
Unsubscribes from a Bluetooth characteristic.
override
writeCharacteristic({required BleCharacteristic characteristic, required String value, int? writeType}) Future<void>
Writes data to a specified characteristic.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited