uni-app微信小程序使用佳博蓝牙打印机

这篇具有很好参考价值的文章主要介绍了uni-app微信小程序使用佳博蓝牙打印机。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.佳博打印js copy到项目里
2.需要打印的vue页面引入js

var app = getApp();
var esc = require("@/viewsA/pages/qualityTest/js/esc.js");
var encode = require("@/viewsA/pages/qualityTest/js/encoding.js");

3.打印数据初始化

onLoad() {
//放data会报错,所以需要在写this.setData方法初始化
	this.setData({
	  buffSize: list,
	  oneTimeData: list[0],
	  printNum: numList,
	  printerNum: 2
	})
}

4.打印按钮事件

//先判断是否链接打印蓝牙
if(!app.$vm.BLEInformation.deviceId){
	uni.showModal({
		title: '提示',
		content: '打印机未连接,请前往连接',
		confirmText: '前往',
		success(res) {
			if (res.confirm) {
				//无连接跳转链接页面链接
				uni.navigateTo({
					url:'/viewsA/pages/qualityTest/bleConner'
				})
			} else if (res.cancel) {
	  
			}
		}
	})
}else{
	//已连接在做链接处理
	this.receiptTest();
}

蓝牙列表连接页面

<template>
	<view class="bleConner">
		<button class="button" hover-class="hover" @click="startSearch" :loading="isScanning">搜索蓝牙设备</button>
		<text class=".td">(Android8.0+系统需开启定位)</text>
		<scroll-view v-if="list.length > 0" class="device_list" scroll-y scroll-with-animation>
			<view
				v-for="item in list"
				:data-title="item.deviceId"
				:data-name="item.name"
				:data-advertisData="item.advertisServiceUUIDs"
				:key="item.deviceId"
				@click="bindViewTap"
				class="item"
				hover-class="item_hover"
				
			>
				<view>{{ item.name }}</view>
			</view>
		</scroll-view>
		<view v-else class="noList">请检查打印机设备是否开机</view>
	</view>
</template>

