bt_classic
호스트/서버 기능, 텍스트 메시징 및 파일 전송 기능을 갖춘 블루투스 클래식 통신을 위한 플러터 플러그인입니다.
ArshiaMojtahedi/bt_classic open-source repository details.
아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.
A Flutter plugin for Bluetooth Classic communication with host/server functionality, text messaging, and file transfer capabilities.
| Platform | Support |
|---|---|
| Android | ✅ |
| iOS | ❌ |
| Web | ❌ |
| Windows | ❌ |
| macOS | ❌ |
| Linux | ❌ |
Add this to your package's pubspec.yaml file:
dependencies:
bt_classic: ^1.0.0
The plugin automatically handles permission requests, but you need to declare them in your android/app/src/main/AndroidManifest.xml:
<!-- Bluetooth permissions for API level 30 and below -->
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<!-- Bluetooth permissions for API level 31 and above -->
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation" />
<!-- Location permissions for device discovery -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Bluetooth hardware feature -->
<uses-feature
android:name="android.hardware.bluetooth"
android:required="true" />
Set the minimum SDK version to 21 in android/app/build.gradle:
android {
defaultConfig {
minSdkVersion 21
}
}
import 'package:bt_classic/bt_classic.dart';
class BluetoothClientExample extends StatefulWidget {
@override
_BluetoothClientExampleState createState() => _BluetoothClientExampleState();
}
class _BluetoothClientExampleState extends State<BluetoothClientExample> {
late BluetoothClientService _clientService;
List<BluetoothDevice> _devices = [];
bool _isConnected = false;
@override
void initState() {
super.initState();
_initializeClient();
}
void _initializeClient() {
_clientService = BluetoothClientService();
// Set up callbacks
_clientService.onDeviceFound = (device) {
setState(() {
_devices.add(device);
});
};
_clientService.onConnected = (address) {
setState(() {
_isConnected = true;
});
print('Connected to $address');
};
_clientService.onDisconnected = () {
setState(() {
_isConnected = false;
});
print('Disconnected');
};
_clientService.onMessageReceived = (message) {
print('Received message: $message');
};
_clientService.onFileReceived = (fileName, fileData) {
print('Received file: $fileName (${fileData.length} bytes)');
};
_clientService.onError = (error) {
print('Error: $error');
};
}
Future<void> _requestPermissions() async {
final granted = await _clientService.requestPermissions();
if (!granted) {
print('Permissions denied');
}
}
Future<void> _startDiscovery() async {
_devices.clear();
final started = await _clientService.startDiscovery();
if (started) {
print('Discovery started');
}
}
Future<void> _connectToDevice(String address) async {
final connected = await _clientService.connectToDevice(address);
if (!connected) {
print('Failed to connect');
}
}
Future<void> _sendMessage(String message) async {
if (_isConnected) {
final sent = await _clientService.sendMessage(message);
if (sent) {
print('Message sent: $message');
}
}
}
Future<void> _sendFile(Uint8List fileData, String fileName) async {
if (_isConnected) {
final sent = await _clientService.sendFile(fileData, fileName);
if (sent) {
print('File sent: $fileName');
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Bluetooth Client')),
body: Column(
children: [
ElevatedButton(
onPressed: _requestPermissions,
child: Text('Request Permissions'),
),
ElevatedButton(
onPressed: _startDiscovery,
child: Text('Start Discovery'),
),
Expanded(
child: ListView.builder(
itemCount: _devices.length,
itemBuilder: (context, index) {
final device = _devices[index];
return ListTile(
title: Text(device.name),
subtitle: Text(device.address),
onTap: () => _connectToDevice(device.address),
);
},
),
),
if (_isConnected)
ElevatedButton(
onPressed: () => _sendMessage('Hello from client!'),
child: Text('Send Message'),
),
],
),
);
}
}
class BluetoothHostExample extends StatefulWidget {
@override
_BluetoothHostExampleState createState() => _BluetoothHostExampleState();
}
class _BluetoothHostExampleState extends State<BluetoothHostExample> {
late BluetoothHostService _hostService;
bool _isServerRunning = false;
bool _isClientConnected = false;
String _connectedClientAddress = '';
@override
void initState() {
super.initState();
_initializeHost();
}
void _initializeHost() {
_hostService = BluetoothHostService();
// Set up callbacks
_hostService.onClientConnected = (address) {
setState(() {
_isClientConnected = true;
_connectedClientAddress = address;
});
print('Client connected: $address');
};
_hostService.onClientDisconnected = () {
setState(() {
_isClientConnected = false;
_connectedClientAddress = '';
});
print('Client disconnected');
};
_hostService.onMessageReceived = (message) {
print('Received message: $message');
};
_hostService.onFileReceived = (fileName, fileData) {
print('Received file: $fileName (${fileData.length} bytes)');
};
_hostService.onError = (error) {
print('Error: $error');
};
}
Future<void> _requestPermissions() async {
final granted = await _hostService.requestPermissions();
if (!granted) {
print('Permissions denied');
}
}
Future<void> _makeDiscoverable() async {
final success = await _hostService.makeDiscoverable();
if (success) {
print('Device is now discoverable');
}
}
Future<void> _startServer() async {
final started = await _hostService.startServer();
if (started) {
setState(() {
_isServerRunning = true;
});
print('Server started');
}
}
Future<void> _stopServer() async {
final stopped = await _hostService.stopServer();
if (stopped) {
setState(() {
_isServerRunning = false;
_isClientConnected = false;
});
print('Server stopped');
}
}
Future<void> _sendMessage(String message) async {
if (_isClientConnected) {
final sent = await _hostService.sendMessage(message);
if (sent) {
print('Message sent: $message');
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Bluetooth Host')),
body: Column(
children: [
ElevatedButton(
onPressed: _requestPermissions,
child: Text('Request Permissions'),
),
ElevatedButton(
onPressed: _makeDiscoverable,
child: Text('Make Discoverable'),
),
ElevatedButton(
onPressed: _isServerRunning ? _stopServer : _startServer,
child: Text(_isServerRunning ? 'Stop Server' : 'Start Server'),
),
Text('Server Running: $_isServerRunning'),
Text('Client Connected: $_isClientConnected'),
if (_isClientConnected)
Text('Connected Client: $_connectedClientAddress'),
if (_isClientConnected)
ElevatedButton(
onPressed: () => _sendMessage('Hello from host!'),
child: Text('Send Message'),
),
],
),
);
}
}
Represents a Bluetooth device with name and address.
class BluetoothDevice {
final String name;
final String address;
const BluetoothDevice({
required this.name,
required this.address,
});
}
Service for Bluetooth client functionality.
Future<bool> requestPermissions() - Request Bluetooth permissionsFuture<bool> isBluetoothEnabled() - Check if Bluetooth is enabledFuture<bool> startDiscovery() - Start discovering nearby devicesFuture<bool> stopDiscovery() - Stop device discoveryFuture<List<BluetoothDevice>> getPairedDevices() - Get paired devicesFuture<bool> connectToDevice(String address) - Connect to a deviceFuture<bool> sendMessage(String message) - Send a text messageFuture<bool> sendFile(Uint8List fileData, String fileName) - Send a fileFuture<bool> disconnect() - Disconnect from deviceFuture<bool> isConnected() - Check connection statusFunction(BluetoothDevice)? onDeviceFound - Called when a device is discoveredFunction()? onDiscoveryFinished - Called when discovery finishesFunction(String)? onConnected - Called when connected to a deviceFunction()? onDisconnected - Called when disconnectedFunction(String)? onMessageReceived - Called when a message is receivedFunction(String, Uint8List)? onFileReceived - Called when a file is receivedFunction(String)? onError - Called when an error occursService for Bluetooth host/server functionality.
Future<bool> requestPermissions() - Request Bluetooth permissionsFuture<bool> isBluetoothEnabled() - Check if Bluetooth is enabledFuture<bool> makeDiscoverable() - Make device discoverableFuture<bool> startServer() - Start the Bluetooth serverFuture<bool> stopServer() - Stop the Bluetooth serverFuture<bool> isServerRunning() - Check if server is runningFuture<String> getDeviceName() - Get device nameFuture<bool> sendMessage(String message) - Send a text messageFuture<bool> sendFile(Uint8List fileData, String fileName) - Send a fileFuture<bool> disconnect() - Disconnect from clientFuture<bool> isConnected() - Check connection statusFunction(String)? onClientConnected - Called when a client connectsFunction()? onClientDisconnected - Called when client disconnectsFunction(String)? onMessageReceived - Called when a message is receivedFunction(String, Uint8List)? onFileReceived - Called when a file is receivedFunction(String)? onError - Called when an error occursThe package includes a comprehensive example app that demonstrates both client and host functionality. To run the example:
cd example
flutter run
The example app features:
Permissions not granted: Make sure to call requestPermissions() before using other methods.
Connection fails: Ensure both devices have Bluetooth enabled and are within range.
Discovery doesn't find devices: Check that location permissions are granted and the target device is discoverable.
File transfer fails: Large files may take time to transfer. The plugin automatically handles Base64 encoding.
Arshia Motjahedi
Contributions are welcome! Please feel free to submit a Pull Request at https://github.com/arshiamoj/bt_classic.
This project is licensed under the MIT License - see the LICENSE file for details.
의존성 목록 4 개
{"sdk":"flutter"}^2.0.2{"sdk":"flutter"}^3.0.0사용 목적에 따라 검색하고 개발 활동을 비교한 뒤 원본 저장소에서 요구 사항을 확인하세요.
“A repository is the most basic element of GitHub.”
GitHub Docs — About repositories ↗