import printerUtil from '@/components/print/printerutil.js' const PrinterJobs = require('@/components/print/printerjobs.js') // import { // getDate // } from '@/utils/dateTime.js' export default { data() { return { list: [], deviceId: '', serviceId: '', characteristics: [], characteristicId: '', deviceslist: [] } }, onShow() { this.list = [] this.deviceslist = [] }, methods: { //初始化蓝牙设备 openBluetoothAdapter() { let _this = this uni.openBluetoothAdapter({ success: (res) => { //已打开 uni.getBluetoothAdapterState({ //蓝牙的匹配状态 success: (res1) => { console.log(res1, '本机设备的蓝牙已打开') // 开始搜索蓝牙设备 _this.startBluetoothDeviceDiscovery() uni.setStorageSync('blueShow', true) }, fail(error) { uni.showToast({ icon: 'none', title: '查看手机蓝牙是否打开err' }); } }); }, fail: err => { //未打开 uni.setStorageSync('blueShow', false) uni.showToast({ icon: 'none', title: '查看手机蓝牙是否打开' }); } }) }, // 开始搜索蓝牙设备 startBluetoothDeviceDiscovery() { uni.startBluetoothDevicesDiscovery({ success: (res) => { // 发现外围设备 uni.showLoading({ title: '加载中', mask: true }); this.onBluetoothDeviceFound() }, fail: err => { console.log(err, '错误信息') } }) }, // 发现外围设备 onBluetoothDeviceFound() { uni.onBluetoothDeviceFound((res) => { this.deviceslist.length > 0 ? uni.hideLoading() : '' if (this.deviceslist.indexOf(res.devices[0].deviceId) == -1) { this.deviceslist.push(res.devices[0].deviceId) if (res.devices[0].name != "") { uni.hideLoading(); this.list.push({ name: res.devices[0].name, deviceId: res.devices[0].deviceId }) } } }) }, //获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。 getBluetoothDevices() { console.log("获取蓝牙设备"); uni.getBluetoothDevices({ success: res => { console.log('获取蓝牙设备成功:'); console.log(res.devices); } }); }, //选择设备连接吧deviceId传进来 createBLEConnection(deviceId) { //data里面建立一个deviceId,存储起来 let _this = this uni.showLoading({ title: '连接蓝牙中...', mask: true }); this.stopBluetoothDevicesDiscovery() this.deviceId = deviceId, //连接蓝牙 uni.createBLEConnection({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, success(res) { uni.hideLoading(); // uni.showToast({ // icon: 'none', // title: '蓝牙连接成功' // }); uni.setStorageSync("deviceId", deviceId) //把已经连接的蓝牙设备信息放入缓存 setTimeout(() => { _this.getBLEDeviceServices(deviceId) }, 1000) }, fail(res) { uni.showToast({ icon: 'none', title: '蓝牙连接失败' }); } }) }, // 停止搜寻蓝牙设备 stopBluetoothDevicesDiscovery() { uni.stopBluetoothDevicesDiscovery({ success: e => { this.loading = false console.log('停止搜索蓝牙设备:' + e.errMsg); }, fail: e => { console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode); } }); }, //获取蓝牙特征 getBLEDeviceCharacteristics(deviceId, serviceId,callback) { console.log("进入特征"); setTimeout(() => { uni.getBLEDeviceCharacteristics({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId: serviceId, success: (res) => { console.log(res, '特征getBLEDeviceCharacteristics') this.characteristics = res.characteristics let _noCharacteristics = true; console.log('characteristics长度:'+res.characteristics.length) for (let i=0;i { uni.hideLoading(); this.BottomShow = false this.DrawerModalL = false }, 1000) _noCharacteristics=false; callback && callback(true) break; } } if (_noCharacteristics) { callback && callback(false) } }, fail: (res) => { console.log(res) callback && callback(false) } }) }, 1000) }, // 启用 notify 功能 notifyBLECharacteristicValueChange(characteristicId) { let _this = this console.log('deviceId' + _this.deviceId) console.log('serviceId' + _this.serviceId) console.log('characteristicId' + characteristicId) // _this.fanhui() uni.notifyBLECharacteristicValueChange({ state: true, // 启用 notify 功能 // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: this.deviceId, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId: this.serviceId, // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取 characteristicId: characteristicId, success: (res) => { if (this.blueFlag == true) { this.writeBLECharacteristicValue() } else { uni.showLoading({ title: '蓝牙服务获取成功,请打印', mask: true, }); } setTimeout(() => { uni.hideLoading(); }, 1000) // _this.fanhui() this.print = "打印" this.BottomShow = false console.log('notifyBLECharacteristicValueChange success', res.errMsg) }, fail: (res) => { uni.showLoading({ title: '蓝牙服务获取成功,请打印', mask: true, }); setTimeout(() => { uni.hideLoading(); }, 1000) _this.print = "打印" _this.BottomShow = false // _this.fanhui() console.log('notifyBLECharacteristicValueChange fail', res.errMsg) } }) }, //获取蓝牙的所有服务 getBLEDeviceServices(deviceId) { uni.showLoading({ title: '正在获取蓝牙服务...', mask: true, }); setTimeout(() => { uni.getBLEDeviceServices({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, success: (res) => { uni.showLoading({ title: '获取中,请稍后...', mask: true, }); let _noSer = true; console.log('services长度:'+res.services.length) for (let i=0;i{ if (fxx) { uni.hideLoading() uni.showToast({ title: '蓝牙连接成功!', icon: 'none', duration: 2000 }) } else { uni.hideLoading() uni.showToast({ title: '取特征码失败', icon: 'error', duration: 2000 }) } }) break; } } if (_noSer) { uni.hideLoading() uni.showToast({ title: '取蓝牙服务失败', icon: 'error', duration: 2000 }) } }, fail:()=>{ uni.hideLoading() uni.showToast({ title: '获取蓝牙服务失败', icon: 'error', duration: 2000 }) } }) }, 1000) }, //写入蓝牙 writeBLECharacteristicValue() { // this.printflag = false uni.showLoading({ title: '打印中...', mask:true }); let deviceId = uni.getStorageSync('deviceId') let serviceId = uni.getStorageSync('serviceId') let characteristicId = uni.getStorageSync('characteristicId') const printerJobs = new PrinterJobs(); printerJobs .label_set_page(576, 400) //边框 .drawLine(1, 0, 0, 570, 0) .drawLine(1, 0, 0, 0, 360) .drawLine(1, 570, 0, 570, 360) .drawLine(1, 0, 360, 570, 360) // 内部横线 .drawLine(1, 0, 52, 400, 52) //第一行 .drawLine(1, 0, 104, 400, 104) //第二行 .drawLine(1, 0, 156, 570, 156) //第三行 .drawLine(1, 0, 208, 570, 208) //第四行 .drawLine(1, 0, 260, 570, 260) //第五行 .drawLine(1, 0, 312, 570, 312) //第六行 .drawLine(1, 120, 0, 120, 360) // 第一根竖线 // .drawLine(1, 220, 208, 220, 312) // 第二根竖线 // .drawLine(1, 300, 208, 300, 312) // 第三根竖线 .drawLine(1, 400, 0, 400, 156) // 第四根竖线 // 1 原始 是否粗体\字体大小\是否旋转角度\起始横坐标\起始纵坐标\字符串 .drawText(10, 20, "制件状态", 24, 0, 0, false, false) .drawText(130, 20, this.stateText, 24, 0, 0, false, false) // 2 .drawText(10, 72, "组盘单号", 24, 0, 0, false, false) .drawText(130, 72, this.cvi, 24, 0, 0, false, false) // 3 .drawText(10, 124, "生产班线", 24, 0, 0, false, false) .drawText(130, 124, this.singlist.createLine, 24, 0, 0, false, false) // 4 .drawText(10, 176, "零件1", 24, 0, 0, false, false) .drawText(130, 176, this.singlist.itemDes, 24, 0, 0, false, false) // 5 .drawText(10, 225, "零件2", 24, 0, 0, false, false) .drawText(130, 225, this.singlist.itemName, 24, 0, 0, false, false) // 6 .drawText(10, 280, "零件3", 24, 0, 0, false, false) .drawText(130, 280, this.singlist.itemNum, 24, 0, 0, false, false) // 7 .drawText(10, 332, "生产日期", 24, 0, 0, false, false) .drawText(130, 332, this.singlist.updateTime, 24, 0, 0, false, false) // .drawText(440, 332, item.user, 24, 0, 0, false, false) // 二维码 .drawQrCode(426, 14, this.containerCode, 0, 5, 1) // .drawText(434, 310, this.containerCode, 1, 0, 0, false, false) .label_print(0, 1); let buffer = printerJobs.buffer(); /* this._writeBLECharacteristicValue(deviceId, serviceId, characteristicId, buffer,(f)=>{ uni.hideLoading(); if (!f) { uni.showModal({ title:'系统提示', content:'蓝牙传输错误!', showCancel:false }) } }) */ // 建议每次写入不超过20字节,分包处理 /* let _i = 0,_length = buffer.byteLength,___continue = true; const maxChunk = 20; while (_i<_length) { if (!___continue) { uni.hideLoading(); uni.showModal({ title:'系统提示', content:'蓝牙传输错误!', showCancel:false }) break; } if (!_writing) { _writing = true; let subPackage = buffer.slice(_i, (_i + maxChunk <= _length ? (_i + maxChunk) : _length)); this._writeSubBLECharacteristicValue(deviceId, serviceId, characteristicId, subPackage,_i,(flagx)=>{ if (!flagx) { ___continue = false; } else { if (_i + maxChunk >= _length) { uni.hideLoading(); } _i = _i + maxChunk; } _writing = false; }) } } */ const maxChunk = 20; const delay = 15; let _writing = false; let length = buffer.byteLength; let pakageLen = Math.trunc(length/maxChunk); if (length%maxChunk!==0) pakageLen=pakageLen+1; console.log(`共${length}字节,分${pakageLen}包`) /* let i=0; let _errFlag = false; while (i{ console.log(`第${i}包返回${f}`) if (f) { if (i===pakageLen-1) { uni.hideLoading(); } i++ } else { _errFlag = true; } _writing = false; }) } } */ let successArr = []; for (let i = 0, j = 0; i < length; i += maxChunk, j++) { let subPackage = buffer.slice(i, i + maxChunk <= length ? (i + maxChunk) : length); //_count = j; setTimeout(() => { console.log(`第${j+1}分包发送,开始字节${i}`) this._writeBLECharacteristicValue(deviceId, serviceId, characteristicId, subPackage,(f)=>{ console.log(`${j+1}返回${f}`) if (f) { successArr.push(j+1) } if (j===pakageLen-1) { uni.hideLoading(); uni.showModal({ title:'打印提示', content:`蓝牙传输分${pakageLen}包,成功传输${successArr.length}个包,依次为:${successArr.join(",")}。`, showCancel:false }) } }) }, j * delay); } }, // 并行调用多次会存在写失败的可能性,延时调用 _writeSubBLECharacteristicValue(deviceId, serviceId, characteristicId,subPackage,index,callback){ this._writeBLECharacteristicValue(deviceId, serviceId, characteristicId, subPackage,callback) /* if (index===0) { this._writeBLECharacteristicValue(deviceId, serviceId, characteristicId, subPackage,callback) } else { setTimeout(()=>{ this._writeBLECharacteristicValue(deviceId, serviceId, characteristicId, subPackage,callback) },50) } */ }, //写入蓝牙 _writeBLECharacteristicValue(deviceId, serviceId, characteristicId, buffer,callback) { //console.log('start writeBLECharacteristicValue') uni.writeBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId, characteristicId: characteristicId, value: buffer, success(res) { //console.log('writeBLECharacteristicValue success', res.errMsg) callback && callback(true) }, fail(res) { //console.log('writeBLECharacteristicValue fail') //console.log(JSON.stringify(res)) //console.log(JSON.stringify(buffer)) callback && callback(false) } }) }, //长按事件 longpress(item, index) { let _this = this if (uni.getStorageSync('deviceId')) { uni.showToast({ title: '蓝牙已连接!', icon: 'none', duration: 2000 }) return } // uni.showModal({ // title: '提示', // content: '是否连接蓝牙:' + item.name, // showCancel: true, // cancelColor: '#333333', // success: (res => { // if (res.confirm) { _this.stopBluetoothDevicesDiscovery() uni.setStorageSync('itemblue', item.name) this.itemblue = item.name _this.createBLEConnection(item.deviceId) // } else if (res.cancel) { // uni.showToast({ // title: '已取消连接', // duration: 2000, // icon: 'none' // }) // } // }) // }); }, // 点击断开蓝牙连接 tomy() { var _this = this uni.closeBluetoothAdapter({ success(res) { uni.removeStorageSync('deviceId') uni.removeStorageSync("serviceId"); uni.removeStorageSync("characteristicId"); _this.openBluetoothAdapter() } }) }, //返回 // fanhui() { // uni.navigateTo({ // url: './printing' // }) // }, } }