安装
安装
npm install @rdlabo/capacitor-brotherprint
发布的插件包通过 SPM 依赖位于应用根目录下的本地 Brother SDK:
ios/LocalPackages/BRLMPrinterKit
此路径相对于 node_modules/@rdlabo/capacitor-brotherprint,即 ../../../ios/LocalPackages/BRLMPrinterKit。请按下图将 Brother iOS SDK 放入 Capacitor 应用的 ios 目录,再运行 npx cap sync。此插件要求 iOS 16,仅支持 Swift Package Manager,无需配置 CocoaPods 或 Podfile。
此插件不包含 Brother SDK。请从 Brother 官网下载适用于目标平台的 SDK。
初始化 Brother SDK
Android 配置
- 将以下文件放入 Capacitor 项目的 android 目录:
android/BrotherPrintLibrary/BrotherPrintLibrary.aarandroid/BrotherPrintLibrary/build.gradle
从 Brother 下载 Android SDK:https://support.brother.co.jp/j/s/es/dev/ja/mobilesdk/android/index.html?c=jp&lang=ja&navi=offall&comple=on&redirect=on#ver4
- 在
android/BrotherPrintLibrary/build.gradle中加入:
configurations.maybeCreate("default")
artifacts.add("default", file('BrotherPrintLibrary.aar'))
- 打开
android/settings.gradle并添加:
include ':BrotherPrintLibrary'
project(':BrotherPrintLibrary').projectDir = new File('./BrotherPrintLibrary/')
iOS 配置
- 在 Capacitor 应用目录(位于
node_modules之外)中放置以下文件:
ios/LocalPackages/BRLMPrinterKit/Sources/BRLMPrinterKit.xcframeworkios/LocalPackages/BRLMPrinterKit/Package.swift
从 Brother 下载 iOS SDK:https://support.brother.com/g/s/es/dev/en/mobilesdk/ios/index.html
- 为该本地二进制包创建
ios/LocalPackages/BRLMPrinterKit/Package.swift(为了与插件匹配,最低支持 iOS 16):
// swift-tools-version: 5.9
import PackageDescription
let package = Package(
name: "BRLMPrinterKit",
platforms: [
.iOS(.v16)
],
products: [
.library(name: "BRLMPrinterKit", targets: ["BRLMPrinterKit"])
],
targets: [
.binaryTarget(
name: "BRLMPrinterKit",
path: "Sources/BRLMPrinterKit.xcframework"
)
]
)
- SDK 文件放置好后,运行
npx cap sync,使应用的 iOS 项目能够解析插件和本地包路径。
权限配置
Android 配置
在 AndroidManifest.xml 中添加以下权限:
- <manifest xmlns:android="http://schemas.android.com/apk/res/android">
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:tools="http://schemas.android.com/tools">
...
+ <!-- 用于蓝牙 -->
+ <uses-permission android:name="android.permission.BLUETOOTH" />
+ <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
+ <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
+ <!-- 用于 Android 11 及更早版本的低功耗蓝牙 -->
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
+ <!-- 用于 Android 12 及更高版本的低功耗蓝牙 -->
+ <uses-permission android:name="android.permission.BLUETOOTH_SCAN"
+ android:usesPermissionFlags="neverForLocation"
+ tools:targetApi="s" />
iOS 配置
在 Info.plist 中添加以下键。UISupportedExternalAccessoryProtocols 的值必须是字符串数组。
+ <key>NSBluetoothAlwaysUsageDescription</key>
+ <string>【Why use Bluetooth for your app.】</string>
+ <key>NSBluetoothPeripheralUsageDescription</key>
+ <string>【Why use Bluetooth for your app.】</string>
+ <key>NSBonjourServices</key>
+ <array>
+ <string>_pdl-datastream._tcp</string>
+ <string>_printer._tcp</string>
+ <string>_ipp._tcp</string>
+ </array>
+ <key>NSLocalNetworkUsageDescription</key>
+ <string>【Why use WiFi for your app.】</string>
+ <key>UISupportedExternalAccessoryProtocols</key>
+ <array>
+ <string>com.brother.ptcbp</string>
+ </array>
蓝牙相关 plist 字段的类型(已于 2026 年 9 月 9 日验证)
即使只使用 com.brother.ptcbp 一种协议,UISupportedExternalAccessoryProtocols 也必须是字符串数组。此仓库早期的示例误用了单个 <string>,导致蓝牙搜索崩溃并报错 -[__NSCFString count]: unrecognized selector。请按上例使用 <array>。参见 Apple 的类型定义。
NSBluetoothAlwaysUsageDescription 和 NSBluetoothPeripheralUsageDescription 的值均为字符串,并非数组。截至 2026 年 9 月 9 日,Brother 官方 iOS 安装指南 中这两个键的示例均正确,与此次崩溃无关。该指南另有说明:将 com.brother.ptcbp 添加为 UISupportedExternalAccessoryProtocols 的一个数组成员。只有部署目标早于 iOS 13 时,才需要额外声明 NSBluetoothPeripheralUsageDescription。