uniapp调用蓝牙打印

介绍

使用支持BLE的蓝牙打印机进行自定义模板打印,通过安卓apk使用uniapp开发进行自定义模板批量打印功能。纯js开发,无需额外本地模块支持。

一、准备

  1. 安卓使用蓝牙用到了定位服务,需要在uniapp的manifest.json配置App权限配置,给location和bluetooth的权限,看着勾就行了。
  2. 下载附件给到的api.rar文件,包含了基础调用的js。

二、调用代码

<template>
		<view >
			<button @click="searchBle">搜索蓝牙</button>
			<button @click="printLabel">已连接进行打印</button>
			<view style="margin-top: 30upx;" :key="index" v-for="(item,index) in devices">
				<button style="width: 400upx; color: #0081FF;" @click="onConn(item)">{{item.name}}</button>
			</view>
		</view>
	</view>
</template>

<script>
	import printService from '@/api/print.js'
	export default {
		data() {
			return {
				devices: [],
				currDev: null,
				deviceId:null,
				serviceId:null,
				characteristicId : null,
				
			}
		},
		methods: {
			printLabel(){
				// 后台调用数据进行填充
				let dataArr = [1,2];
				if(!printService.openBlueToothLinkAndPrint(this.deviceId,this.serviceId,this.characteristicId,dataArr)){
					this.$modal.msg("请先连接蓝牙!")
				};
			},
			// 打开蓝牙模块开始搜索
			searchBle() {
				var that = this
				uni.openBluetoothAdapter({
					success(res) {
						console.log("打开 蓝牙模块")
						console.log(res)
						that.onDevice()
						uni.getBluetoothAdapterState({
							success: function(res) {
								console.log(res)
								if (res.available) {
									if (res.discovering) {
										that.stopFindBule()
									}
									//搜索蓝牙
									//开始搜寻附近的蓝牙外围设备
									console.log("开始搜寻附近的蓝牙外围设备")
									uni.startBluetoothDevicesDiscovery({
										success(res) {
											console.log(res)
										}
									})
								} else {
									console.log('本机蓝牙不可用')
								}
							},
						})
					}
				})
			},
			onDevice(){
				console.log("监听寻找到新设备的事件---------------")
				var that = this
				//监听寻找到新设备的事件
				uni.onBluetoothDeviceFound(function(devices) {
					var re = JSON.parse(JSON.stringify(devices))
					let name = re.devices[0].name
					// 打印机设备过滤名称前缀
					let preName = "";
					if (name != "未知设备" && name.indexOf(preName) != -1) {
						let deviceId = re.devices[0].deviceId
						that.devices.push({
							name: name,
							deviceId: deviceId,
							services: []
						})
						that.stopFindBule();
						return;
					}
				})
			},
			// 停止搜寻附近的蓝牙外围设备
			stopFindBule() {
				uni.stopBluetoothDevicesDiscovery({
					success(res) {
						console.log(res)
					}
				})
			},
			onConn(item) {
				var that = this
				console.log("连接蓝牙---------------" + item.deviceId)
				that.deviceId = item.deviceId;
				let deviceId = item.deviceId;
				uni.createBLEConnection({
					deviceId: deviceId,
					complete(res) {
						if (res.errMsg == "createBLEConnection:ok") {
							that.$modal.msg("连接蓝牙[" + item.name + "]成功")
							that.connId = deviceId;
							that.currDev = item
							that.$modal.msg("连接中请等待!")
							setTimeout(function() {
								that.getBLEServices(deviceId)
								that.$modal.msg("连接成功!")
							}, 2000)
						} else {
							console.log(res)
						}
						//连接成功 关闭搜索
						that.stopFindBule()
					},
				})
				
			},
			// 获取蓝牙所有服务
			getBLEServices(_deviceId) {
				var that = this;
				let deviceId = _deviceId
				uni.getBLEDeviceServices({
					deviceId: deviceId,
					complete(res) {
						if(that.serviceId != null){
							return
						}
						let serviceId = ""
						for (var s = 0; s < res.services.length; s++) {
							let serviceId = res.services[s].uuid
							uni.getBLEDeviceCharacteristics({
								deviceId: deviceId,
								serviceId: serviceId,
								success(res) {
									var re = JSON.parse(JSON.stringify(res))
									for (var c = 0; c < re.characteristics.length; c++) {
										if (re.characteristics[c].properties.write == true && that.characteristicId == null) {
											let uuid = re.characteristics[c].uuid
											that.serviceId = serviceId;
											that.characteristicId = uuid;
											console.log("that.serviceId"+that.serviceId)
											console.log("that.characteristicId"+that.characteristicId)
											return 
										}
									}
								}
							})
						}
					},
					fail(res) {
						console.log(res)
					},
				})
			},
	}
</script>


三、调试

在这里插入图片描述

连接安卓设备进行模拟调试,先点击《搜索蓝牙》,这里可以通过代码里的preName 过滤打印机,扫描到打印机后选择自己的打印机,然后显示连接成功后点击《已连接进行打印》即打印完成。

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值