uni-app 蓝牙打印

1,257 阅读6分钟

此方法支持连接蓝牙 发送打印指令;(测试机 ,芝柯打印机)

此功能没有搜索附近的蓝牙,因为没什么必要,手机自带蓝牙搜索 连接,不比手写方便稳定?

打印机uuid通用

   打印指令根据  便携式蓝牙打印机 所支持的命令自行编写!

直接上代码

新建 js文件 

export function initPrinter() {  //初始化 获取设备已连接的蓝牙
				let deviceList = [];
				var main = plus.android.runtimeMainActivity();
				var Context = plus.android.importClass("android.content.Context");
				var BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);
				plus.android.importClass(BManager); 
				var BAdapter = BManager.getAdapter();
				plus.android.importClass(BAdapter); 
				var lists = BAdapter.getBondedDevices();
				plus.android.importClass(lists);
				var iterator = lists.iterator();
				plus.android.importClass(iterator);
				while (iterator.hasNext()) {
					var d = iterator.next();
					plus.android.importClass(d);
					var temp = {
						name: d.getName(),
						address: d.getAddress(),
						status: d.getBondState(),
						uuids: d.getUuids(),
						op: d
					};
					deviceList.push(temp);
				}
	return deviceList
}
function printTest(outputStream,text) { //写入命令
		var arrayBuffer = plus.android.invoke(text, 'getBytes', 'gbk');
		outputStream.write(arrayBuffer);
		outputStream.flush();
}		
export  function  printSomething(dev,text) {  // 连接蓝牙 
				var main = plus.android.runtimeMainActivity();
				var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
				var UUID = plus.android.importClass("java.util.UUID");
				var uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
				var BAdapter = BluetoothAdapter.getDefaultAdapter();
				var device = BAdapter.getRemoteDevice(dev.address);
				plus.android.importClass(device);
				var bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
				plus.android.importClass(bluetoothSocket);
				setTimeout(async e=>{
					if (!bluetoothSocket.isConnected()) {
						bluetoothSocket.connect();
						if (bluetoothSocket.isConnected()) {
							var outputStream = bluetoothSocket.getOutputStream();
							plus.android.importClass(outputStream);
							
						await	printTest(outputStream,text);
							bluetoothSocket.close();
							if (!bluetoothSocket.isConnected()) {
								console.log("设备已关闭");
							}
						} else {
							uni.showToast({
							    title: '设备连接失败',
								icon:'error',
							    duration: 2000
							});
						}
					}
				})
};
export function openBluetooth(){   //主动打开蓝牙
				var main = plus.android.runtimeMainActivity();  
				var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");  
				var BAdapter = BluetoothAdapter.getDefaultAdapter();  
				if(!BAdapter.isEnabled()) {  
					BAdapter.enable();  
				}
}

dom 

<view>
		<view style="text-align: center;margin-bottom: 20rpx;">
			已配对蓝牙设备
		</view>
		<view style="text-align: center;padding: 10rpx 0;border: 1rpx solid #2B85E4;border-radius: 15rpx;" class="uni-flex uni-row" v-for="(device,index) in deviceList" :key="index">
			<view style="width: 30rpx;">{{index+1}}</view>
			<view style="width: 180rpx;">{{device.name}}</view>
			<view style="width: 310rpx;">{{device.address}}</view>
			<view class="flex-item"><button type="primary" plain size="mini" @click="pint()">打印测试</button></view>
		</view>
	</view>

使用

