flutter_bluetooth_serial
Flutter 经典蓝牙基础实现(目前仅支持 RFCOMM)。
特点
该项目的第一目标由 @edufolly 发起,是为串行端口协议 (HC-05 适配器) 创建一个接口。现在插件支持
-
适配器状态监控,
-
打开和关闭适配器,
-
打开设置,
-
发现设备(并请求可发现性),
-
列出已配对的设备并配对新设备,
-
同时连接到多个设备,
-
发送和接收数据(多连接)。
该插件(目前)使用串行端口配置文件通过 RFCOMM 传输数据,因此请确保运行了指向设备 SP/RFCOMM 通道的服务发现协议。最多可以有 7 个蓝牙连接。
目前仅支持 Android。
入门
取决于
# Add dependency to `pubspec.yaml` of your project.
dependencies:
# ...
flutter_bluetooth_serial: ^0.3.2
安装
# With pub manager
pub get
# or with Flutter
flutter pub get
导入
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
用法
您应该查看库的 Dart 代码(主要是已记录的函数)或示例代码。
// Some simplest connection :F
try {
BluetoothConnection connection = await BluetoothConnection.toAddress(address);
print('Connected to the device');
connection.input.listen((Uint8List data) {
print('Data incoming: ${ascii.decode(data)}');
connection.output.add(data); // Sending data
if (ascii.decode(data).contains('!')) {
connection.finish(); // Closing connection
print('Disconnecting by local host');
}
}).onDone(() {
print('Disconnected by remote request');
});
}
catch (exception) {
print('Cannot connect, exception occured');
}
注意:目前正在进行工作,以使通信比字节流操作更容易。有关该主题的讨论,请参见 #41。
示例
查看 示例应用程序,它同时连接了 Arduino HC-05 和 Raspberry Pi (RFCOMM) 蓝牙接口。
| 主屏幕和选项 | 发现和连接 | 与服务器的简单聊天 | 后台连接 |
|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
待办事项列表
- 添加一些实用工具以更轻松地管理
BluetoothConnection(参见讨论 #41), - 允许指定连接方法/协议/UUID,
- 监听/服务器模式,
- 识别并显示设备的
BluetoothClass, - 也许有一天可以与
flutter_blue集成?
您可能还想查看 里程碑。
鸣谢
0.3.0 版本之后,我们有了许多贡献者。如果您想获得署名,请给我发送一封 电子邮件。



