• uni-app 蓝牙打印, CPCL指令集使用


    先上代码:

    GitHub - byc233518/uniapp-bluetooth-printer-demo: 使用uniApp 连接蓝牙打印机 Demo, CPCL 指令简单实用示例

    (内含 芝珂,佳博,精臣 多个厂家指令集使用文档)

    文件结构:


    ├── App.vue 
    ├── CPCL 指令手册.pdf  // 指令集参考手册
    ├── LICENSE
    ├── README.md
    ├── libs
    │   └── print.js // uni-app打印插件
    ├── main.js // Demo 入口文件
    ├── manifest.json // uni-app 
    ├── node_modules // 依赖
    ├── pages 
    │   ├── index 
    │   │   └── index.vue // Demo 页面, 业务上使用可参考此文件
    │   └── setting
    │   └── index.vue // 打印机连接, 可以集成到应用的系统设置功能
    ├── pages.json // 页面配置文件
    ├── uni.scss // uni-app 样式文件
    └── yarn.lock

    运行方法:

    1. 将代码解压缩, 导入到HBuilderX中;
    2. 连接手机;
    3. 运行到手机或模拟器--运行到Android App 基座;
    4. 待运行完毕, 在 蓝牙设置 界面点击“搜索设备”并进行连接;
    5. 点击”Demo界面”按钮跳转到Demo, 填写表单信息后点击”打印测试”按钮进行测试;

       

    集成步骤:

    1. Demo  libs/print.js 复制到项目目录下;
    2. 参考Demo  pages/setting/index.vue 在项目合适位置集成打印机连接配置功能, 一般在系统设置;
    3. 在需要的文件中引用 libs/print.js  print 方法;
    4. 拼接指令集, 见demo, 更多使用方法参见 CPCL 指令集变成文档.pdf ;
    5. 调用 打印插件的 print 方法进行打印,入参为 打印机ID  拼接好的指令集字符串;

    代码解析:

    *************************************************libs/print.js***********************************************

    1. /**
    2.  * 打印
    3.  * @param mac_address 打印机ID
    4.  * @param data 指令集字符串, 为了灵活起见, 指令集在业务代码中进行转换然后传递进来
    5.  */
    6. export const print = (mac_address, data) => {
    7.     var that = this
    8.     if (!mac_address) {
    9.         uni.showModal({
    10.             title: "提示",
    11.             content: "请选择蓝牙打印机",
    12.             showCancel: false,
    13.         })
    14.         return
    15.     }
    16.     if (!data) {
    17.         uni.showModal({
    18.             title: "提示",
    19.             content: "请提供打印数据.",
    20.             showCancel: false,
    21.         })
    22.         return
    23.     }
    24.     main = plus.android.runtimeMainActivity()
    25.     BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter")
    26.     var UUID = plus.android.importClass("java.util.UUID")
    27.     uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
    28.     BAdapter = BluetoothAdapter.getDefaultAdapter()
    29.     if (!BAdapter.isEnabled()) {
    30.         uni.showModal({
    31.             title: "提示",
    32.             content: "蓝牙处于关闭状态,是否打开?",
    33.             success: (_) => {
    34.                 if (_.confirm) {
    35.                     BAdapter.enable()
    36.                 }
    37.             },
    38.         })
    39.         console.log("蓝牙处于关闭状态,正在打开...")
    40.         return
    41.     }
    42.     device = BAdapter.getRemoteDevice(mac_address)
    43.     plus.android.importClass(device)
    44.     bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid)
    45.     plus.android.importClass(bluetoothSocket)
    46.     if (!bluetoothSocket.isConnected()) {
    47.         console.log("检测到设备未连接,尝试连接....")
    48.         bluetoothSocket.connect()
    49.     }
    50.     console.log("设备已连接")
    51.     if (bluetoothSocket.isConnected()) {
    52.         var outputStream = bluetoothSocket.getOutputStream()
    53.         plus.android.importClass(outputStream)
    54.         outputStream.write([0x1b, 0x40]) //打印复位
    55.         outputStream.flush()
    56.         var bytes = plus.android.invoke(data, "getBytes", "gbk") /*utf-8*/
    57.         outputStream.write(bytes)
    58.         outputStream.flush()
    59.         device = null //这里关键
    60.         bluetoothSocket.close()
    61.     }
    62. }

    ******************************************pages/index/index.vue******************************************

    1. <template>
    2. <view>
    3. <view>
    4. <view>
    5. <input v-model="formData.name" class="uni-input" placeholder="公司名称" />
    6. <input v-model="formData.model" class="uni-input" placeholder="车型" />
    7. <input v-model="formData.code" class="uni-input" placeholder="条码" />
    8. <input v-model="formData.line" class="uni-input" placeholder="产线" />
    9. <input v-model="formData.box" class="uni-input" placeholder="箱号" />
    10. <input v-model="formData.date" class="uni-input" placeholder="日期" />
    11. <input v-model="formData.operator" class="uni-input" placeholder="装箱人" />
    12. <input v-model="formData.auditor" class="uni-input" placeholder="确认人" />
    13. view>
    14. <view class="buttos-bar">
    15. <button class="plain-button plain-button--blue" @click="printTest">打印测试button>
    16. <navigator url="/pages/setting/index" hover-class="navigator-hover">
    17. <button type="default">跳转到设置界面button>
    18. navigator>
    19. view>
    20. view>
    21. view>
    22. view>
    23. template>
    24. <script>
    25. // 引入打印插件的打印方法
    26. import {
    27. print
    28. } from '@/libs/print.js'
    29. export default {
    30. name: 'PrintDemo',
    31. data() {
    32. return {
    33. // 业务数据
    34. formData: {
    35. name: "xxx配件有限公司",
    36. model: "型号123456789",
    37. code: "编码123456789",
    38. line: "产线1",
    39. box: "序号1",
    40. date: "2023/11/15",
    41. operator: "操作人",
    42. auditor: "审核人",
    43. }
    44. }
    45. },
    46. methods: {
    47. printTest() {
    48. // 从缓存中获取已经连接的打印机信息
    49. var printerid = uni.getStorageSync('ble_printerId')
    50. if (printerid) {
    51. if (printerid != null && printerid.length > 0) {
    52. const data = this.formData
    53. // 标签开始, 固定开头, 详见 指令集文档
    54. var str = " ! 0 200 200 350 1 " + '\r\n';
    55. // 设置打印纸张宽度
    56. str += "PAGE-WIDTH 600" + '\r\n';
    57. // 标签内容
    58. // 文本 {command} {font} {size} {x} {y} {data}
    59. str += "TEXT 24 0 30 50 " + data.name +"\r\n";
    60. // 二维码 {command} {type} {x} {y} [M n] [U n] {data}
    61. str += "B QR 380 20 M 2 U 5" + '\r\n';
    62. str += "MA," + data.code +"\r\n";
    63. str += "ENDQR" + '\r\n';
    64. str += "TEXT 24 0 30 100 车型: " + data.model +"\r\n";
    65. str += "TEXT 24 0 30 150 条码编号:" + data.code +"\r\n";
    66. str += "TEXT 24 0 320 150 生产线号:" + data.line +"\r\n";
    67. str += "TEXT 24 0 30 200 装箱序号:" + data.box +"\r\n";
    68. str += "TEXT 24 0 320 200 日期:" + data.date +"\r\n";
    69. str += "TEXT 24 0 30 250 装箱人:" + data.operator +"\r\n";
    70. str += "TEXT 24 0 320 250 确认人:" + data.auditor +"\r\n";
    71. // 标签结束
    72. str += "GAP-SENSE" + '\r\n';
    73. str += "FORM " + '\r\n';
    74. str += "PRINT " + '\r\n';
    75. // 指令集拼接完成, 调用打印插件打印方法进行打印
    76. print(printerid, str);
    77. }
    78. } else {
    79. uni.showModal({
    80. title: '提示',
    81. content: '请先选择已配对的蓝牙打印机, 再进行测试.',
    82. showCancel: false
    83. })
    84. }
    85. },
    86. },
    87. }
    88. script>
    89. <style scoped lang="scss">
    90. .uni-input {
    91. margin-top: 10px;
    92. height: 30px;
    93. border: 1px solid #eee;
    94. }
    95. style>

    ****************************************pages/setting/index.vue******************************************

    方法解释:

    1. searchDevices  //开始搜寻附近的蓝牙外围设备
    2. onConn // 连接打印机

  • 相关阅读:
    机器学习笔记之受限玻尔兹曼机(二)模型表示
    电流互感器与电能仪表的施工安装指导
    Unity-动画状态机使用细节记录
    C#使用策略模式或者委托替代多IfElse判断和Switch语句
    SpringBoot项目集成Redis+JWT实现系统登录token校验
    java基于SpringBoot+vue 的简历模板分享系统 elementui前后端分离
    SM4 研究与实现
    gdb结合valgrind一起使用
    QTday06(人脸识别项目前置知识)
    2022年最新重庆机动车签字授权人模拟考试及答案
  • 原文地址:https://blog.csdn.net/byc233518/article/details/134433477