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')
|
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, 94, 570, 94) //第一行
|
.drawLine(1, 0, 160, 570, 160) //第二行
|
.drawLine(1, 0, 227, 570, 227) //第三行
|
.drawLine(1, 0, 293, 400, 293) //第四行
|
|
.drawLine(1, 120, 94, 120, 360) // 第一根竖线
|
|
.drawLine(1, 400, 227, 400, 360) // 第二根竖线
|
// 1 原始 是否粗体\字体大小\是否旋转角度\起始横坐标\起始纵坐标\字符串
|
|
.drawText(170, 20, "重庆青山工业有限责任公司", 26, 0, 1, false, false)
|
.drawText(240, 62, '物料标识签', 26, 0, 1, false, false)
|
|
// 3
|
.drawText(10, 120, "物料编号", 24, 0, 0, false, false)
|
.drawText(130, 120, item.item_number, 24, 0, 0, false, false)
|
|
// 4
|
.drawText(10, 185, "物料名称", 24, 0, 0, false, false)
|
.drawText(130, 185, item.item_desc, 24, 0, 0, false, false)
|
|
// 5
|
.drawText(10, 248, "物料批次", 24, 0, 0, false, false)
|
.drawText(130, 248, item.lot_number, 24, 0, 0, false, false)
|
|
// 7
|
.drawText(10, 312, "打印时间", 24, 0, 0, false, false)
|
.drawText(130, 312, item.print_date, 24, 0, 0, false, false)
|
// 二维码
|
.drawQrCode(438, 232, item.product_barcode, 0, 4, 1)
|
.drawText(423, 340, item.product_barcode, 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 = 13;
|
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))
|
uni.getStorageSync('failerr','try')
|
}
|
})
|
},
|
//长按事件
|
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'
|
})
|
},
|
}
|
}
|