wangfukang 3 years ago
parent
commit
1aea34d189
  1. 252
      components/kk-printer/index.vue

252
components/kk-printer/index.vue

@ -15,7 +15,7 @@
<input type="text" placeholder-class="kk-placeholder-class" placeholder="请输入蓝牙名字或设备ID搜索" v-model="filterName" /> <input type="text" placeholder-class="kk-placeholder-class" placeholder="请输入蓝牙名字或设备ID搜索" v-model="filterName" />
</view> --> </view> -->
<view class="kk-btn-wrap"> <view class="kk-btn-wrap">
<view class="kk-btn-item confirm-btn" @tap="searchBtnTap" v-if="!isSearching"> <view class="kk-btn-item confirm-btn" @tap="sousuo" v-if="!isSearching">
搜索设备 搜索设备
</view> </view>
<view class="kk-btn-item confirm-btn" v-else> <view class="kk-btn-item confirm-btn" v-else>
@ -26,29 +26,21 @@
</view> </view>
</view> </view>
<view class="kk-devices-wrap"> <view class="kk-devices-wrap">
<view class="empty-wrap" v-if="filterDeviceList.length <= 0"> <view class="empty-wrap" v-if="devicesList.length <= 0">
<view class="empty-icon"></view> <view class="empty-icon"></view>
<view class="empty-text">无可搜索到的设备</view> <view class="empty-text">无可搜索到的设备</view>
</view> </view>
<view class="" v-else> <view class="" v-else>
<view class="kk-devices-item" v-for="(item,index) in filterDeviceList" :key="index" <view class="kk-devices-item" v-for="(item,index) in devicesList" :key="index"
@tap="handleConnectDevice(item)"> @tap="lianejieshebei(item.deviceId)">
<view class="name" style="color:#000;"> <view class="name" style="color:#000;">
<text>设备名称:</text> <text>设备名称:</text>
<text>{{item.name?item.name:'未命名'}}</text> <text>{{item.name?item.name:'未命名'}}</text>
</view> </view>
<view class="rssi"> <view class="rssi">
<text>信号强度:</text> <text>信号强度:</text>
<text>({{Math.max(0, item.RSSI + 100)}}%)</text> <text>({{item.RSSI}})</text>
</view> </view>
<!-- <view class="deviceid">
<text>设备ID:</text>
<text>{{item.deviceId}}</text>
</view>
<view class="advmac" v-if="item.advMac">
<text>advMac:</text>
<text>{{item.advMac}}</text>
</view> -->
</view> </view>
</view> </view>
</view> </view>
@ -117,38 +109,6 @@
require: true require: true
} }
}, },
computed: {
mapFilterRSSI() {
return (0 - this.filterRSSI)
},
filterDeviceList() {
let devices = this.devicesList;
let name = this.filterName;
let rssi = this.filterRSSI;
//按RSSI过滤
let filterDevices1 = devices.filter((item) => {
return item.RSSI > rssi
})
// 按名字过滤
let filterDevices2
if (name != '') {
filterDevices2 = filterDevices1.filter((item) => {
return (item.name.indexOf(name) >= 0 || item.deviceId.indexOf(name) >= 0)
})
} else {
filterDevices2 = filterDevices1
}
// 根据广播数据提取MAC地址
for (let i = 0; i < filterDevices2.length; i++) {
if (filterDevices2[i].hasOwnProperty('advertisData')) {
if (filterDevices2[i].advertisData.byteLength == 8) {
filterDevices2[i].advMac = util.buf2hex(filterDevices2[i].advertisData.slice(2, 7));
}
}
}
return filterDevices2
}
},
mounted() { mounted() {
}, },
@ -166,6 +126,10 @@
//点击打印按钮 //点击打印按钮
handlePrintTap() { handlePrintTap() {
let that = this let that = this
uni.showLoading({
mask:true,
title: '加载中...'
});
uni.getSystemInfo({ uni.getSystemInfo({
success (res) { success (res) {
that.deviceType = res.osName that.deviceType = res.osName
@ -180,7 +144,9 @@
that.sousuo() that.sousuo()
}, },
fail: (res) => { fail: (res) => {
that.tui.toast('初始化失败', res) that.tui.toast('初始化失败', res)
this.isShowSearch = true
uni.hideLoading();
} }
}) })
@ -194,10 +160,16 @@
that.jiantingshebei() that.jiantingshebei()
}, },
fail(err) { fail(err) {
that.tui.toast('获取设备失败', res) that.tui.toast('获取设备失败', err)
this.isShowSearch = true
uni.hideLoading();
} }
}) })
}, },
//停止搜索蓝牙设备
stopSearchBtnTap(){
wx.stopBluetoothDevicesDiscovery()
},
/** /**
* jiantingshebei方法,判断缓存中有没有deviceId等数据的时候, * jiantingshebei方法,判断缓存中有没有deviceId等数据的时候,
* 有: 直接拿到缓存中的deviceId等,直接请求 wx.createBLEConnection * 有: 直接拿到缓存中的deviceId等,直接请求 wx.createBLEConnection
@ -206,17 +178,20 @@
jiantingshebei(){ jiantingshebei(){
let that = this; let that = this;
if(uni.getStorageSync('deviceId')){ if(uni.getStorageSync('deviceId')){
console.log("111")
that.lianejieshebei(uni.getStorageSync('deviceId')) that.lianejieshebei(uni.getStorageSync('deviceId'))
}else{ }else{
console.log("222")
uni.hideLoading();
this.isShowSearch = true
// 监听扫描到新设备事件 // 监听扫描到新设备事件
wx.onBluetoothDeviceFound((res4) => { //333 wx.onBluetoothDeviceFound((res4) => { //333
console.log('监听新设备列表,拿到想要的就自动停止', res4) console.log('监听新设备列表,拿到想要的就自动停止', res4)
res4.devices.forEach((device) => { res4.devices.forEach((device) => {
if(device.name == 'QR380A-6824_Ble'){ //搜索到需要的设备 if (!device.name && !device.localName) {
// 找到要搜索的设备后,及时停止扫描 return
wx.stopBluetoothDevicesDiscovery() //444
that.lianejieshebei(device.deviceId);
} }
that.devicesList.push(device)
}) })
}) })
} }
@ -233,6 +208,8 @@
}, },
fail: (res) => { fail: (res) => {
that.tui.toast('连接设备失败', res) that.tui.toast('连接设备失败', res)
this.isShowSearch = true
uni.hideLoading();
} }
}) })
}, },
@ -247,6 +224,8 @@
}, },
fail: (res) => { fail: (res) => {
that.tui.toast('获取服务失败', res) that.tui.toast('获取服务失败', res)
this.isShowSearch = true
uni.hideLoading();
} }
}) })
}, },
@ -258,51 +237,55 @@
serviceId:res5.services[0].uuid, // 上一步中找到的某个服务 serviceId:res5.services[0].uuid, // 上一步中找到的某个服务
success: (res6) => { success: (res6) => {
console.log('读写特征值成功',res6) console.log('读写特征值成功',res6)
that.xierushuju(deviceId,res5,res6) 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) => { fail: (res) => {
that.tui.toast('读写特征值失败', res) that.tui.toast('读写特征值失败', res)
this.isShowSearch = true
uni.hideLoading();
} }
}) })
}, },
xierushuju(deviceId,res5,res6){ xierushuju(){
let that = this; let that = this;
//写入数据 //写入数据
for (let i =0; i <res6.characteristics.length; i++) { if(this.defaultText == '打印小票'){
let item =res6.characteristics[i] that.$emit('onPrintSmall');
if (item.properties.write) { // 该特征值可写 }else if(this.defaultText == '确认打印'){
console.log('该特征值可写') that.$emit('goPrint');
uni.setStorageSync('deviceId', deviceId); }else{
uni.setStorageSync('serviceId', res5.services[0].uuid); that.$emit('onPrint');
uni.setStorageSync('characteristicId', item.uuid);
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;
}
} }
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) { _writeBLECharacteristicValue(buffer) {
let that = this;
wx.writeBLECharacteristicValue({ wx.writeBLECharacteristicValue({
deviceId: uni.getStorageSync('deviceId'), deviceId: uni.getStorageSync('deviceId'),
serviceId: uni.getStorageSync('serviceId'), serviceId: uni.getStorageSync('serviceId'),
@ -314,101 +297,14 @@
}, },
fail(res) { fail(res) {
console.log('writeBLECharacteristicValue fail', res) console.log('writeBLECharacteristicValue fail', res)
if(uni.getStorageSync('deviceId')){
console.log("111")
that.xierushuju()
}else{
that.jiantingshebei()
}
} }
}) })
},
onGetDevice(res) {
this.devicesList = res;
},
handleSearchClose() {
this.isShowSearch = false
},
handleSRRIChange(e) {
this.filterRSSI = e.detail.value
},
//开始搜索设备
searchBtnTap() {
blesdk.startBluetoothDevicesDiscovery();
this.isSearching = true;
blesdk.onfindBlueDevices(this.onGetDevice)
},
//停止搜索设备
stopSearchBtnTap() {
blesdk.stopBlueDevicesDiscovery();
this.isSearching = false;
},
//点击连接设备
handleConnectDevice(device) {
let deviceId = device.deviceId;
let name = device.name;
this.deviceId = deviceId;
uni.setStorageSync('deviceId', deviceId);
uni.onBLEConnectionStateChange(function(res) {
if (res.connected) {
// plus.nativeUI.toast('设备'+ res.deviceId + '已连接',{
// verticalAlign:'center'
// })
} else {
// plus.nativeUI.toast('设备'+ res.deviceId + '已断开连接',{
// verticalAlign:'center'
// })
}
})
blesdk.createBLEConnection(deviceId, this.onConnectSuccess, this.onConnectFail);
this.devicesList == []
},
onConnectSuccess(res) {
this.stopSearchBtnTap()
uni.setStorageSync('ok', true)
blesdk.getBLEDeviceServices(this.deviceId, this.onGetServicesSuccess, this.onGetServicesFail);
},
onConnectFail(err) {
blesdk.closeBLEConnection(uni.getStorageSync('deviceId'));
this.isShowSearch = true
uni.removeStorageSync('deviceId')
uni.removeStorageSync('serviceId')
uni.removeStorageSync('writeId')
uni.removeStorageSync('readId')
// uni.
},
onGetServicesSuccess(res) {
this.services = res.serviceId;
blesdk.getDeviceCharacteristics(uni.getStorageSync('deviceId') ? uni.getStorageSync('deviceId') : this
.deviceId, uni.getStorageSync('services') ? uni.getStorageSync('services') : this.services, this
.onGetCharacterSuccess, this.onGetCharacterFail);
},
onGetServicesFail(err) {},
onGetCharacterSuccess(res) {
this.serviceId = res.serviceId;
this.writeId = res.writeId;
this.readId = res.readId;
uni.setStorageSync('serviceId', res.serviceId)
uni.setStorageSync('writeId', res.writeId)
uni.setStorageSync('readId', res.readId)
this.isShowSearch = false;
this.handlePrintTap()
},
onGetCharacterFail(err) {
},
onPrintSuccess() {
console.log("打印完成22222")
this.isPrinting = false;
this.$u.route('/pages/toll/toll', {
roomcode: this.roomcode,
CustName: this.CustName,
id: this.roomid
})
this.$emit('onPrintSuccess')
},
onPrintFail() {
this.isPrinting = false;
} }
} }
} }

Loading…
Cancel
Save