flutter_classic_bluetooth library

Flutter Classic Bluetooth: Bluetooth Classic (RFCOMM) communication plugin.

Provides a unified Dart API for discovering, pairing, and communicating with Bluetooth Classic devices over RFCOMM across Android, iOS (MFi only), Windows, macOS, and Linux.

Getting Started

import 'package:flutter_classic_bluetooth/flutter_classic_bluetooth.dart';

final bluetooth = FlutterClassicBluetooth();

Core API

Class Purpose
FlutterClassicBluetooth Main entry point: adapter, discovery, pairing, connection, server
BtcDevice Represents a remote Bluetooth device
BtcConnection Active RFCOMM connection with input/output streams
BtcReconnectingConnection Self-healing connection that auto-reconnects
BtcReconnectPolicy Backoff/retry settings for auto-reconnect
BtcServerSocket Listens for incoming RFCOMM connections
BtcStreamSink Ordered write sink for a connection
BtcFrameSplitter Splits input into delimited frames (.lines() helper)
BtcPlatformCapabilities Platform feature support matrix

Enums

Enum Purpose
BtcAdapterState Adapter on/off/transitioning states
BtcBondState Device pairing state
BtcDeviceType Classic, LE, or Dual-mode
BtcConnectionState Connection lifecycle states
BtcReconnectState Auto-reconnect link states
BtcPermissionStatus Whether the app holds the required permissions

Exceptions

All exceptions extend BtcException:

Exception When
BtcUnsupportedException Feature not available on platform
BtcPermissionException Permission denied
BtcDisabledException Adapter is off
BtcConnectionException Connection failed
BtcWriteException Write failed
BtcDiscoveryException Discovery failed to start
BtcTimeoutException Operation timed out
BtcAddressException Invalid MAC address
BtcUuidException Invalid UUID

Platform Support

Feature Android iOS Windows macOS Linux
Permissions Yes Yes n/a(5) n/a(5) n/a(5)
Adapter state Yes Yes Yes Yes Yes
Discovery Yes No Yes Yes Yes
Paired devices Yes Yes(1) Yes Yes Yes
Bond Yes No Yes(2) Yes(3) No
Unbond Yes No No(4) Yes(3) No
RFCOMM connect Yes Yes(1) Yes Yes Yes
RFCOMM server Yes No Yes Yes Yes
Enable/Disable Yes No No No Yes(3)
Set discoverable Yes No No No Yes

(1) iOS requires MFi-certified accessories via ExternalAccessory framework. (2) macOS pairs via IOBluetoothDevicePair and may show a system prompt. (3) Linux uses the BlueZ D-Bus API (org.bluez); pairing a PIN/passkey device needs a system pairing agent. (4) macOS has no public API to remove a pairing; unpair via System Settings. (5) Windows, macOS and Linux grant Bluetooth access at build time, through a manifest entry, an entitlement or the system's D-Bus policy, so checkPermissions reports notRequired and there is nothing to ask for.

Example

// Ask for permissions at a moment the user expects it
if (await bluetooth.checkPermissions() == BtcPermissionStatus.denied) {
  await bluetooth.requestPermissions();
}

// Discover and connect
final caps = await bluetooth.getPlatformCapabilities();
if (caps.canDiscoverDevices) {
  bluetooth.discoveryResults.listen((device) {
    print('Found: ${device.displayName}');
  });
  await bluetooth.startDiscovery();
}

// Connect to a device
final conn = await bluetooth.connect(
  address: 'AA:BB:CC:DD:EE:FF',
  uuid: '00001101-0000-1000-8000-00805F9B34FB',
);
conn.input.listen((data) => print('Received: $data'));
await conn.output.add(Uint8List.fromList([0x01, 0x02]));
await conn.finish();

Classes

BtcConnection Models
An active RFCOMM connection to a remote Bluetooth device.
BtcDevice Models
Represents a Bluetooth Classic remote device.
BtcFrameSplitter Models
Splits a byte stream into frames separated by a delimiter, buffering across chunk boundaries so a frame may span several reads. The delimiter is stripped from the emitted frames.
BtcLengthFrameSplitter Models
Splits a byte stream into frames that each begin with their own length, buffering across chunk boundaries so a frame may span several reads. The length prefix is stripped from the emitted frames.
BtcPlatformCapabilities Models
Reports the Bluetooth Classic capabilities of the current platform.
BtcReconnectingConnection Models
A self-healing RFCOMM connection that transparently reconnects when the link drops, using the exponential backoff described by its policy.
BtcReconnectPolicy Models
Controls how a BtcReconnectingConnection retries after the link drops.
BtcServerSocket Models
A server socket that listens for incoming Bluetooth RFCOMM connections.
BtcStreamSink Models
A write sink for an active Bluetooth connection.
BtcUuid Core
Well-known Bluetooth Classic service UUIDs.
FlutterClassicBluetooth Core
Primary API for Bluetooth Classic operations.
FlutterClassicBluetoothPlatform Platform
The interface that platform-specific implementations must implement.
MethodChannelFlutterClassicBluetooth Platform
An implementation of FlutterClassicBluetoothPlatform that uses method channels and event channels to communicate with native code.

Enums

BtcAdapterState Enums
State of the Bluetooth adapter.
BtcBondState Enums
Bond/pairing state of a remote Bluetooth device.
BtcConnectFailure Exceptions
Why a connection attempt failed.
BtcConnectionState Enums
Connection state of an active RFCOMM connection.
BtcDeviceType Enums
Type classification of a Bluetooth device.
BtcPermission Enums
A Bluetooth capability that may need its own permission.
BtcPermissionStatus Enums
Whether the app holds the Bluetooth permissions the platform requires.
BtcReconnectState Enums
High-level state of a BtcReconnectingConnection.

Extensions

BtcByteStreamReader on Stream<Uint8List> Models
Serial-friendly reading helpers on a byte stream such as BtcConnection.input or BtcReconnectingConnection.input.

Exceptions / Errors

BtcAddressException Exceptions
Thrown when an invalid Bluetooth MAC address is provided.
BtcConnectionException Exceptions
Thrown when a connection attempt fails.
BtcDisabledException Exceptions
Thrown when an operation requires Bluetooth to be enabled but it is off.
BtcDiscoveryException Exceptions
Thrown when starting or running device discovery fails.
BtcException Exceptions
Base exception for all Bluetooth Classic operations.
BtcPermissionException Exceptions
Thrown when a required Bluetooth permission is denied.
BtcTimeoutException Exceptions
Thrown when an operation times out.
BtcUnsupportedException Exceptions
Thrown when a feature is not available on the current platform.
BtcUuidException Exceptions
Thrown when an invalid UUID is provided.
BtcWriteException Exceptions
Thrown when a write operation to a connected device fails.