H5 连接蓝牙打印机,打印条码、二维码

本文介绍了如何通过JavaScript操作蓝牙打印机,包括打印初始化、条形码和二维码的打印过程,以及设置文本对齐和走纸功能。重点展示了`initPrinter`、`printTM`和`printQrcode`函数的使用。
摘要由CSDN通过智能技术生成
/**
 * @Description:打印初始化
 * @author yk
 * @createTime: 2021-1-16 10:37:09
 */
function initPrinter() {

	console.log('打印初始化开始');
	var outputStream = bluetoothSocket.getOutputStream();
	plus.android.importClass(outputStream);
	OutputStreamWriter = plus.android.importClass('java.io.OutputStreamWriter')
	writer = new OutputStreamWriter(outputStream, 'GBK')
	plus.android.importClass(writer)
	writer.write(0x1B)
	writer.write(0x40)
	writer.flush()
	console.log('打印初始化结束');

}


/**
  * @Description: 打印条码
  * @author yk
  * @createTime: 2021-1-16 11:13:14
  */

function printTM(byteStr) {

	if (!bluetoothSocket.isConnected()) {
		console.log('断开了,需要重新连接,连接中');
		bluetoothSocket.connect();
	}

	initPrinter();

	var bytes = plus.android.invoke(byteStr, 'getBytes', 'gbk')
	var length = bytes.length
	outputStream = bluetoothSocket.getOutputStream()
	plus.android.importClass(outputStream)
	OutputStreamWriter = plus.android.importClass('java.io.OutputStreamWriter')
	writer = new OutputStreamWriter(outputStream, 'GBK')
	plus.android.importClass(writer)

	writer.write(0x1B); //打印复位
	writer.write(0x40); //打印复位
	writer.flush();
	var vstr = '{B' + byteStr;
	console.log(vstr);
	var bytes = plus.android.invoke(vstr, 'getBytes', 'gbk');
	var length = bytes.length

	
	
	writer.write(0x1D);
	writer.write(0x6B);
	writer.write(73);
	writer.write(length);
	writer.write(vstr);
	writer.flush();
	//换行
	writer.write("\n");
	writer.flush();



	writer.write(byteStr);
	writer.flush();

	setAlignPosition(1);
	console.log('条码打印成功');

	ff(2);



}

/**
 * 方法说明 设置文本对齐方式
 * @param align 打印位置  0:居左(默认) 1:居中 2:居右
 * @return 
 * @createTime: 2021-1-16 10:38:39
 */
function setAlignPosition(align) {
	writer.write(0x1B);
	writer.write(0x61);
	writer.write(1);
	writer.flush();
}



/**
 * @Description: 二维码打印
 * @Author: EricLee
 * @Date: 2020-10-15 15:16:10
 * @Param: byteStr {String} 要打印的内容
 * @Return: void
 */
function printQrcode(byteStr) {

	// init
	initPrinter();
	console.log(byteStr);
	var moduleSize = 8
	var bytes = plus.android.invoke(byteStr, 'getBytes', 'gbk')
	var length = bytes.length
	outputStream = bluetoothSocket.getOutputStream()
	plus.android.importClass(outputStream)
	OutputStreamWriter = plus.android.importClass('java.io.OutputStreamWriter')
	writer = new OutputStreamWriter(outputStream, 'GBK')
	plus.android.importClass(writer)
	console.log(length)
	// 缓存二维码数据
	writer.write(0x1D) // init
	writer.write('(k') // adjust height of barcode
	writer.write(length + 3) // pl
	writer.write(0) // ph
	writer.write(49) // cn
	writer.write(80) // fn
	writer.write(48) //
	writer.write(byteStr)
	// 二维码纠错等级
	writer.write(0x1D)
	writer.write('(k')
	writer.write(3)
	writer.write(0)
	writer.write(49)
	writer.write(69)
	writer.write(48)
	// 设置二维码块大小
	writer.write(0x1D)
	writer.write('(k')
	writer.write(3)
	writer.write(0)
	writer.write(49)
	writer.write(67)
	writer.write(moduleSize)
	// 打印已缓存的数据二维码
	writer.write(0x1D)
	writer.write('(k')
	writer.write(3) // pl
	writer.write(0) // ph
	writer.write(49) // cn
	writer.write(81) // fn
	writer.write(48) // m

	writer.flush()
	// 二维码打印 结束
	ff(2);
	console.log('print Qrcode')
}


/**
  * @Description:走纸
  * @author yk
  * @createTime: 2021-1-16 11:13:57
  */

function ff(n) {

	// bluetoothSocket.close();
	if (!bluetoothSocket.isConnected()) {
		console.log('断开了,需要重新连接,连接中');
		bluetoothSocket.connect();
	}

	if (bluetoothSocket.isConnected()) {
		console.log('连接成功');
		//initPrinter();
		const line = n || 1;
		var outputStream = bluetoothSocket.getOutputStream();
		plus.android.importClass(outputStream);
		outputStream.write(0x1B);
		outputStream.write(0x64);
		outputStream.write(line); // 行数
		outputStream.flush();
	}

}

```javascript
在这里插入代码片

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值