import {openBluetooth,initPrinter,printSomething} from './xxx.js'
	export default {
		data() {
			return {
				deviceList: [],
			}
		},
		onLoad(p) {
			// openBluetooth   主动开启蓝牙
			var that = this;
			// #ifdef APP-PLUS
			that.deviceList = initPrinter()
			// that.initPrinter();
			// #endif
		},
		methods: {
			
			pint(){
				// 给设备发送指令  只能以下面text发送  发送多个 也需要一条一条如  ['TEXT 9 5 250 2 内箱标签..','TEXT 9 5 250 2 内箱标签...']   就需要一条一条发
				let dev = {   //这里根据实际情况调整 
					name:this.deviceList[0].name,
					address:this.deviceList[0].address
				};
				var text = "! 0 200 200 1680 1\r\n" +
        "B 128 2 2 64 400 0 3100488650709\r\n" +
        "LINE 0 136 800 136 1\r\n" +
        "LINE 0 188 800 188 1\r\n" +
        "LINE 0 240 800 240 1\r\n" +
        "LINE 600 136 600 240 1\r\n" +
        "LINE 48 240 48 345 1\r\n" +
        "TEXT 24 0 464 80 3100488650709\r\n" +
        "TEXT 24 0 16 144 发件网点名\r\n" +
        "TEXT 24 0 608 144 收件地邮编_mo32\r\n" +
        "TEXT 24 0 16 193 收件网点名_mo33\r\n" +
        "TEXT 24 0 608 193 收件地邮编_mo32\r\n" +
        "LINE 0 289 800 289 1\r\n" +
        "LINE 0 345 800 345 1\r\n" +
        "LINE 400 377 800 377 1\r\n" +
        "LINE 0 408 800 408 1\r\n" +
        "LINE 0 500 800 500 1\r\n" +
        "LINE 400 345 400 408 1\r\n" +
        "TEXT 24 0 8 248 寄方\r\n" +
        "TEXT 24 0 8 297 收方\r\n" +
        "TEXT 24 0 56 248 发件人姓名_mo_36]          发件人手机_mo_37] 发件人座机_mo_38]\r\n" +
        "TEXT 24 0 56 272 寄件时间:       年   月   日   时   分\r\n" +
        "TEXT 24 0 56 297 收件人姓名_mo_43]          收件人手机_mo_44] 收件人座机_mo_45]\r\n" +
        "TEXT 24 0 56 321 收件人详细地址_mo_46]\r\n" +
        "TEXT 24 0 8 353 寄托物:订单物品_mo_47]\r\n" +
        "TEXT 24 0 408 353 打印时间:时间_mo_39]\r\n" +
        "TEXT 24 0 408 384 签收时间:       年   月   日   时   分\r\n" +
        "TEXT 24 0 8 416 附加服务:声明价值:订单总全额_mo_52]\r\n" +
        "TEXT 24 0 8 448 备注:卖家备注_mo_53]\r\n" +
        "B 128 2 2 64 400 648 3100488650709\r\n" +
        "LINE 0 744 800 744 1\r\n" +
        "LINE 0 816 800 816 1\r\n" +
        "LINE 0 897 800 897 1\r\n" +
        "LINE 48 744 48 897 1\r\n" +
        "LINE 600 744 600 897 1\r\n" +
        "LINE 0 960 800 960 1\r\n" +
        "LINE 0 1016 800 1016 1\r\n" +
        "LINE 400 968 400 1016 1\r\n" +
        "TEXT 24 0 464 728 3100488650709\r\n" +
        "TEXT 24 0 8 752 寄方\r\n" +
        "TEXT 24 0 8 832 收方\r\n" +
        "TEXT 24 0 608 840 业务类型_mo34\r\n" +
        "TEXT 24 0 56 752 发件人姓名_mo_36]          发件人手机_mo_37] 发件人座机_mo_38]\r\n" +
        "TEXT 24 0 56 776 寄件时间:       年   月   日   时   分\r\n" +
        "TEXT 24 0 56 832 收件人姓名_mo_43]          收件人手机_mo_44] 收件人座机_mo_45]\r\n" +
        "TEXT 24 0 56 856 收件人详细地址_mo_46]\r\n" +
        "TEXT 24 0 8 968 寄托物:订单物品_mo_47]\r\n" +
        "TEXT 24 0 408 968 签收时间:       年   月   日   时   分\r\n" +
        "TEXT 24 0 8 1024 附加服务:声明价值:订单总全额_mo_52]\r\n" +
        "TEXT 24 0 8 1056 备注:卖家备注_mo_53]\r\n" +
        "B 128 2 2 64 400 1120 3100488650709\r\n" +
        "TEXT 24 0 464 1200 3100488650709\r\n" +
        "LINE 0 1216 800 1216 1\r\n" +
        "LINE 0 1288 800 1288 1\r\n" +
        "LINE 0 1369 800 1369 1\r\n" +
        "LINE 48 1216 48 1216 1\r\n" +
        "LINE 600 1216 600 1369 1\r\n" +
        "LINE 0 1432 800 1432 1\r\n" +
        "LINE 0 1488 800 1488 1\r\n" +
        "LINE 400 1440 400 1488 1\r\n" +
        "TEXT 24 0 8 1224 寄方\r\n" +
        "TEXT 24 0 8 1304 收方\r\n" +
        "TEXT 24 0 608 1312 业务类型_mo34\r\n" +
        "TEXT 24 0 56 1224 发件人姓名_mo_36]          发件人手机_mo_37] 发件人座机_mo_38]\r\n" +
        "TEXT 24 0 56 1248 寄件时间:       年   月   日   时   分\r\n" +
        "TEXT 24 0 56 1304 收件人姓名_mo_43]          收件人手机_mo_44] 收件人座机_mo_45]\r\n" +
        "TEXT 24 0 56 1328 收件人详细地址_mo_46]\r\n" +
        "TEXT 24 0 8 1440 寄托物:订单物品_mo_47]\r\n" +
        "TEXT 24 0 408 1440 签收时间:       年   月   日   时   分\r\n" +
        "TEXT 24 0 8 1496 附加服务:声明价值:订单总全额_mo_52]\r\n" +
        "TEXT 24 0 8 1528 备注:卖家备注_mo_53]\r\n" +
        "FORM\r\n" +
        "PRINT\r\n";
					printSomething(dev,text)
			}
			
		}
	}

如果可以打印出来没有格式 自行调整,次代码仅为 连接蓝牙以及传输命令;

:注 公司测试机 芝柯打印机 蓝牙连接时间长,会断开 便每次发送指令重新连接一下  如不需要自行调整