# uni-app-demo **Repository Path**: Walbert/uni-app-demo ## Basic Information - **Project Name**: uni-app-demo - **Description**: 用于app调试-集成原生SDK插件 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-02-26 - **Last Updated**: 2026-02-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # uniapp-uView-Base #### 介绍 基于 uView1.8.8 封装的一套基础模版,开箱即用,可直接开始快速开发业务需求~ ``` 想用uview2.0版本的去uview2的分支上下载 ``` # 打印机插件使用说明 ## 插件简介 这是一个 uniapp 原生打印机插件,支持 USB、网络、蓝牙、串口等多种连接方式,基于 POSPrinter SDK 开发。 ## 一、插件配置 ### 1. 复制插件文件 将以下文件复制到你的 uniapp 项目中: ``` nativeplugins/ └── Printer-PrinterModule/ ├── android/ │ ├── plugin_printer-release.aar │ └── printer-lib-3.5.3.aar └── package.json ``` ### 2. 创建 package.json 在 `nativeplugins/Printer-PrinterModule/` 目录下创建 `package.json`: ```json { "name": "Printer-PrinterModule", "id": "Printer-PrinterModule", "version": "1.0.0", "description": "打印机插件", "main": "index.js", "_dp_type": "nativeplugin", "_dp_nativeplugin": { "android": { "plugins": [ { "type": "module", "name": "Printer-PrinterModule", "class": "com.example.plugin.printer.PrinterModule" } ], "integrateType": "aar", "dependencies": [ "printer-lib-3.5.3.aar" ] } } } ``` ### 3. 在 manifest.json 中配置插件 打开项目的 `manifest.json`,在 `App` -> `nativePlugins` 中添加: ```json { "app-plus": { "nativePlugins": { "Printer-PrinterModule": { "package": "Printer-PrinterModule", "class": "com.example.plugin.printer.PrinterModule" } } } } ``` ### 4. 添加权限配置 在 `manifest.json` 的 `App` -> `permissions` 中添加必要权限: ```json { "app-plus": { "permissions": { "Bluetooth": { "description": "用于蓝牙打印机连接" }, "BluetoothAdmin": { "description": "用于蓝牙打印机管理" }, "Internet": { "description": "用于网络打印机连接" }, "AccessNetworkState": { "description": "用于检测网络状态" }, "AccessWifiState": { "description": "用于WiFi打印机连接" } } } } ``` ## 二、插件使用 ### 1. 引入插件 ```javascript const printerModule = uni.requireNativePlugin('Printer-PrinterModule'); ``` ### 2. 初始化打印机 SDK ```javascript printerModule.initPrinter({}, (res) => { if (res.success) { console.log('初始化成功'); } else { console.error('初始化失败:', res.message); } }); ``` ### 3. 连接打印机 #### USB 连接 ```javascript // 先获取 USB 设备列表 printerModule.getUsbDevices({}, (res) => { if (res.success && res.devices.length > 0) { // 连接第一个设备 printerModule.connectUSB({ pathName: res.devices[0] }, (result) => { if (result.success) { console.log('USB连接成功'); } else { console.error('USB连接失败:', result.message); } }); } }); ``` #### 网络连接 ```javascript printerModule.connectNet({ ipAddress: '192.168.1.100' }, (res) => { if (res.success) { console.log('网络连接成功'); } else { console.error('网络连接失败:', res.message); } }); ``` #### 蓝牙连接 ```javascript printerModule.connectBluetooth({ macAddress: '00:11:22:33:44:55' }, (res) => { if (res.success) { console.log('蓝牙连接成功'); } else { console.error('蓝牙连接失败:', res.message); } }); ``` #### 串口连接 ```javascript // 先获取串口列表 printerModule.getSerialPorts({}, (res) => { if (res.success && res.ports.length > 0) { // 连接第一个串口 printerModule.connectSerial({ port: res.ports[0], baudRate: '115200' }, (result) => { if (result.success) { console.log('串口连接成功'); } else { console.error('串口连接失败:', result.message); } }); } }); ``` ### 4. 打印功能 #### 打印文本 ```javascript printerModule.printText({ text: '欢迎使用打印机插件\n', alignment: 1, // 0:左对齐 1:居中 2:右对齐 fontSize: 0 // 0:正常 1:倍宽 2:倍高 3:双倍 }, (res) => { if (res.success) { console.log('打印成功'); } else { console.error('打印失败:', res.message); } }); ``` #### 打印简单文本 ```javascript printerModule.printString({ text: '这是一行简单文本\n' }, (res) => { console.log(res.message); }); ``` #### 打印条码 ```javascript printerModule.printBarcode({ content: '123456789012', type: 73, // 条码类型,73 为 CODE128 width: 2, // 宽度 height: 70, // 高度 alignment: 1 // 对齐方式 }, (res) => { console.log(res.message); }); ``` #### 打印二维码 ```javascript printerModule.printQRCode({ content: 'https://www.example.com', size: 5, // 二维码大小 alignment: 1 // 对齐方式 }, (res) => { console.log(res.message); }); ``` #### 打印图片 ```javascript // 图片需要转换为 Base64 编码 printerModule.printImage({ imageBase64: 'iVBORw0KGgoAAAANSUhEUgAA...', // Base64 编码的图片 width: 384, // 打印宽度 alignment: 1 // 对齐方式 }, (res) => { console.log(res.message); }); ``` ### 5. 其他功能 #### 走纸 ```javascript printerModule.feedLine({ lines: 3 // 走纸行数 }, (res) => { console.log(res.message); }); ``` #### 切纸 ```javascript printerModule.cutPaper({ feedLines: 1 // 切纸前走纸行数 }, (res) => { console.log(res.message); }); ``` #### 开钱箱 ```javascript printerModule.openCashBox({ pin: 2 // 引脚,默认为 2 }, (res) => { console.log(res.message); }); ``` #### 断开连接 ```javascript printerModule.disconnect({}, (res) => { console.log(res.message); }); ``` #### 检查连接状态 ```javascript printerModule.isConnected({}, (res) => { console.log('连接状态:', res.connected); }); ``` ## 三、完整示例 ```vue ``` ## 四、注意事项 1. **权限申请**:使用蓝牙连接时,需要在运行时动态申请蓝牙权限 2. **网络连接**:确保设备和打印机在同一网络下 3. **USB 连接**:部分 Android 设备需要用户手动授权 USB 设备访问 4. **依赖库**:使用插件的应用需要同时引入 `printer-lib-3.5.3.aar` 依赖 5. **异步回调**:所有方法都是异步的,需要通过回调函数获取结果 6. **错误处理**:建议对所有操作添加错误处理逻辑 ## 五、常见问题 ### 1. 插件无法加载 - 检查 `package.json` 配置是否正确 - 确认 aar 文件路径是否正确 - 重新编译自定义基座 ### 2. 连接失败 - 检查设备权限是否授予 - 确认打印机地址/路径是否正确 - 检查网络连接状态 ### 3. 打印乱码 - 确认文本编码格式 - 检查打印机是否支持中文 ## 六、技术支持 如有问题,请查看项目源码或联系开发者。