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) { console.log("进入特征"); setTimeout(() => { uni.getBLEDeviceCharacteristics({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId: serviceId, success: (res) => { console.log(res, '特征getBLEDeviceCharacteristics') this.characteristics = res.characteristics res.characteristics.forEach((item) => { if (item.uuid.indexOf("FF02") != -1) { this.characteristicId = item.uuid uni.setStorageSync("characteristicId", item .uuid) //把已经连接的蓝牙设备标识放入缓存 uni.setStorageSync("serviceId", serviceId) //把已经连接的蓝牙设备ID放入缓存 //console.log('characteristicId:', item.uuid) //利用传参的形势传给下面的notify,这里的uuid如果都需要用到,就不用做判断了,建议使用setTimeout进行间隔性的调用此方法 // this.notifyBLECharacteristicValueChange(item.uuid) setTimeout(() => { uni.hideLoading(); this.BottomShow = false this.DrawerModalL = false }, 1000) } }) }, fail: (res) => { console.log(res) } }) }, 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, }); res.services.forEach((item) => { if (item.uuid.indexOf("FF00") != -1) { this.serviceId = item.uuid; //获取特征 this.getBLEDeviceCharacteristics(deviceId, item.uuid) } }) } }) }, 1000) }, //写入蓝牙 writeBLECharacteristicValue() { this.printflag = false let deviceId = uni.getStorageSync('deviceId') let serviceId = uni.getStorageSync('serviceId') let characteristicId = uni.getStorageSync('characteristicId') // if (!uni.getStorageSync("deviceId")) { // uni.showToast({ // title: '请连接打印机', // icon: 'error', // duration: 2000 // }) // return // } const printerJobs = new PrinterJobs(); // this.singlist.forEach(item => { 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, 570, 52) //第一行 // .drawLine(1, 0, 104, 570, 104) //第二行 // .drawLine(1, 0, 156, 570, 156) //第三行 // .drawLine(1, 0, 208, 400, 208) //第四行 // .drawLine(1, 0, 260, 400, 260) //第五行 // .drawLine(1, 0, 312, 400, 312) //第六行 // .drawLine(1, 120, 0, 120, 360) // 第一根竖线 // .drawLine(1, 220, 208, 220, 312) // 第二根竖线 // .drawLine(1, 300, 208, 300, 312) // 第三根竖线 // .drawLine(1, 400, 156, 400, 312, ) // 第四根竖线 // 1 原始 是否粗体\字体大小\是否旋转角度\起始横坐标\起始纵坐标\字符串 // .drawText(10, 20, "物料大类", 24, 0, 0, false, false) // .drawText(130, 20, this.printlist.category, 24, 0, 0, false, false) // // 2 // .drawText(10, 72, "物料名称", 24, 0, 0, false, false) // .drawText(130, 72, item.materialName, 24, 0, 0, false, false) // // 3 // .drawText(10, 124, "物料批次", 24, 0, 0, false, false) // .drawText(130, 124, item.lotsSn, 24, 0, 0, false, false) // // 4 // .drawText(10, 176, "工单号", 24, 0, 0, false, false) // .drawText(130, 176, item.wo, 24, 0, 0, false, false) // // 5 // .drawText(10, 225, "生产线", 24, 0, 0, false, false) // .drawText(130, 225, item.productLine, 24, 0, 0, false, false) // // 5-1 // .drawText(230, 225, "工序", 24, 0, 0, false, false) // .drawText(310, 225, item.cellNo, 24, 0, 0, false, false) // // 6 // .drawText(10, 280, "物料数量", 24, 0, 0, false, false) // .drawText(130, 280, item.materialNum, 24, 0, 0, false, false) // // 6-1 // .drawText(230, 280, "清洗", 24, 0, 0, false, false) // .drawText(340, 280, item.cleanAttr, 24, 0, 0, false, false) // // 7 // .drawText(10, 332, "打印时间", 24, 0, 0, false, false) // .drawText(130, 332, item.cTime, 24, 0, 0, false, false) // .drawText(440, 332, item.user, 24, 0, 0, false, false) // // 条形码 .drawBarCode(10, 50, this.printlist.wareMaterialCode,1, 0, 1, 50 ) // .drawText(434, 310, item.containerCode, 1, 0, 0, false, false) .label_print(0, 1); // }) let buffer = printerJobs.buffer(); // console.log('ArrayBuffer', 'length: ' + buffer.byteLength, ' hex: ' + ab2hex(buffer)); // 1.并行调用多次会存在写失败的可能性 // 2.建议每次写入不超过20字节 // 分包处理,延时调用 const maxChunk = 15 const delay = 20; for (let i = 0, j = 0, length = buffer.byteLength; i < length; i += maxChunk, j++) { let subPackage = buffer.slice(i, i + maxChunk <= length ? (i + maxChunk) : length); // setTimeout(this._writeBLECharacteristicValue, j * delay, subPackage); setTimeout(() => { this._writeBLECharacteristicValue(deviceId, serviceId, characteristicId, subPackage) }, j * delay); } }, _writeBLECharacteristicValue(deviceId, serviceId, characteristicId, buffer) { uni.writeBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId, characteristicId: characteristicId, value: buffer, success(res) { console.log('writeBLECharacteristicValue success', res.errMsg) }, fail(res) { console.log(JSON.stringify(res)) console.log(JSON.stringify(buffer)) } }) }, //长按事件 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' }) }, } }