Browse Source

打印机

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

41
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;
//打印成功跳转页面
@ -199,14 +197,8 @@
})
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
}else{
console.log("有设备连接")
if(uni.getStorageSync('deviceId') != '' && uni.getStorageSync('serviceId') != ''){
console.log('第二次进入')
let datalen=20;
// #ifdef APP-PLUS
if (plus.os.name != 'Android')
@ -222,22 +214,33 @@
let buffer = gbk.strToGBKByte(this.bufferData)
let opt = {
deviceId: this.deviceId,
serviceId: this.serviceId,
characteristicId: this.writeId,
deviceId: uni.getStorageSync('deviceId'),
serviceId: uni.getStorageSync('serviceId'),
characteristicId: uni.getStorageSync('writeId'),
value:buffer,
lasterSuccess: this.onPrintSuccess,
onceLength:datalen
}
console.log("开始打印了")
console.log(opt)
blesdk.sendDataToDevice(opt);
this.isPrinting = false;
//打印成功跳转页面
}
})
}else{
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)
@ -299,6 +301,7 @@
onGetServicesSuccess(res){
console.log('获取服务',res)
this.services = res.serviceId;
blesdk.getDeviceCharacteristics(this.deviceId, this.services, this.onGetCharacterSuccess, this.onGetCharacterFail);
},
onGetServicesFail(err){
@ -309,6 +312,9 @@
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;
},
onGetCharacterFail(err){
@ -444,6 +450,7 @@
border-bottom: 1upx solid #ebebeb;
padding:10upx 0;
box-sizing: border-box;
z-index: 500;
&:nth-last-child(1){
border-bottom: none;
}

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

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

Loading…
Cancel
Save