<script>
var app = getApp();
export default {
	data() {
		return {
			list: [],
			services: [],
			serviceId: 0,
			writeCharacter: false,
			readCharacter: false,
			notifyCharacter: false,
			isScanning: false
		};
	},
	onLoad() {
		app.$vm.BLEInformation.platform = app.getPlatform();
	},
	methods: {
		//搜索设备
		startSearch: function() {
			var that = this;
			wx.openBluetoothAdapter({
				success: function(res) {
					wx.getBluetoothAdapterState({
						success: function(res) {
							console.log('openBluetoothAdapter success', res);
							if (res.available) {
								if (res.discovering) {
									wx.stopBluetoothDevicesDiscovery({
										success: function(res) {
											console.log(res);
										}
									});
								} else {
									// that.startBluetoothDevicesDiscovery()
									that.getBluetoothDevices();
								}
								// that.checkPemission()
							} else {
								wx.showModal({
									title: '提示',
									content: '本机蓝牙不可用',
									showCancel: false
								});
							}
						}
					});
				},
				fail: function() {
					// if (res.errCode === 10001) {
					//   wx.onBluetoothAdapterStateChange(function (res) {
					//     console.log('onBluetoothAdapterStateChange', res)
					//     if (res.available) {
					//       this.startBluetoothDevicesDiscovery()
					//     }
					//   })
					// }

					wx.showModal({
						title: '提示',
						content: '蓝牙初始化失败,请到设置打开蓝牙',
						showCancel: false
					});
				}
			});
		},
		checkPemission: function() {
			//android 6.0以上需授权地理位置权限
			var that = this;
			var platform = app.$vm.BLEInformation.platform;
			if (platform == 'ios') {
				app.globalData.platform = 'ios';
				that.getBluetoothDevices();
			} else if (platform == 'android') {
				app.globalData.platform = 'android';
				console.log(app.getSystem().substring(app.getSystem().length - (app.getSystem().length - 8), app.getSystem().length - (app.getSystem().length - 8) + 1));
				if (app.getSystem().substring(app.getSystem().length - (app.getSystem().length - 8), app.getSystem().length - (app.getSystem().length - 8) + 1) > 5) {
					wx.getSetting({
						success: function(res) {
							console.log(res);
							if (!res.authSetting['scope.userLocation']) {
								wx.authorize({
									scope: 'scope.userLocation',
									complete: function(res) {
										that.getBluetoothDevices();
									}
								});
							} else {
								that.getBluetoothDevices();
							}
						}
					});
				}
			}
		},
		getBluetoothDevices: function() {
			//获取蓝牙设备信息
			var that = this;
			console.log('start search');
			wx.showLoading({
				title: '正在加载',
				icon: 'loading'
			});
			that.setData({
				isScanning: true
			});
			wx.startBluetoothDevicesDiscovery({
				// services: ['E7810A71-73AE-499D-8C15-FAA9AEF0C3F2'],
				// E7810A71-73AE-499D-8C15-FAA9AEF0C3F2
				success: function(res) {
					console.log(res);
					setTimeout(function() {
						wx.getBluetoothDevices({
							success: function(res) {
								console.log(res)
								var devices = [];
								var num = 0;
								for (var i = 0; i < res.devices.length; ++i) {
									if (res.devices[i].name != '未知设备') {
										devices[num] = res.devices[i];
										num++;
									}
								}
								that.setData({
									list: devices,
									isScanning: false
								});
								wx.hideLoading();
								wx.stopPullDownRefresh();
								wx.stopBluetoothDevicesDiscovery({
									success: function(res) {
										console.log('停止搜索蓝牙');
									}
								});
							}
						});
					}, 5000);
				}
			});
		},
		bindViewTap: function(e) {
			var that = this;
			wx.stopBluetoothDevicesDiscovery({
				success: function(res) {
					console.log(res);
				}
			});
			that.setData({
				serviceId: 0,
				writeCharacter: false,
				readCharacter: false,
				notifyCharacter: false
			});
			console.log(e.currentTarget.dataset.title);
			wx.showLoading({
				title: '正在连接'
			});
			wx.createBLEConnection({
				deviceId: e.currentTarget.dataset.title,
				success: function(res) {
					console.log(res);
					app.$vm.BLEInformation.deviceId = e.currentTarget.dataset.title;
					that.getSeviceId();
				},
				fail: function(e) {
					wx.showModal({
						title: '提示',
						content: '连接失败',
						showCancel: false
					});
					console.log(e);
					wx.hideLoading();
				},
				complete: function(e) {
					console.log(e);
				}
			});
		},
		getSeviceId: function() {
			var that = this;
			var platform = app.$vm.BLEInformation.platform;
			console.log(app.$vm.BLEInformation.deviceId);
			wx.getBLEDeviceServices({
				deviceId: app.$vm.BLEInformation.deviceId,
				success: function(res) {
					console.log(res);
					// var realId = ''
					// if (platform == 'android') {
					//   // for(var i=0;i<res.services.length;++i){
					//   // var item = res.services[i].uuid
					//   // if (item == "0000FEE7-0000-1000-8000-00805F9B34FB"){
					//   realId = "0000FEE7-0000-1000-8000-00805F9B34FB"
					//   //       break;
					//   //     }
					//   // }
					// } else if (platform == 'ios') {
					//   // for(var i=0;i<res.services.length;++i){
					//   // var item = res.services[i].uuid
					//   // if (item == "49535343-FE7D-4AE5-8FA9-9FAFD205E455"){
					//   realId = "49535343-FE7D-4AE5-8FA9-9FAFD205E455"
					//   // break
					//   // }
					//   // }
					// }
					// app.$vm.BLEInformation.serviceId = realId
					that.setData({
						services: res.services
					});
					that.getCharacteristics();
				},
				fail: function(e) {
					console.log(e);
				},
				complete: function(e) {
					console.log(e);
				}
			});
		},
		getCharacteristics: function() {
			var that = this;
			var list = that.services;
			var num = that.serviceId;
			var write = that.writeCharacter;
			var read = that.readCharacter;
			var notify = that.notifyCharacter;
			wx.getBLEDeviceCharacteristics({
				deviceId: app.$vm.BLEInformation.deviceId,
				serviceId: list[num].uuid,
				success: function(res) {
					console.log(res);
					for (var i = 0; i < res.characteristics.length; ++i) {
						var properties = res.characteristics[i].properties;
						var item = res.characteristics[i].uuid;
						if (!notify) {
							if (properties.notify) {
								app.$vm.BLEInformation.notifyCharaterId = item;
								app.$vm.BLEInformation.notifyServiceId = list[num].uuid;
								notify = true;
							}
						}
						if (!write) {
							if (properties.write) {
								app.$vm.BLEInformation.writeCharaterId = item;
								app.$vm.BLEInformation.writeServiceId = list[num].uuid;
								write = true;
							}
						}
						if (!read) {
							if (properties.read) {
								app.$vm.BLEInformation.readCharaterId = item;
								app.$vm.BLEInformation.readServiceId = list[num].uuid;
								read = true;
							}
						}
					}
					if (!write || !notify || !read) {
						num++;
						that.setData({
							writeCharacter: write,
							readCharacter: read,
							notifyCharacter: notify,
							serviceId: num
						});
						if (num == list.length) {
							wx.showModal({
								title: '提示',
								content: '找不到该读写的特征值',
								showCancel: false
							});
						} else {
							that.getCharacteristics();
						}
					} else {
						wx.showToast({
							title: '连接成功'
						});
						that.openControl();
					}
				},
				fail: function(e) {
					console.log(e);
				},
				complete: function(e) {
					console.log('write:' + app.$vm.BLEInformation.writeCharaterId);
					console.log('read:' + app.$vm.BLEInformation.readCharaterId);
					console.log('notify:' + app.$vm.BLEInformation.notifyCharaterId);
				}
			});
		},
		openControl: function() {
			//连接成功返回主页
			uni.navigateBack();
		},
		setData: function(obj) {
			let that = this;
			let keys = [];
			let val, data;
			Object.keys(obj).forEach(function(key) {
				keys = key.split('.');
				val = obj[key];
				data = that.$data;
				keys.forEach(function(key2, index) {
					if (index + 1 == keys.length) {
						that.$set(data, key2, val);
					} else {
						if (!data[key2]) {
							that.$set(data, key2, {});
						}
					}
					data = data[key2];
				});
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.bleConner {
	.button {
		margin-top: 20px;
		width: 90%;
		background: #456ae8;
		color: white;
		border-radius: 10rpx;
		font-size: 32upx;
		font-weight: 700;
		color: #ffffff;
		height: 96upx;
		line-height: 96upx;
	}

	/* 按下变颜色 */
	.hover {
		background: #dcdcdc;
	}
	.td {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 24upx;
		color: #7E7E8F;
		font-size: 24upx;
	}
	.device_list {
		height: auto;
		margin: 24rpx;
		border: 1upx solid #f4f4f4;
		border-radius: 10upx;
		width: auto;
	}
	.item {
		display: block;
		border-bottom: 1px solid #F4F4F4;
		padding: 24upx;
		color: #39394F;
	}
	.item_hover {
		background-color: rgba(0, 0, 0, 0.1);
	}
	.noList{
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 24upx;
		color: #7E7E8F;
		font-size: 24upx;
	}
}
</style>

已连接处打印方法文章来源地址https://www.toymoban.com/news/detail-657429.html

receiptTest() { //票据测试
	var that = this;
	var canvasWidth = that.canvasWidth
	var canvasHeight = that.canvasHeight
	var command = esc.jpPrinter.createNew()
	command.init() //初始化打印机
	command.setCutter();
	// 标题
	command.setSelectJustification(0);
	command.setCharacterSize(17); //设置倍高倍宽
	command.setText("大洋智慧快检收费单");
	command.setPrint(); //打印并换行
	command.setPrint(); //打印并换行
	// 虚线
	command.setCharacterSize(0); //设置倍高倍宽
	command.setSelectJustification(0)
	command.setText("----------------------------------------------");
	command.setPrint() //打印并换行
	command.setPrint() //打印并换行
	// 订单号
	command.setSelectJustification(0)
	command.setText("订单号:");
	command.setAbsolutePrintPosition(158);
	command.setText(that.orderInfo.order_no);
    command.setPrint() //打印并换行
	command.setPrint() //打印并换行
	// 车牌号
	command.setSelectJustification(0)
	command.setText("车牌号:");
	command.setAbsolutePrintPosition(158);
	command.setText(that.submission_goods_car_number);
	command.setPrint() //打印并换行
	command.setPrint() //打印并换行
	// 检测商品
	command.setSelectJustification(0)
	command.setText("检测商品:");
	command.setAbsolutePrintPosition(158);
	// command.setText('#' + that.testDetail.day_num + that.testDetail.submission_goods_name);
	command.setText(that.printGoodsName);
	command.setPrint() //打印并换行
	command.setPrint() //打印并换行
	// 支付方式
	command.setSelectJustification(0)
	command.setText("支付方式:");
	command.setAbsolutePrintPosition(158);
	command.setText(that.payType);
	command.setPrint() //打印并换行
	command.setPrint() //打印并换行
	// 支付时间
	command.setSelectJustification(0)
	command.setText("支付时间:");
	command.setAbsolutePrintPosition(158);
	command.setText(that.testDetail.pay_time);
	command.setPrint() //打印并换行
	command.setPrint() //打印并换行
	// 支付金额
	command.setSelectJustification(0)
	command.setText("支付金额:");
	command.setAbsolutePrintPosition(158);
	command.setCharacterSize(17); //设置倍高倍宽
	// command.setText("RMB "+that.testDetail.price);
	command.setText("RMB "+that.printPrice);
	command.setPrint() //打印并换行
	command.setPrint() //打印并换行
	// 虚线
	command.setCharacterSize(0); //设置倍高倍宽
	command.setSelectJustification(0)
	command.setText("----------------------------------------------");
	command.setPrint() //打印并换行
	command.setPrint() //打印并换行
	// 送检人签名
	command.setSelectJustification(0)
	command.setText("送检人签名:");
	command.setPrint() //打印并换行
	command.setPrint() //打印并换行
	// 虚线
	command.setSelectJustification(0)
	command.setText("----------------------------------------------");
	command.setPrint() //打印并换行
	command.setPrint() //打印并换行
	// 开票员 开票时间
	command.setSelectJustification(0)
	command.setText("开票员:" + that.adminName);
	command.setAbsolutePrintPosition(220);
	command.setText("开票时间:" + that.printTime);
	command.setPrint() //打印并换行
	command.setPrint() //打印并换行
	// 北京大洋路农副产品有限公司
	command.setSelectJustification(0)
	command.setText("北京大洋路农副产品有限公司");
	command.setPrint() //打印并换行
	command.setPrint()
	command.setPrint()
	command.setPrint()
	command.setPrint()
	command.setCutter();
	// 调用出票接口
	let idStr = '';
	if(that.payType == '现金支付'){
		if(that.selCashList.length > 0){
			idStr = that.selCashList.map(item=> item.check_object_id).join(",");
		}else{
			idStr = that.testDetail.check_object_id;
		}
	}
	if(that.payType == '线上支付'){
		idStr = that.payList.map(item=> item.check_object_id).join(",");
	}
	let data = {
		check_object_ids : idStr
	}
	API.printMore(data).then(res => {
		if (res.status == 200) {
			
		} else {
			
		}
	})
	that.prepareSend(command.getData())//准备发送数据
},

prepareSend: function(buff) { //准备发送,根据每次发送字节数来处理分包数量
  //console.log(buff)
  var that = this
  var time = that.oneTimeData
  var looptime = parseInt(buff.length / time);
  var lastData = parseInt(buff.length % time);
  //console.log(looptime + "---" + lastData)
  that.setData({
    looptime: looptime + 1,
    lastData: lastData,
    currentTime: 1,
  })
  that.Send(buff)
},
Send: function(buff) { //分包发送
	var that = this
	var currentTime = that.currentTime
	var loopTime = that.looptime
	var lastData = that.lastData
	var onTimeData = that.oneTimeData
	var printNum = that.printerNum
	var currentPrint = that.currentPrint
	var buf
	var dataView
	if (currentTime < loopTime) {
		buf = new ArrayBuffer(onTimeData)
		dataView = new DataView(buf)
		for (var i = 0; i < onTimeData; ++i) {
			dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
		}
	} else {
		buf = new ArrayBuffer(lastData)
		dataView = new DataView(buf)
		for (var i = 0; i < lastData; ++i) {
			dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
		}
	}
	wx.writeBLECharacteristicValue({
		deviceId: app.$vm.BLEInformation.deviceId,
		serviceId: app.$vm.BLEInformation.writeServiceId,
		characteristicId: app.$vm.BLEInformation.writeCharaterId,
		value: buf,
		success: function(res) {
			
			if (currentTime <= loopTime) {
				// wx.showLoading({
				//   title: '传输中...',
				// })
			} else {
				wx.showToast({
				  title: '已打印第' + currentPrint + '张成功',
				})
			}
			//console.log(res)
		},
		fail: function(e) {
			wx.showToast({
			  title: '打印第' + currentPrint + '张失败',
			  icon: 'none',
			})
			
			//console.log(e)
		},
		complete: function() {
			currentTime++
			if (currentTime <= loopTime) {
				that.setData({
					currentTime: currentTime
				})
				that.Send(buff)
			} else {
				if (currentPrint == printNum) {
					that.setData({
						looptime: 0,
						lastData: 0,
						currentTime: 1,
						isReceiptSend: false,
						currentPrint: 1
					})
				} else {
					currentPrint++
					that.setData({
						currentPrint: currentPrint,
						currentTime: 1,
					})
					that.Send(buff)
				}
			}
		}
	})
},
setData: function(obj) {
	let that = this;
	let keys = [];
	let val, data;
	Object.keys(obj).forEach(function(key) {
		keys = key.split(".");
		val = obj[key];
		data = that.$data;
		keys.forEach(function(key2, index) {
			if (index + 1 == keys.length) {
				that.$set(data, key2, val);
			} else {
				if (!data[key2]) {
				that.$set(data, key2, {});
				}
			}
		  data = data[key2];
		});
	});
},

到了这里,关于uni-app微信小程序使用佳博蓝牙打印机的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • uni-app:vue3 + uni-app 在微信小程序中无法使用app.component全局注册组件

    按上文中的代码执行后,会发现在微信小程序开发中全局注册的组件是无法显示的,这是uniapp的一个未解决bug, 在uniapp中出了可以通过vue实例的component方法注册全局组件外,uniapp支持另一种全局注册的方式,就是通过 easycom 扫描注册,步骤如下 easycom 的扫描流程是:通过代码

    2024年02月16日
    浏览(27)
  • uni-app 微信小程序 使用mixins设置分享 onShareAppMessage

    参考链接:https://www.jianshu.com/p/844018ca174f 这样设置后,右上角三个点的分享就可以分享了

    2024年02月12日
    浏览(16)
  • uni-app 使用webview加载H5打开微信小程序

    最近公司有个需求要求在app里点击一个功能打开小程序,并且关闭小程序回到app,模仿平安保险app。 毕竟我也是刚学习uni-app,找了很多资料,找到了一个天天外链的网站可以生成一个小程序的链接,使用uni的webview去加载这个链接,很好,需求满足,但是收费,那能不能自己

    2023年04月18日
    浏览(24)
  • 微信小程序使用uni-app开发小程序及部分功能实现详解心得

    目录 一、uni-app 1、简介 2、开发工具 3、新建 uni-app项目 4、把项目运行到微信开发者工具 二、实现tabBar效果 1、创建tabBar页面 2、配置tabBar 1、创建分包目录 2、在 pages.json 文件中配置 3、创建分包页面 四、公用方法封装 五、搜索功能 1、搜索组件 2、搜索建议实现 3、本地存储

    2024年02月11日
    浏览(19)
  • 微信小程序一键登录功能,使用uni-app和springboot(JWT鉴权)

    目录 概述 微信登录接口说明  关于获取微信用户的信息 前端代码(uni-app) 后端代码(SpringBoot) 配置文件:application.yml  配置文件:Pom.xml  类:WeChatModel    类:WeChatSessionModel  类:UserInfoController 业务层实现类:UserInfoServiceImpl 工具类:JWTUtils 拦截器配置-自定义拦截器

    2024年02月09日
    浏览(21)
  • uni-app:使用 Painter 在微信小程序将当前页面保存为图片

    手机截屏 Painter 实现 方式一:Painter Painter 是一个微信小程序组件,具体介绍和 API 请参考:GitHub文档。 在 GitHub 下载下来之后只需要将 components 下的 painter 文件夹放到项目根目录下的 wxcomponents 文件夹即可。然后就是如何在 uni-app 中使用微信小程序形式的组件,其实很简单,

    2024年02月12日
    浏览(17)
  • uni-app使用HBuilder X工具和微信小程序工具开发微信小程序

    选择uni-app的原因是什么 需要使用到的工具 关于HBuilder X工具和微信小程序工具的介绍  怎么下载HBuilder X工具和微信小程序工具  如何使用HBuilderX工具 如何使用微信小程序工具  结尾 什么是uni-app uni-app是一个使用vue.js开发所有前端应用的框架,开发者编写一套代码,可发布到

    2024年02月08日
    浏览(21)
  • 【微信小程序】使用uni-app——开发首页搜索框导航栏(可同时兼容APP、H5、小程序)

    目录 前言 App、H5效果 小程序效果 一、兼容APP、H5的方式 二、兼容小程序 三、实现同时兼容 首页都会提供一个搜索框给到客户,让客户自己去搜索自己想要的内容,这里就需要导航栏,来实现搜索页面的跳转,效果如下 在常见titleNView配置代码示例中可以看到基本样式的代码

    2024年02月03日
    浏览(18)
  • 【uni-app】uni项目打包微信小程序中使用 ECharts (mpvue-echarts)

    在小程序里画图表,uniapp 不想引入 u-charts怎么办,个人还是喜欢用echarts 先看成品图 说明: 示例下载-- 完整dom 如果你想要在单页面渲染多个图标可以看看:这里(相关文章传送门) 原生小程序使用的是 echarts-for-weixin ,具体地址如下: https://github.com/ecomfe/echarts-for-weixin 想在

    2024年02月07日
    浏览(12)
  • uni-app+vue2 微信小程序 使用canvas绘制折线图/波形图

      接口返回一个数组,每一项均是一个数字,代表着y坐标,x坐标需自己处理。 我的数据是1024个浮点数,在-10到10之间 波形图需要xy轴缩放功能,用c3的 transform: scale()是不行的,至少会失真。 然后背景的格子,我这里是每个格子要求100个点,初始缩放下是11个格子,10条线(

    2024年04月26日
    浏览(20)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包