uniapp连接蓝牙打印机(测试)

<template>
	<view class="content">
		<button @click="init()">test</button>
		<ul v-for="item in devicesLsit">
			<li class="ts" @click="createConnection(item.deviceId)">
				{{item.localName!=''?item.localName : item.deviceId}}
			</li>
		</ul>
		<button @click="print()">打印</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				devicesLsit: [],
				deviceId: '',
				serviceId: '',
				characteristicId: ''
			}
		},
		onLoad() {

		},
		methods: {
			init() {
				let that = this
				plus.bluetooth.openBluetoothAdapter({
					success: function(e) {

						console.log('open success: ' + JSON.stringify(e));
						that.searchStartBoolTList()

					},
					fail: function(e) {
						console.log('open failed: ' + JSON.stringify(e));
					}
				});
			},
			searchStartBoolTList() {
				let that = this
				plus.bluetooth.startBluetoothDevicesDiscovery({
					success: function(e) {
						that.getBList()
						console.log('start discovery success: ' + JSON.stringify(e));
					},
					fail: function(e) {

						console.log('start discovery failed: ' + JSON.stringify(e));
					}
				});
			},
			getBList() {
				let that = this
				plus.bluetooth.onBluetoothDeviceFound(function(e) {
					var devices = e.devices;
					console.log('device found: ' + e.length);
					for (var i in devices) {
						that.devicesLsit.push(devices[i])
						console.log(i + ': ' + JSON.stringify(devices[i]));
					}
				});
			},
			createConnection(deviceId) {
				uni.showLoading({
					title: '连接中...'
				})
				let that = this
				this.deviceId = deviceId
				plus.bluetooth.createBLEConnection({
					deviceId: deviceId,
					success: function(e) {
						uni.hideLoading()
						uni.showToast({
							duration: 2500,
							title: '连接成功'
						})
						that.stopBluetoothDiscovery()
						console.log('create connection success: ' + JSON.stringify(e));
					},
					fail: function(e) {
						console.log('create connection failed: ' + JSON.stringify(e));
					}
				});
			},
			stopBluetoothDiscovery() {
				plus.bluetooth.stopBluetoothDevicesDiscovery({
					success: function(e) {
						console.log('stop discovery success: ' + JSON.stringify(e));
						// plus.bluetooth.closeBluetoothAdapter({
						// 	success: function(e) {
						// 		console.log('close success: ' + JSON.stringify(e));
						// 	},
						// 	fail: function(e) {
						// 		console.log('close failed: ' + JSON.stringify(e));
						// 	}
						// });
					},
					fail: function(e) {
						console.log('stop discovery failed: ' + JSON.stringify(e));
					}
				});
			},
			//获取已连接蓝牙设备
			getConnectedDevices() {

				plus.bluetooth.getConnectedBluetoothDevices({
					success: function(e) {

						var devices = e.devices;
						console.log('connected devices success: ' + e.length);
						for (var i in devices) {
							console.log(i + ': ' + JSON.stringify(devices[i]));
						}
					},
					fail: function(e) {

						console.log('connected devices failed: ' + JSON.stringify(e));
					}
				});

			},
			//监听蓝牙连接状态
			listenerConnection() {
				plus.bluetooth.onBLEConnectionStateChange(function(e) {
					console.log('connection state changed: ' + JSON.stringify(e));
				});
			},
			writeCharacteristics(item, value) {
				plus.bluetooth.writeBLECharacteristicValue({
					deviceId: item.deviceId,
					serviceId: item.serviceId,
					characteristicId: item.characteristicId,
					value: value,
					success: function(e) {

						console.log('write characteristics success: ' + JSON.stringify(e));
					},
					fail: function(e) {
						console.log('write characteristics failed: ' + JSON.stringify(e));
					}
				});
			},
			getServices() {
				let that = this
				plus.bluetooth.getBLEDeviceServices({
					deviceId: that.deviceId,
					success: function(e) {
						var services = e.services;
						console.log('get services success: ' + services.length);
						that.serviceId = services[4].uuid
						for (var i in services) {
							console.log(i + ': ' + JSON.stringify(services[i]));
						}
						that.getCharacteristics()
					},
					fail: function(e) {
						console.log('get services failed: ' + JSON.stringify(e));
					}
				});
			},
			getCharacteristics() {
				let that = this
				plus.bluetooth.getBLEDeviceCharacteristics({
					deviceId: that.deviceId,
					serviceId: that.serviceId,
					success: function(e) {
						var characteristics = e.characteristics;
						console.log('get characteristics success: ' + characteristics.length);
						for (var i in characteristics) {
							console.log(i + ': ' + JSON.stringify(characteristics[i]));
						}
						that.characteristicId = characteristics[0].uuid

						let item = {
							characteristicId: that.characteristicId,
							deviceId: that.deviceId,
							serviceId: that.serviceId
						}
						// 准备要发送的字符串
						// const str = `SIZE 50 ,30  \r\n GAP 2 mm \r\n CLS \r\n QRCODE 280, 28, L, 4, A, 0, "www.baidu.com" \n\r   TEXT 50,50,"2",0,1,1,"DEMO FOR TEXT"\r\n BOX 0,20,380,225,3 \r\n BOX 0,20,260,120,2 \r\n BOX 0,120,380,180,2 \r\n BOX 0,20,380,120,2 \r\n   PRINT 1,1\r\n `;
						// const str = 'SIZE 50mm,30mm  \r\n GAP 2mm  \r\n CLS \r\n TEXT 50,50,"2",0,1,1,"DEMO FOR TEXT"\r\n QRCODE 280, 28, L, 4, A, 0, "www.baidu.com" \r\n  BOX 0,20,260,120,2 \r\n BOX 0,20,380,120,2 \r\n BOX 0,20,380,225,3 \r\n   PRINT 1,1 \r\n'
						// const str = 'SIZE 50mm,30mm  \r\n GAP 2mm  \r\n CLS \r\n TEXT 50,50,"2",0,1,1,"DEMO FOR TEXT"\r\n QRCODE 280, 28, L, 4, A, 0, "www.baidu.com" \r\n  BOX 0,20,260,120,2 \r\n BOX 0,20,380,225,3 \r\n BOX 0,20,380,225,3 \r\n   PRINT 1,1 \r\n'
						 const content="123"
						 const djh="CGDD20240411001"
						const str = 'SIZE 50 mm, 30 mm\r\nGAP 2 mm\r\nCLS\r\n';
						// 将字符串转换为 ArrayBuffer
						const arrayBuffer = that.stringToArrayBufferManual(str);
						that.writeCharacteristics(item, arrayBuffer)
						const str2 = `QRCODE 280, 28, L, 4, A, 0,"${content}"\r\n`;
						// 将字符串转换为 ArrayBuffer
						const arrayBuffer2 = that.stringToArrayBufferManual(str2);
						const str3 = `TEXT 5,30,"2",0,1,1,"${djh}"\r\n`;
						// 将字符串转换为 ArrayBuffer
						const arrayBuffer3 = that.stringToArrayBufferManual(str3);
						that.writeCharacteristics(item, arrayBuffer3)
						const str1 = `BOX 0,20,260,120,2\r\nBOX 0,20,400,240,3\r\nBOX 0,20,400,120,2\r\nBOX 0,20,260,70,2\r\nBOX 0,70,130,120,2\r\nBOX 0,120,400,200,2 \r\nBOX 0,200,200,240,2 \r\nPRINT 1,1\r\n`;
						// 将字符串转换为 ArrayBuffer
						const arrayBuffer1 = that.stringToArrayBufferManual(str1);
						that.writeCharacteristics(item, arrayBuffer1)
						
						
					},
					fail: function(e) {
						console.log('get characteristics failed: ' + JSON.stringify(e));
					}
				});
			},
			stringToArrayBufferManual(str) {
				const buffer = new ArrayBuffer(str.length); // 分配所需的空间
				const bufferView = new Uint8Array(buffer);
				for (let i = 0; i < str.length; i++) {
					bufferView[i] = str.charCodeAt(i); // 将字符转换为 UTF-16 代码单元
						console.log(JSON.stringify(buffer))
				}
				console.log(JSON.stringify(buffer))
				return buffer;
			},
			createQRcode(content) {
				let str= `QRCODE 280, 28, L, 4, A, 0, "${content}"\r\n`
				const arrayBuffer1 = that.stringToArrayBufferManual(str1);
				that.writeCharacteristics(item, arrayBuffer1)
			},
			print() {
				this.getConnectedDevices()
				this.listenerConnection()

				this.getServices()

			}
		}
	}
</script>

<style lang="scss">
	.ts {
		padding: 40rpx;
		background-color: palevioletred;
		margin-top: 20rpx;

	}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值