wangfukang 3 years ago
parent
commit
0ffdd6a5f0
  1. BIN
      components/.DS_Store
  2. 455
      components/kk-printer/index.vue

BIN
components/.DS_Store

Binary file not shown.

455
components/kk-printer/index.vue

@ -69,8 +69,8 @@
serviceId: '', serviceId: '',
writeId: '', writeId: '',
readId: '', readId: '',
iosNo: false, iosNo: false,
deviceType:'ios' deviceType: 'ios'
} }
}, },
props: { props: {
@ -103,9 +103,16 @@
}, },
mounted() { mounted() {
}, },
onLoad(){ watch: {
isShowSearch(newVal, oldVal) {
if(newVal == false){
this.$parent.$parent.isShow = true
}
}
},
onLoad() {
}, },
beforeDestroy() { beforeDestroy() {
@ -116,236 +123,238 @@
return; return;
}, },
//点击打印按钮 //点击打印按钮
handlePrintTap() { handlePrintTap() {
let that = this let that = this
uni.showLoading({ console.log(this.$parent)
mask:true, this.$parent.$parent.clickShow();
title: '加载中...' uni.showLoading({
}); mask: true,
uni.getSystemInfo({ title: '加载中...'
success (res) { });
that.deviceType = res.osName uni.getSystemInfo({
console.log('设备类型',res.osName) success(res) {
} that.deviceType = res.osName
console.log('设备类型', res.osName)
}
}) })
// 初始化蓝牙模块 // 初始化蓝牙模块
wx.openBluetoothAdapter({ //111 wx.openBluetoothAdapter({ //111
mode: 'central', mode: 'central',
success: (res2) => { success: (res2) => {
console.log('初始化蓝牙模块', res2) console.log('初始化蓝牙模块', res2)
that.sousuo() that.sousuo()
}, },
fail: (res) => { fail: (res) => {
console.log('初始化蓝牙模块', res) console.log('初始化蓝牙模块', res)
that.tui.toast('初始化失败', res) that.tui.toast('初始化失败', res)
this.isShowSearch = true this.isShowSearch = true
uni.hideLoading();
}
})
},
// 开始搜索附近的蓝牙外围设备
sousuo() {
let that = this;
wx.startBluetoothDevicesDiscovery({ //222
success(res3) {
console.log('获取设备成功', res3)
that.jiantingshebei()
},
fail(err) {
console.log('获取设备失败', err)
that.tui.toast('获取设备失败', err)
wx.closeBLEConnection({
deviceId: uni.getStorageSync('deviceId'),
success(res) {
console.log(res)
}
})
uni.removeStorageSync('deviceId');
that.isShowSearch = true
that.sousuo()
uni.hideLoading(); uni.hideLoading();
} }
}) })
},
//停止搜索蓝牙设备
stopSearchBtnTap() {
wx.stopBluetoothDevicesDiscovery()
},
/**
* jiantingshebei方法,判断缓存中有没有deviceId等数据的时候,
* 有: 直接拿到缓存中的deviceId等,直接请求 wx.createBLEConnection
* 没有:才会调用 wx.onBluetoothDeviceFound 搜索
*/
jiantingshebei() {
let that = this;
if (uni.getStorageSync('deviceId')) {
console.log("111")
// that.huoqufuwu(uni.getStorageSync('deviceId'))
that.lianejieshebei(uni.getStorageSync('deviceId'))
} else {
console.log("222")
uni.hideLoading();
this.isShowSearch = true
// 监听扫描到新设备事件
wx.onBluetoothDeviceFound((res4) => { //333
console.log('监听新设备列表,拿到想要的就自动停止', res4)
res4.devices.forEach((device) => {
if (!device.name && !device.localName) {
return
}
that.devicesList.push(device)
})
})
}
},
lianejieshebei(deviceId) {
let that = this;
// 连接设备
wx.createBLEConnection({ //555
deviceId: deviceId, // 搜索到设备的 deviceId
success: () => {
that.isShowSearch = false
console.log('连接设备成功')
that.huoqufuwu(deviceId)
},
fail: (res) => {
console.log('连接设备失败', res)
that.tui.toast('连接设备失败', res)
if (uni.getStorageSync('deviceId')) {
that.huoqufuwu(uni.getStorageSync('deviceId'))
} else {
wx.closeBLEConnection({
deviceId: uni.getStorageSync('deviceId'),
success(res) {
console.log(res)
}
})
uni.removeStorageSync('deviceId')
that.sousuo()
that.isShowSearch = true
}
}, uni.hideLoading();
// 开始搜索附近的蓝牙外围设备 }
sousuo(){ })
let that = this; },
wx.startBluetoothDevicesDiscovery({ //222 huoqufuwu(deviceId) {
success(res3) { let that = this;
console.log('获取设备成功', res3) //获取服务
that.jiantingshebei() wx.getBLEDeviceServices({ //666
}, deviceId: deviceId, // 搜索到设备的 deviceId
fail(err) { success: (res5) => {
console.log('获取设备失败', err) console.log('获取服务成功', res5)
that.tui.toast('获取设备失败', err) that.duxietezhengzhi(deviceId, res5)
wx.closeBLEConnection({ },
deviceId:uni.getStorageSync('deviceId'), fail: (res) => {
success (res) { console.log(res)
console.log(res) that.tui.toast('获取服务失败', res)
} wx.closeBLEConnection({
}) deviceId: uni.getStorageSync('deviceId'),
uni.removeStorageSync('deviceId'); success(res1) {
this.isShowSearch = true console.log(res1)
this.sousuo() }
uni.hideLoading(); })
} that.isShowSearch = true
}) uni.hideLoading();
}, }
//停止搜索蓝牙设备 })
stopSearchBtnTap(){ },
wx.stopBluetoothDevicesDiscovery() duxietezhengzhi(deviceId, res5) {
}, let that = this;
/** //读写特征值
* jiantingshebei方法,判断缓存中有没有deviceId等数据的时候, wx.getBLEDeviceCharacteristics({ //777
* 有: 直接拿到缓存中的deviceId等,直接请求 wx.createBLEConnection deviceId: deviceId, // 搜索到设备的 deviceId
* 没有:才会调用 wx.onBluetoothDeviceFound 搜索 serviceId: res5.services[0].uuid, // 上一步中找到的某个服务
*/ success: (res6) => {
jiantingshebei(){ console.log('读写特征值成功', res6)
let that = this; for (let i = 0; i < res6.characteristics.length; i++) {
if(uni.getStorageSync('deviceId')){ let item = res6.characteristics[i]
console.log("111") if (item.properties.write) { // 该特征值可写
// that.huoqufuwu(uni.getStorageSync('deviceId')) console.log('该特征值可写')
that.lianejieshebei(uni.getStorageSync('deviceId')) uni.setStorageSync('deviceId', deviceId);
}else{ uni.setStorageSync('serviceId', res5.services[0].uuid);
console.log("222") uni.setStorageSync('characteristicId', item.uuid);
uni.hideLoading(); that.xierushuju()
this.isShowSearch = true return;
// 监听扫描到新设备事件 }
wx.onBluetoothDeviceFound((res4) => { //333 }
console.log('监听新设备列表,拿到想要的就自动停止', res4) },
res4.devices.forEach((device) => { fail: (res) => {
if (!device.name && !device.localName) { that.tui.toast('读写特征值失败', res)
return wx.closeBLEConnection({
} deviceId: uni.getStorageSync('deviceId'),
that.devicesList.push(device) success(res) {
}) console.log(res)
}) }
} })
that.isShowSearch = true
}, uni.hideLoading();
lianejieshebei(deviceId){ }
let that = this; })
// 连接设备 },
wx.createBLEConnection({//555 xierushuju() {
deviceId: deviceId, // 搜索到设备的 deviceId let that = this;
success: () => { //写入数据
this.isShowSearch = false if (this.defaultText == '打印小票') {
console.log('连接设备成功') that.$emit('onPrintSmall');
that.huoqufuwu(deviceId) } else if (this.defaultText == '确认打印') {
}, that.$emit('goPrint');
fail: (res) => { } else {
console.log('连接设备失败',res) that.$emit('onPrint');
that.tui.toast('连接设备失败', res) }
if(uni.getStorageSync('deviceId')){ console.log('查看数据是否正确', that.bufferData)
this.huoqufuwu(uni.getStorageSync('deviceId')) that.$nextTick(() => {
}else{ var dataStr = ''
wx.closeBLEConnection({ for (let m = 0; m < that.bufferData.length; m++) {
deviceId:uni.getStorageSync('deviceId'), dataStr += that.bufferData[m]
success (res) { }
console.log(res) const maxChunk = that.deviceType == 'android' ? 20 : 180;
} const delay = that.deviceType == 'android' ? 20 : 180;
})
uni.removeStorageSync('deviceId') let buffer = gbk.strToGBKByte(dataStr)
this.sousuo() for (let i = 0, j = 0, length = buffer.byteLength; i < length; i += maxChunk, j++) {
this.isShowSearch = true let subPackage = buffer.slice(i, i + maxChunk <= length ? (i + maxChunk) : length);
} setTimeout(that._writeBLECharacteristicValue, j * delay, subPackage);
}
uni.hideLoading();
} })
}) return;
}, },
huoqufuwu(deviceId){ _writeBLECharacteristicValue(buffer) {
let that = this; let that = this;
//获取服务 wx.writeBLECharacteristicValue({
wx.getBLEDeviceServices({ //666 deviceId: uni.getStorageSync('deviceId'),
deviceId: deviceId, // 搜索到设备的 deviceId serviceId: uni.getStorageSync('serviceId'),
success: (res5) => { characteristicId: uni.getStorageSync('characteristicId'),
console.log('获取服务成功',res5) value: buffer,
that.duxietezhengzhi(deviceId,res5) success(res) {
}, console.log('打印buffer', buffer)
fail: (res) => { console.log('writeBLECharacteristicValue success', res)
console.log(res) },
that.tui.toast('获取服务失败', res) fail(res) {
wx.closeBLEConnection({ console.log('writeBLECharacteristicValue fail', res)
deviceId:uni.getStorageSync('deviceId'), wx.closeBLEConnection({
success (res1) { deviceId: uni.getStorageSync('deviceId'),
console.log(res1) success(res) {
} console.log(res)
}) }
this.isShowSearch = true })
uni.hideLoading(); if (uni.getStorageSync('deviceId')) {
} console.log("111")
}) that.xierushuju()
}, } else {
duxietezhengzhi(deviceId,res5){ that.jiantingshebei()
let that = this; }
//读写特征值 }
wx.getBLEDeviceCharacteristics({ //777 })
deviceId:deviceId, // 搜索到设备的 deviceId uni.hideLoading();
serviceId:res5.services[0].uuid, // 上一步中找到的某个服务 },
success: (res6) => { handleSearchClose() {
console.log('读写特征值成功',res6) this.isShowSearch = false
for (let i =0; i <res6.characteristics.length; i++) {
let item =res6.characteristics[i]
if (item.properties.write) { // 该特征值可写
console.log('该特征值可写')
uni.setStorageSync('deviceId', deviceId);
uni.setStorageSync('serviceId', res5.services[0].uuid);
uni.setStorageSync('characteristicId', item.uuid);
that.xierushuju()
return;
}
}
},
fail: (res) => {
that.tui.toast('读写特征值失败', res)
wx.closeBLEConnection({
deviceId:uni.getStorageSync('deviceId'),
success (res) {
console.log(res)
}
})
this.isShowSearch = true
uni.hideLoading();
}
})
},
xierushuju(){
let that = this;
//写入数据
if(this.defaultText == '打印小票'){
that.$emit('onPrintSmall');
}else if(this.defaultText == '确认打印'){
that.$emit('goPrint');
}else{
that.$emit('onPrint');
}
console.log('查看数据是否正确',that.bufferData)
that.$nextTick(()=>{
var dataStr = ''
for(let m=0;m<that.bufferData.length;m++){
dataStr += that.bufferData[m]
}
const maxChunk = that.deviceType== 'android'?20:180;
const delay = that.deviceType== 'android'?20:180;
let buffer = gbk.strToGBKByte(dataStr)
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(that._writeBLECharacteristicValue, j * delay, subPackage);
}
})
return;
},
_writeBLECharacteristicValue(buffer) {
let that = this;
wx.writeBLECharacteristicValue({
deviceId: uni.getStorageSync('deviceId'),
serviceId: uni.getStorageSync('serviceId'),
characteristicId: uni.getStorageSync('characteristicId'),
value: buffer,
success(res) {
console.log('打印buffer',buffer)
console.log('writeBLECharacteristicValue success', res)
},
fail(res) {
console.log('writeBLECharacteristicValue fail', res)
wx.closeBLEConnection({
deviceId:uni.getStorageSync('deviceId'),
success (res) {
console.log(res)
}
})
if(uni.getStorageSync('deviceId')){
console.log("111")
that.xierushuju()
}else{
that.jiantingshebei()
}
}
})
uni.hideLoading();
},
handleSearchClose(){
this.isShowSearch = false
} }
} }
} }

Loading…
Cancel
Save