Browse Source

打印机

master
wangfukang 3 years ago
parent
commit
1c94773017
  1. 101
      components/kk-printer/index.vue
  2. 3
      components/kk-printer/utils/bluetoolth.js

101
components/kk-printer/index.vue

@ -32,7 +32,7 @@
</view>
<view class="" v-else>
<view class="kk-devices-item" v-for="(item,index) in filterDeviceList" :key="index" @tap="handleConnectDevice(item)">
<view class="name">
<view class="name" style="color:#000;">
<text>设备名称:</text>
<text>{{item.name?item.name:'未命名'}}</text>
</view>
@ -128,7 +128,6 @@
let filterDevices1 = devices.filter((item)=>{
return item.RSSI > rssi
})
console.log(filterDevices1)
// 按名字过滤
let filterDevices2
if(name!=''){
@ -190,7 +189,6 @@
onceLength:datalen
}
console.log("开始打印了")
console.log(opt)
blesdk.sendDataToDevice(opt);
this.isPrinting = false;
//打印成功跳转页面
@ -198,46 +196,51 @@
})
return
}
//若没有已连接设备,弹框搜索设备
console.log(res,this.deviceId,this.serviceId,this.writeId,this.bufferData,this.onPrintSuccess,'res,this.deviceId,this.serviceId,this.writeId,this.bufferData,this.onPrintSuccess’')
if(res.devices.length == 0){
console.log("没有设备连接")
this.iosNo = true
this.isShowSearch = true
}
if(uni.getStorageSync('deviceId') != '' && uni.getStorageSync('serviceId') != ''){
console.log('第二次进入')
let datalen=20;
// #ifdef APP-PLUS
if (plus.os.name != 'Android')
{
datalen=180;
}
// #endif
this.isPrinting = true;
this.$emit('onPrint');
this.$nextTick(()=>{
if(this.bufferData!=''){
let buffer = gbk.strToGBKByte(this.bufferData)
let opt = {
deviceId: uni.getStorageSync('deviceId'),
serviceId: uni.getStorageSync('serviceId'),
characteristicId: uni.getStorageSync('writeId'),
value:buffer,
lasterSuccess: this.onPrintSuccess,
onceLength:datalen
}
console.log("开始打印了")
blesdk.sendDataToDevice(opt);
this.isPrinting = false;
//打印成功跳转页面
}
})
}else{
console.log("有设备连接")
let datalen=20;
// #ifdef APP-PLUS
if (plus.os.name != 'Android')
{
datalen=180;
}
// #endif
this.isPrinting = true;
this.$emit('onPrint');
this.$nextTick(()=>{
if(this.bufferData!=''){
let buffer = gbk.strToGBKByte(this.bufferData)
let opt = {
deviceId: this.deviceId,
serviceId: this.serviceId,
characteristicId: this.writeId,
value:buffer,
lasterSuccess: this.onPrintSuccess,
onceLength:datalen
}
console.log("开始打印了")
console.log(opt)
blesdk.sendDataToDevice(opt);
this.isPrinting = false;
//打印成功跳转页面
}
})
}
console.log('首次进入')
console.log("没有设备连接")
this.iosNo = true
this.isShowSearch = true
}
// //若没有已连接设备,弹框搜索设备
// if(res.devices.length == 0){
// }else{
// }
}).catch((err)=>{
blesdk.catchToast(err);
})
@ -271,8 +274,7 @@
let deviceId = device.deviceId;
let name = device.name;
this.deviceId = deviceId;
console.log('deviceId',this.deviceId)
// uni.setStorageSync('k_curDeviceID',deviceId);
uni.setStorageSync('deviceId',deviceId);
// uni.setStorageSync('k_curDeviceName',name);plus.bluetooth.onBluetoothDeviceFound
uni.onBLEConnectionStateChange(function(res){
console.log('连接',res)
@ -298,7 +300,8 @@
},
onGetServicesSuccess(res){
console.log('获取服务',res)
this.services = res.serviceId;
this.services = res.serviceId;
blesdk.getDeviceCharacteristics(this.deviceId, this.services, this.onGetCharacterSuccess, this.onGetCharacterFail);
},
onGetServicesFail(err){
@ -308,7 +311,10 @@
console.log('获取特征值成功',res)
this.serviceId = res.serviceId;
this.writeId = res.writeId;
this.readId = res.readId;
this.readId = res.readId;
uni.setStorageSync('serviceId',res.serviceId)
uni.setStorageSync('writeId',res.writeId)
uni.setStorageSync('readId',res.readId)
this.isShowSearch = false;
},
onGetCharacterFail(err){
@ -443,7 +449,8 @@
width:100%;
border-bottom: 1upx solid #ebebeb;
padding:10upx 0;
box-sizing: border-box;
box-sizing: border-box;
z-index: 500;
&:nth-last-child(1){
border-bottom: none;
}

3
components/kk-printer/utils/bluetoolth.js

@ -52,7 +52,8 @@ export function onfindBlueDevices(getDevices) {
//监听寻找到新设备的事件
uni.onBluetoothDeviceFound((devices)=>{
//获取在蓝牙模块生效期间所有已发现的蓝牙设备
uniAsyncPromise('getBluetoothDevices').then((res) => {
uniAsyncPromise('getBluetoothDevices').then((res) => {
console.log('res这里',res.devices)
getDevices && getDevices(res.devices);
});
});

Loading…
Cancel
Save