Flutter 3.47 鸿蒙三方库适配实战:从 0 到 1 处理 print_bluetooth_thermal
小票打印是零售、餐饮类应用的刚需。print_bluetooth_thermal 通过经典蓝牙 SPP 把 ESC/POS 字节流写给热敏打印机。
本文以 print_bluetooth_thermal 1.2.5 为例,记录我如何保留原有 Dart API、补齐 Flutter OH 插件入口、在鸿蒙端接入真实系统能力,并在 HarmonyOS API 26 真机上完成构建、安装、启动和按钮调用验证。
适配仓库: print_bluetooth_thermal
适配分支: feat/ohos_print_bluetooth_thermal_1.2.5
适配 TAG: 1.2.5-ohos.1
一、先看最终效果
*图 1:HarmonyOS API 26 真机启动 Demo。页面显示 `print_bluetooth_thermal 1.2.5`。*
*图 2:点击“查询蓝牙开关”后的真实返回:`bluetoothEnabled → true`。*
*图 3:点击“列出已配对打印机”后的真实返回:`pairedBluetooths → 0 个`。*
| 验证点 | 实测结果 |
|---|---|
| Flutter OH 3.47 工程构建 | 通过,产出 entry-default-signed.hap |
| 安装、启动(真机) | 通过 |
| 查询蓝牙开关 | bluetoothEnabled → true |
| 列出已配对打印机 | pairedBluetooths → 0 个 |
二、这个库原来是怎么工作的
一条 MethodChannel,方法名全小写:
MethodChannel('groons.web.app/print')
-> ispermissionbluetoothgranted / bluetoothEnabled
-> pairedbluetooths / connectionstatus
-> connect(mac) / writebytes(bytes) / printstring(text) / disconnect
三、实际环境
Flutter 鸿蒙环境搭建不在本文重复展开,直接参考 Flutter OH 环境搭建指南。
| 组件 | 版本 |
|---|---|
| Flutter OH | 3.47.4-ohos-1.0.4 |
| Dart | 3.13.3 |
| DevEco Studio | 26.0.0 Release |
| HarmonyOS SDK | API 26 |
| 测试设备 | 华为畅享 90 Pro Max,HarmonyOS 7.0.0.105(API 26) |
| 目标库 | print_bluetooth_thermal 1.2.5 |
flutter --version
hdc list targets
四、确认上游版本并同步到 AtomGit
上游包地址是 https://github.com/groons-dev/print_bluetooth_thermal,版本 1.2.5 使用 MIT License。适配前我用 AtomGit API(GET /api/v5/repos/<org>/<lib>)在 oh-flutter、hxa-flutter、CPF-Flutter 三个组织做了排重,未发现同名 OHOS 实现。
仓库适配提交为 6c24b3d,分支与 TAG 1.2.5-ohos.1 均指向该提交。
git clone https://atomgit.com/oh-flutter/print_bluetooth_thermal.git
cd print_bluetooth_thermal
git switch feat/ohos_print_bluetooth_thermal_1.2.5
git log --oneline -5
五、创建适配分支并检查工程结构
git switch -c feat/ohos_print_bluetooth_thermal_1.2.5
关键目录如下:
print_bluetooth_thermal/
├── lib/ # 原有 Dart API 和通道
├── ohos/
│ ├── index.ets # OHOS 插件导出入口
│ └── src/main/ets/components/plugin/
│ └── PrintBluetoothThermalPlugin.ets # 通道实现
├── example/lib/main.dart # 真机验证 Demo
├── example/ohos/ # Flutter OH 宿主工程
├── pubspec.yaml # 声明 ohos 平台
└── README.OpenHarmony_CN.md # 鸿蒙适配说明
flutter:
plugin:
platforms:
ohos:
pluginClass: PrintBluetoothThermalPlugin
pluginClass 必须和 ohos/index.ets 导出的类名一致。这一步在所有库里都踩过:名字对不上时 Dart 能编译,运行时却抛 MissingPluginException,没有任何构建期提示。
六、适配过程:注册通道并对齐语义
通道名必须与上游 Dart 源码逐字一致。本库的映射如下:
| 方法 | OpenHarmony 实现 | 说明 |
|---|---|---|
bluetoothEnabled | bluetooth.access.getState() | 真实蓝牙开关 |
pairedBluetooths | connection.getPairedDevices() + getRemoteDeviceName | 返回 name#mac 列表 |
connect | socket.sppConnect(uuid 00001101, RFCOMM) | 建立 SPP 串口 |
writeBytes / printString | socket.sppWrite | 真实写字节流 |
核心代码:
private pairedDevices(): Array<string> {
const out: Array<string> = [];
if (!this.isBluetoothOn()) {
return out;
}
for (const id of connection.getPairedDevices()) {
const name = connection.getRemoteDeviceName(id) ?? id;
out.push(`${name}#${id}`); // 上游按 name#mac 解析
}
return out;
}
七、构建配置与签名
示例工程的目标 SDK 对齐到 26,与 @ohos/flutter_ohos 的要求一致:
{
"name": "default",
"compatibleSdkVersion": "26.0.0",
"targetSdkVersion": "26.0.0",
"runtimeOS": "HarmonyOS"
}
Demo 的 AppScope/app.json5 统一成 com.example.flutter_oh_demo。签名 profile 里绑定的 bundleName 必须与它一致,否则 SignHap 直接报 00303074 Configuration Error。证书、p12、p7b 和密码不提交到仓库。
八、通过 AtomGit 引入适配版
在业务工程中使用时锁定 TAG:
dependencies:
print_bluetooth_thermal:
git:
url: https://atomgit.com/oh-flutter/print_bluetooth_thermal.git
ref: 1.2.5-ohos.1
flutter pub get
然后在 pubspec.lock 里确认 source: git、AtomGit 地址和 resolved-ref,避免解析到 pub.dev 的同名版本。
九、构建、安装和真机验证
cd example
flutter pub get
flutter build hap --debug
hdc install -r build/ohos/hap/entry-default-signed.hap
hdc shell aa start -a EntryAbility -b com.example.flutter_oh_demo
构建输出为 ✓ Built build/ohos/hap/entry-default-signed.hap.,随后 hdc 报告安装成功。所有示例共用同一个 bundleName,重装后要先 aa force-stop 再 aa start,否则系统只是把旧任务拉到前台,看到的是上一次安装的界面。
十、适配结果和当前边界
鸿蒙和 Android 一样,SPP 只能连已配对设备。未配对时 connect 会报错,需要用户先去系统设置配对——这是平台限制。
十一、FAQ
Q1:为什么方法名是全小写?
- 原因: 上游 Dart 源码里写的是 invokeMethod(‘bluetoothenabled’),骨架阶段按驼峰实现会命不中。
- 验证结果: 改成全小写后 bluetoothEnabled → true。
Q2:为什么配对列表是 0 个?
- 原因: 当前设备没有配对的打印机,这是真实结果。
- 验证结果: 不伪造设备列表。
十二、总结
print_bluetooth_thermal 1.2.5 在鸿蒙上已经接入真实系统能力,Dart 侧 API 与上游保持一致。本文的验证结果全部来自 HarmonyOS API 26 真机按钮点击,没有占位返回值;平台本身不具备的能力在正文和 README 中都已明确标注为边界。
十三、参考资料和仓库地址
- 适配仓库主页
- 适配分支
- 适配 TAG
- 最终提交 6c24b3d
- ArkTS 通道实现
- 鸿蒙适配说明
- pub.dev 包页面
- pub.dev 1.2.5 版本页
- 上游仓库
- Flutter 官方平台通道文档
- Flutter OH 鸿蒙 SDK 仓库
- HarmonyOS 开发者官方文档
欢迎加入CPF-Flutter 鸿蒙社区:https://atomgit.com/CPF-Flutter
更多推荐

所有评论(0)