flutter_blue插件
时间: 2025-06-05 12:34:47 浏览: 16
### flutter_blue 插件的使用与相关信息
`flutter_blue` 是一个用于在 Flutter 应用中实现蓝牙通信的插件。以下是关于如何使用 `flutter_blue` 插件的相关信息,包括示例代码和常见问题解决方法。
#### 1. 安装与配置
在 `pubspec.yaml` 文件中添加以下依赖项:
```yaml
dependencies:
flutter_blue: ^0.8.0
```
然后运行以下命令以安装依赖项:
```bash
flutter pub get
```
如果遇到编译错误,例如 `Could not find protoc-3.11.4-osx-aarch_64.exe`,请确保已正确配置 Gradle 和 Maven 仓库[^1]。可以尝试更新 Gradle 版本或手动下载所需的 Protobuf 工具并将其路径添加到项目的构建配置中。
#### 2. 示例代码
以下是一个简单的示例,展示如何扫描附近的蓝牙设备并连接到指定设备:
```dart
import 'package:flutter/material.dart';
import 'package:flutter_blue/flutter_blue.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: BluetoothPage(),
);
}
}
class BluetoothPage extends StatefulWidget {
@override
_BluetoothPageState createState() => _BluetoothPageState();
}
class _BluetoothPageState extends State<BluetoothPage> {
final FlutterBlue flutterBlue = FlutterBlue.instance;
List<ScanResult> scanResults = [];
@override
void initState() {
super.initState();
startScan();
}
void startScan() async {
flutterBlue.startScan(timeout: Duration(seconds: 4));
flutterBlue.scanResults.listen((results) {
setState(() {
scanResults = results;
});
});
}
void stopScan() {
flutterBlue.stopScan();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Blue Example'),
actions: [
IconButton(
icon: Icon(Icons.refresh),
onPressed: () {
startScan();
},
),
],
),
body: ListView.builder(
itemCount: scanResults.length,
itemBuilder: (context, index) {
final result = scanResults[index];
return ListTile(
title: Text(result.device.name ?? "Unknown Device"),
subtitle: Text(result.device.id.toString()),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DevicePage(device: result.device),
),
);
},
);
},
),
);
}
}
class DevicePage extends StatefulWidget {
final BluetoothDevice device;
DevicePage({required this.device});
@override
_DevicePageState createState() => _DevicePageState();
}
class _DevicePageState extends State<DevicePage> {
List<BluetoothService> services = [];
@override
void initState() {
super.initState();
connectToDevice();
}
void connectToDevice() async {
await widget.device.connect();
services = await widget.device.discoverServices();
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.device.name ?? "Unknown Device"),
),
body: ListView.builder(
itemCount: services.length,
itemBuilder: (context, index) {
final service = services[index];
return ListTile(
title: Text(service.uuid.toString()),
);
},
),
);
}
}
```
#### 3. 常见问题及解决方法
- **编译失败**:如果出现类似 `Execution failed for task ':flutter_blue:generateDebugProto'` 的错误,可能是由于 Protobuf 工具未正确安装或找不到相关文件。请检查 Maven 仓库是否可用,并确保系统支持所需的操作系统架构。
- **蓝牙连接不稳定**:在某些情况下,蓝牙连接可能会因为信号不佳或其他原因中断。建议等待回调完成后再执行下一步操作[^2]。可以通过监听 `onDescriptorWrite` 回调来确认 GATT 操作已完成。
#### 4. 文档与资源
官方文档提供了详细的 API 参考和示例代码,用户可以参考以下链接获取更多信息:
- [flutter_blue GitHub 仓库](https://github.com/pauldemarco/flutter_blue)
- [Flutter 官方插件文档](https://pub.dev/packages/flutter_blue)
---
阅读全文
相关推荐



















