H5 +蓝牙打印机 CPCL和ESC

这篇博客介绍了如何使用H5进行蓝牙打印机的搜索、连接,并展示了如何利用CPCL和ESC命令进行打印操作。内容涵盖了获取蓝牙设备、建立连接、发送打印指令等关键步骤,涉及字符串编码、字节处理等技术。
摘要由CSDN通过智能技术生成

@[搜索蓝牙打印机函数]
var SearchBluetooth = function() {
/dom变量定义/
var BluetoothBtn = document.getElementById(“BluetoothBtn”), //最下边的按钮
unpairedList = document.getElementById(“unpairedList”), //未配对设备列表
pairedList = document.getElementById(“pairedList”), //已配对设备列表
loadImgHtml = ‘’; //加载图像HTML

/*plus变量定义*/
var main, BluetoothAdapter, BAdapter, IntentFilter, BluetoothDevice, receiver; //有些我也不知道是啥意思-_-!;

/*其他定义*/
var isSearchDevices = false, //是否处于搜索状态
	savedBleId = localStorage.getItem("bleId"), //缓存的设备ID
	IntervalObj, //定时器对象
	BleDeviceObjAry = [], //BleDevice对象数组 
	debug = false; //调试模式

return {
	//初始化方法
	Init: function() {
		main = plus.android.runtimeMainActivity(),
			BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter"),
			IntentFilter = plus.android.importClass('android.content.IntentFilter'),
			BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice"),
			BAdapter = new BluetoothAdapter.getDefaultAdapter();

		this.CheckBluetoothState();
		this.EventInit();
	},

	//事件绑定
	EventInit: function() {
		var self = this,
			bdevice = new BluetoothDevice();

		//搜索
		BluetoothBtn.addEventListener("tap", function() {
			if(!isSearchDevices) {
				self.SearchDevices();
			}
		});

		/*未配对列表点击事件*/
		mui("#unpairedList").on("tap", "li", function() {
			var id = this.getAttribute("data-id"),
				state = true;
			self.SetButtonStatus("正在配对...", true);
			for(var i = 0, l = BleDeviceObjAry.length; i < l; i++) {
				var BleDeviceItem = BleDeviceObjAry[i];
				main.unregisterReceiver(receiver); //取消监听

				if(BleDeviceItem.getAddress() === id) {
					BleDeviceItem.createBond();

					self.SetButtonStatus("正在配对...", true);

					var testBondState = setInterval(function() {
						if(BleDeviceItem.getBondState() === bdevice.BOND_BONDED) {
							mui.toast("配对成功");
							self.SetButtonStatus("配对成功正在尝试连接打印机...", true);
							localStorage.setItem("bleId", id);
							
							var bleObj = new ConnectPrinter(id);
							bleObj = null;
							window.clearInterval(testBondState);
							mui.back();
						} else if(BleDeviceItem.getBondState() === bdevice.BOND_NONE) {
							mui.toast("配对失败");
							window.clearInterval(testBondState);
							self.SetButtonStatus("重新搜索设备", false);
						}
					}, 1000);
					state = false;
					break;
				}
			}

			if(state) {
				mui.toast("配对失败请重新搜索设备");
				self.SetButtonStatus("重新搜索设备", false);
			}
		});

		/*已配对列表点击事件*/
		mui("#pairedList").on("tap", "li", function() {
			var id = this.getAttribute("data-id");
			if(id) {
				self.SetButtonStatus("配对成功正在尝试连接打印机...", true);
				localStorage.setItem("bleId", id);
				var bleObj = new ConnectPrinter(id);
				
					bleObj = null;
				mui.back();
			}
		});
	},

	//检测蓝牙状态
	CheckBluetoothState: function() {
		var self = this;
		if(!BAdapter.isEnabled()) {
			plus.nativeUI.confirm("蓝牙处于关闭状态,是否打开?", function(e) {
				if(e.index == 0) {
					BAdapter.enable();
				}
			});
			debug && console.log("蓝牙处于关闭状态,正在打开...");
		} else {
			self.SearchDevices();
			debug && console.log("蓝牙处于开启状态,准备搜索蓝牙设备...");
		}
	},

	//搜索设备
	SearchDevices: function() {
		var self = this;
		isSearchDevices = true;
		self.SetButtonStatus("正在搜索蓝牙设备...", true);
		debug && console.log("开始搜索蓝牙设备...");

		var filter = new IntentFilter(),
			bdevice = new BluetoothDevice();

		BleDeviceObjAry = []; //清空BleDeviceObjAry
		unpairedList.innerHTML = '';
		pairedList.innerHTML = '';
		BAdapter.startDiscovery(); //开启搜索

		receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
			onReceive: onReceiveFn
		});
		filter.addAction(bdevice.ACTION_FOUND);
		filter.addAction(BAdapter.ACTION_DISCOVERY_STARTED);
		filter.addAction(BAdapter.ACTION_DISCOVERY_FINISHED);
		filter.addAction(BAdapter.ACTION_STATE_CHANGED);
		main.registerReceiver(receiver, filter); /
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值