# 蓝牙小票打印机 **Repository Path**: walloc/BLETicketPrinter ## Basic Information - **Project Name**: 蓝牙小票打印机 - **Description**: 用于小程序集成蓝牙小票打印(蓝牙低功耗外围设备小票打印) - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 11 - **Created**: 2022-06-05 - **Last Updated**: 2022-06-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 当前项目状态 - [✓] 文字打印 - [✓] 列表表格打印 #### 下方示例代码,路径:pages/index/index.js ```javascript import { Devices } from "../../BLETicketPrinter/devices"; import { Printer } from "../../BLETicketPrinter/printer"; Page({ data: { //状态栏高度,默认值是iPhone XS +++ 的 statusBarHeight: 44, //是否正在发现设备 discovering: false, //已发现的设备列表(名称不为空、不会重复的) devices: [], //已选设备 device: { deviceId: '', name: '', RSSI: 0, serviceId: '', isPrimary: false, characteristicsId: '' } }, //页面加载... onLoad() { let self = this; //设置状态栏高度 wx.getSystemInfo({ success: res => { self.setData({statusBarHeight: res.statusBarHeight}); } }); }, //页面准备完毕 onReady() { let self = this; //开始 Devices.start(); //监听发现设备时 Devices.onDeviceFound(device => { //将设备加入已发现列表,用于页面渲染 self.data.devices.push(device); //更新数据,让设备在列表中展示出来 self.setData({ devices: self.data.devices }); }); //监听蓝牙模块初始化 Devices.onAdapterStateChange(res => { //标记 self.setData({ discovering: res.discovering }); }); //监听设备已连接/已断开 Devices.onConnectionStateChange(res => { console.log(res.deviceId, res.connected ? "已连接" : "已断开"); let devices = self.data.devices; for(let i = 0; i { if(res.error){ self.showMessage(res.message); } self.setData({ discovering: res.error === 0 }); }); }, //停止搜索 stopDiscover(){ let self = this; Devices.stopDiscovery(res => { if(res.error){ self.showMessage(res.message); } self.setData({ discovering: false }); }); }, //选择设备, data-id="{{d.deviceId}}" selectDevice(e){ const { id } = e.currentTarget.dataset; let device = Devices.getDeviceById(id); if(device !== null){ this.setData({ device: device }); } //停止发现设备 this.stopDiscover(); }, //连接设备(必须已选择设备) connectDevice(){ if(this.data.device.deviceId !== "") { let self = this; //发起连接,连接成功时,会直接获取到服务ID、服务特征ID Devices.connectDevice(this.data.device.deviceId, res => { if(res.error){ self.showMessage(res.message); }else{ //连接成功(关于标记连接成功的,可在上方监听事件里看得,这里也可以处理) self.setData({ device: res.device }); } }); } else{ this.showMessage("先选择设备"); } }, //连接成功后,测试打印 printTest(){ let self = this; let order = { order_id: "202204210901", time: "2022-04-21 14:00:36", mall_name: "助小农大数据体验店-武鸣桥罗旗舰店", manager_name: "小农人", price: 3825, coupon: 300, discount: 50, pay_price: 3475, //多例 pros: [ { name: "40%乐土富安植物蛋白有机肥", amount: 30, price: 80 }, { name: "啶虫咪50%", amount: 15, price: 35 }, { name: "王铜", amount: 10, price: 90 } ], //单列 lines: ["你知道我在等你妈?如果不知道的话就漆黑了,有本事去找点钱啊?", "你如果真的在乎我。", "我不在乎你!"] }; //将订单商品列表转换成一个数组 let pros = []; //表头 pros.push(["名称", "数量", "单价"]); //多列的列表 for(let i=0; i { if(!res.error){ wx.showToast({ icon: "success", duration: 2000, title: "打印完成" }); }else{ self.showMessage(res.message); } }); }else{ self.showMessage("未选择设备"); } } }) ``` > 该项目包含其它作者的开发源码,后续一一列出...