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 版本之后,我们有很多协作者。如果您想被提及,请给我发送一封 电子邮件。



