flutter_bluetooth_classic_serial 1.0.2
flutter_bluetooth_classic_serial: ^1.0.2 copied to clipboard
A Flutter plugin for Bluetooth Classic communication on Android, iOS, and Windows platforms. Supports device discovery, connection management, and data transmission.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_bluetooth_classic_serial/flutter_bluetooth_classic.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Bluetooth Classic Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const BluetoothClassicDemo(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
//
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
// action in the IDE, or press "p" in the console), to see the
// wireframe for each widget.
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('You have pushed the button this many times:'),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class BluetoothClassicDemo extends StatefulWidget {
const BluetoothClassicDemo({super.key});
@override
State<BluetoothClassicDemo> createState() => _BluetoothClassicDemoState();
}
class _BluetoothClassicDemoState extends State<BluetoothClassicDemo> {
late FlutterBluetoothClassic _bluetooth;
bool _isBluetoothAvailable = false;
BluetoothConnectionState? _connectionState;
List<BluetoothDevice> _pairedDevices = [];
BluetoothDevice? _connectedDevice;
String _receivedData = '';
final TextEditingController _messageController = TextEditingController();
@override
void initState() {
super.initState();
_bluetooth = FlutterBluetoothClassic();
_initBluetooth();
}
Future<void> _initBluetooth() async {
try {
bool isSupported = await _bluetooth.isBluetoothSupported();
bool isEnabled = await _bluetooth.isBluetoothEnabled();
setState(() {
_isBluetoothAvailable = isSupported && isEnabled;
});
if (isSupported && isEnabled) {
_loadPairedDevices();
_listenToConnectionState();
_listenToIncomingData();
}
} catch (e) {
debugPrint('Error initializing Bluetooth: $e');
}
}
Future<void> _loadPairedDevices() async {
try {
List<BluetoothDevice> devices = await _bluetooth.getPairedDevices();
setState(() {
_pairedDevices = devices;
});
} catch (e) {
debugPrint('Error loading paired devices: $e');
}
}
void _listenToConnectionState() {
_bluetooth.onConnectionChanged.listen((state) {
setState(() {
_connectionState = state;
if (state.isConnected) {
// Find the connected device
_connectedDevice = _pairedDevices.firstWhere(
(device) => device.address == state.deviceAddress,
orElse: () => BluetoothDevice(
name: 'Unknown Device',
address: state.deviceAddress,
paired: false,
),
);
} else {
_connectedDevice = null;
}
});
});
}
void _listenToIncomingData() {
_bluetooth.onDataReceived.listen((data) {
setState(() {
_receivedData += '${data.asString()}\n';
});
});
}
Future<void> _connectToDevice(BluetoothDevice device) async {
try {
await _bluetooth.connect(device.address);
} catch (e) {
debugPrint('Error connecting to device: $e');
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Failed to connect: $e')));
}
}
}
Future<void> _disconnect() async {
try {
await _bluetooth.disconnect();
} catch (e) {
debugPrint('Error disconnecting: $e');
}
}
Future<void> _sendMessage() async {
if (_messageController.text.isNotEmpty &&
_connectionState?.isConnected == true) {
try {
await _bluetooth.sendString(_messageController.text);
_messageController.clear();
} catch (e) {
debugPrint('Error sending message: $e');
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Failed to send message: $e')));
}
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Bluetooth Classic Demo'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Bluetooth Status
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Bluetooth Status',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
Row(
children: [
Icon(
_isBluetoothAvailable
? Icons.bluetooth
: Icons.bluetooth_disabled,
color: _isBluetoothAvailable
? Colors.blue
: Colors.grey,
),
const SizedBox(width: 8),
Text(
_isBluetoothAvailable ? 'Available' : 'Not Available',
),
],
),
const SizedBox(height: 8),
Row(
children: [
Icon(
_connectionState?.isConnected == true
? Icons.link
: Icons.link_off,
color: _connectionState?.isConnected == true
? Colors.green
: Colors.red,
),
const SizedBox(width: 8),
Text(_connectionState?.status ?? 'disconnected'),
],
),
if (_connectedDevice != null) ...[
const SizedBox(height: 8),
Text('Connected to: ${_connectedDevice!.name}'),
Text('Address: ${_connectedDevice!.address}'),
],
],
),
),
),
const SizedBox(height: 16),
// Paired Devices
Expanded(
flex: 2,
child: Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Paired Devices',
style: Theme.of(context).textTheme.titleMedium,
),
IconButton(
onPressed: _loadPairedDevices,
icon: const Icon(Icons.refresh),
),
],
),
const SizedBox(height: 8),
Expanded(
child: _pairedDevices.isEmpty
? const Center(
child: Text('No paired devices found'),
)
: ListView.builder(
itemCount: _pairedDevices.length,
itemBuilder: (context, index) {
final device = _pairedDevices[index];
final isConnected =
_connectedDevice?.address ==
device.address;
return ListTile(
leading: Icon(
Icons.devices,
color: isConnected ? Colors.green : null,
),
title: Text(device.name),
subtitle: Text(device.address),
trailing: isConnected
? ElevatedButton(
onPressed: _disconnect,
child: const Text('Disconnect'),
)
: ElevatedButton(
onPressed:
_connectionState?.isConnected !=
true
? () => _connectToDevice(device)
: null,
child: const Text('Connect'),
),
);
},
),
),
],
),
),
),
),
const SizedBox(height: 16), // Message Section
if (_connectionState?.isConnected == true) ...[
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Send Message',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
Row(
children: [
Expanded(
child: TextField(
controller: _messageController,
decoration: const InputDecoration(
hintText: 'Enter message to send',
border: OutlineInputBorder(),
),
),
),
const SizedBox(width: 8),
ElevatedButton(
onPressed: _sendMessage,
child: const Text('Send'),
),
],
),
],
),
),
),
const SizedBox(height: 16),
],
// Received Data
Expanded(
flex: 1,
child: Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Received Data',
style: Theme.of(context).textTheme.titleMedium,
),
IconButton(
onPressed: () {
setState(() {
_receivedData = '';
});
},
icon: const Icon(Icons.clear),
),
],
),
const SizedBox(height: 8),
Expanded(
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(4.0),
),
child: SingleChildScrollView(
child: Text(
_receivedData.isEmpty
? 'No data received'
: _receivedData,
style: const TextStyle(fontFamily: 'monospace'),
),
),
),
),
],
),
),
),
),
],
),
),
);
}
@override
void dispose() {
_messageController.dispose();
super.dispose();
}
}