Classic Blue
A Flutter plugin for Bluetooth Classic serial communication on Android and iOS.
Features
- Check Bluetooth support and state.
- Discover nearby Bluetooth Classic devices.
- List paired devices.
- Connect and disconnect devices.
- Send and receive string or byte data.
- Listen to Bluetooth state, connection, discovery, and data streams.
Platform Support
| Platform | Support |
|---|---|
| Android | Yes |
| iOS | Yes |
Installation
Add the package to your pubspec.yaml:
dependencies:
classic_blue: ^1.0.0
Import it in Dart:
import 'package:classic_blue/classic_blue.dart';
Permissions
Android
Add the required permissions to android/app/src/main/AndroidManifest.xml:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Android 12+ -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
iOS
Add Bluetooth usage descriptions to ios/Runner/Info.plist:
<key>NSBluetoothAlwaysUsageDescription</key>
<string>This app needs Bluetooth access to communicate with devices.</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>This app needs Bluetooth access to communicate with devices.</string>
Basic Usage
import 'package:classic_blue/classic_blue.dart';
final classicBlue = ClassicBlue();
Future<void> connectAndSend() async {
final supported = await classicBlue.isSupported();
if (!supported) {
return;
}
final enabled = await classicBlue.isEnabled();
if (!enabled) {
await classicBlue.enable();
}
final devices = await classicBlue.getPairedDevices();
if (devices.isEmpty) {
return;
}
await classicBlue.connect(devices.first.address);
await classicBlue.sendString('Hello Bluetooth');
classicBlue.onDataReceived.listen((data) {
final message = data.asString();
print('Received: $message');
});
}
Example
An example app is available in the example/ directory:
cd example
flutter run
Notes
- Bluetooth Classic behavior can vary by device and platform.
- iOS Bluetooth Classic support is limited by Apple's platform restrictions and accessory requirements.
- Android device discovery may require location permission, depending on the Android version.