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

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

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

Loading…
Cancel
Save