最近在作uni-app項目時,遇到了須要藍牙打印文件的功能須要製做,在網上找到了一個教程,這裏分享給你們。css
簡單得引入到本身所須要得頁面中去,本次咱們只要到了標籤模式,他同時還有帳單模式能夠選擇。app
// 藍牙打印 指令和轉碼 var tsc = require('@components/gprint/tsc.js')
首先咱們須要先初始化藍牙模塊,在進行搜索藍牙。在監聽到附近藍牙設備時,記錄他的名稱和deviceId。oop
onBlue(e) { uni.openBluetoothAdapter({ success(res) { //監聽尋找到新設備的事件 that.findDevice() //監聽本機藍牙適配器狀態變化事件 that.onStatus() } })
findDevice(){ console.log("監聽尋找到新設備的事件---------------") //監聽尋找到新設備的事件 uni.onBluetoothDeviceFound(function(devices) { const {name,deviceId} = devices[0]; if(name == "未知設備")return; if(!name || !name.length){ that.devices.push({ name: name, deviceId: deviceId, services: [] }) } that.devices.forEach(e=>{ if(that.devicesList){ let b = true; that.devicesList.forEach(e1=>{ if(e.name == e1.name){ b = false; } }); if(b)that.devicesList.push(e); }else{ that.devicesList.push(e); } }); } }
onStatus(){ uni.getBluetoothAdapterState({ success: function(res) { //本機藍牙開啓時 if (res.available) { //如在正在搜索設備,則中止搜索 if (res.discovering) { uni.stopBluetoothDevicesDiscovery() } //搜索藍牙 //開始搜尋附近的藍牙外圍設備 uni.startBluetoothDevicesDiscovery() } else { console.log('本機藍牙不可用') } }, }) }
搜索出附近藍牙設備後,獲取藍牙設備的deviceId傳入createBLEConnection方法中。在鏈接藍牙設備時,咱們須要注意的是保證儘可能成對的調用 createBLEConnection 和 closeBLEConnection 接口。安卓若是屢次調用 createBLEConnection 建立鏈接,有可能致使系統持有同一設備多個鏈接的實例,致使調用 closeBLEConnection 的時候並不能真正的斷開與設備的鏈接。
咱們將鏈接成功的藍牙信息存到currDev中,以便直接鏈接,無需進行搜索操做。ui
onLink(item){ const {deviceId} = item; console.log("鏈接藍牙---------------" + deviceId); //鏈接低功耗藍牙設備。 uni.createBLEConnection({ deviceId: deviceId, complete(res) { if (res.errMsg != "createBLEConnection:ok") return //鏈接設備時,需斷開本機鏈接設備 uni.closeBLEConnection({ deviceId }) that.connId = deviceId; that.currDev = item setTimeout(()=> { //獲取藍牙設備全部服務(service) that.getBLEServices(deviceId) }, 2000) } //鏈接成功 關閉搜索 uni.stopBluetoothDevicesDiscovery() }) }
getBLEServices(deviceId) { uni.getBLEDeviceServices({ // 這裏的 deviceId 須要已經經過 createBLEConnection 與對應設備創建連接 deviceId: deviceId, complete(res) { const {services} = res; services.forEach(item=>{ const {uuid} = item; uni.getBLEDeviceCharacteristics({ // 這裏的 deviceId 須要已經經過 createBLEConnection 與對應設備創建連接 deviceId: deviceId, // 這裏的 serviceId 須要在 getBLEDeviceServices 接口中獲取 serviceId: uuid, success(res) { const {characteristics} = res; for(let block of characteristics){ if(!block.properties.write)return for (let index in that.devices) { if (that.devices[index].deviceId == deviceId) { that.devices[index].services.push({ serviceId: uuid, characteristicId: block.uuid, }) break } } } uni.setStorage({ key: 'currDev', data: that.devices, }); } }) }) } }) }
打印格式須要本身根據當前設備的格式來進行設置打印。本章用到的是tsc.js中的form格式。this
onPrint(){ if(this.currDev.length == 0){ uni.showToast({ title: '請先鏈接藍牙打印機', duration: 2000 }); return } //標籤模式 const {deviceId} = this.currDev; const {serviceId,characteristicId} = this.currDev.services[0]; var command = tsc.jpPrinter.createNew(); //DaYin這個字段存放咱們須要打印的數據 let DaYin = JSON.parse(JSON.stringify(this.rowsList)); let Customer = JSON.stringify(this.Customer); //打印格式須要根據打印機的特定格式來。在tsc文件中修改格式。 DaYin.forEach(e=>{ command.form(e.ReceSheetNo,`客 戶:${Customer}`,`匹 數:${e.Rolls}`,`坯布品名:${e.GrayID}`,`進倉編號:${e.LotNo}`,`坯布類型:${e.GrayTypeName}`) command.setPagePrint() }) //轉碼處理 this.senBlData(deviceId, serviceId, characteristicId,command.getData()) }
senBlData(deviceId, serviceId, characteristicId,uint8Array) { let uint8Buf = Array.from(uint8Array); function split_array(datas,size){ let result = {}; let j = 0 for (var i = 0; i < datas.length; i += size) { result[j] = datas.slice(i, i + size) j++ } return result } let sendloop = split_array(uint8Buf, 20); function realWriteData(sendloop, i) { let data = sendloop[i] if(typeof(data) == "undefined"){ return } let buffer = new ArrayBuffer(data.length) let dataView = new DataView(buffer) uni.writeBLECharacteristicValue({ deviceId, serviceId, characteristicId, value: buffer, success(res) { realWriteData(sendloop, i + 1); } }) } let i = 0; realWriteData(sendloop, i); },
form條碼格式.net
// 條形碼和文字合成打印 jpPrinter.form = function (content,text1,text2,text3,text4) { data = header + "LEFT" + "\r\n" + "GAR-SENSE" + "\r\n" + barcodeText + "BARCODE " + 128 + " " + 1 + " " + 1 + " " + 125 + " " + 125 + " " + 0 + " " + content + "\r\n" + "TEXT " + " " + 12 + " " + 0 + " " + 125 + " " + 180 + " " + text1 + "\r\n" + "TEXT " + " " + 12 + " " + 2 + " " + 125 + " " + 210 + " " + text2 + "\r\n" + "TEXT " + " " + 12 + " " + 2 + " " + 125 + " " + 240 + " " + text3 + "\r\n" + "TEXT " + " " + 12 + " " + 2 + " " + 125 + " " + 270 + " " + text4 + "\r\n" + "FORM" + "\r\n" ; jpPrinter.addCommand(data) };
轉載於:https://blog.csdn.net/zhanleibo/article/details/103035645code