//搜索蓝牙打印机函数
var SearchBluetooth = function() {
	/*dom变量定义*/
	var BluetoothBtn = document.getElementById("BluetoothBtn"), //最下边的按钮
		unpairedList = document.getElementById("unpairedList"), //未配对设备列表
		pairedList = document.getElementById("pairedList"), //已配对设备列表
		loadImgHtml = '<img src="images/ring.gif" class="loadImg"/>'; //加载图像HTML

	/*plus变量定义*/
	var main, BluetoothAdapter, BAdapter, IntentFilter, BluetoothDevice, receiver , receiverStart;

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

	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.CheckPermissions();
		},
		
		CheckPermissions: function() {
			var self = this;
			plus.android.requestPermissions(['android.permission.ACCESS_FINE_LOCATION','android.permission.ACCESS_COARSE_LOCATION',
			'android.permission.BLUETOOTH','android.permission.BLUETOOTH_ADMIN'], function(e){
					if(e.deniedAlways.length>0){	//权限被永久拒绝
						// 弹出提示框解释为何需要定位权限，引导用户打开设置页面开启
						debug && console.log("获取失败，权限被永久拒绝...");
					}
					if(e.deniedPresent.length>0){	//权限被临时拒绝
						// 弹出提示框解释为何需要定位权限，可再次调用plus.android.requestPermissions申请权限
						debug && console.log("获取失败，权限被临时拒绝...");
					}
					if(e.granted.length>0){	//权限被允许
					debug && console.log("获取权限成功，调用搜索...");
					    //调用依赖获取权限的代码
						self.CheckBluetoothState();
						self.EventInit();
					}
				}, function(e){
				    console.log('Request Permissions error:'+JSON.stringify(e));
				});
		},

		//事件绑定
		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);
							} 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;
				}
			});
		},

		//检测蓝牙状态
		CheckBluetoothState: function() {
			var self = this;
			var filter = new IntentFilter();
             receiverStart = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
                            onReceive: onReceiveStart
                        });
            filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
            main.registerReceiver(receiverStart, filter); //注册监听事件

            //监听回调函数
            function onReceiveStart(context, intent) {
                 plus.android.importClass(intent); //通过intent实例引入intent类，方便以后的‘.’操作
                 var state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                             BluetoothAdapter.ERROR)
                 if(state === BluetoothAdapter.STATE_ON) {
                     debug && console.log("检测到蓝牙开启，准备搜索蓝牙设备...");
                     self.SearchDevices();
                 }
            }

			if(!BAdapter.isEnabled()) {
				var AlertDialog = plus.android.importClass('android.app.AlertDialog');
				var adb = plus.android.newObject("android.app.AlertDialog$Builder",plus.android.runtimeMainActivity());
				adb.setTitle("蓝牙处于关闭状态，是否打开？");
				var listener = plus.android.implements('android.content.DialogInterface$OnClickListener',{
                        onClick:function(dialog,which){
                        BAdapter.enable();
                        debug && console.log("蓝牙处于关闭状态，正在打开...");
                     }

                    });

                    adb.setPositiveButton("确定",listener);
                    adb.setNegativeButton("取消",null);
                    adb.setCancelable(false);
                    adb.show();
                } else {
                    self.SearchDevices();
                    debug && console.log("蓝牙处于开启状态，准备搜索蓝牙设备...");
                }
		},

		//搜索设备
		SearchDevices: function() {
			var self = this;
			var arr_mac = [];
			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); //注册监听事件

			//监听回调函数
			function onReceiveFn(context, intent) {
				plus.android.importClass(intent); //通过intent实例引入intent类，方便以后的‘.’操作

				//开始搜索改变状态
				intent.getAction() === "android.bluetooth.device.action.FOUND" && (isSearchDevices = true);

				//判断是否搜索结束
				if(intent.getAction() === 'android.bluetooth.adapter.action.DISCOVERY_FINISHED') {
					main.unregisterReceiver(receiver); //取消监听
					isSearchDevices = false;
					BleDeviceObjAry = [];
					self.SetButtonStatus("重新搜索设备", false);
					return false;
				}

				var BleDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE),
					bleName = BleDevice.getName(), //设备名称
					bleId = BleDevice.getAddress(); //设备mac地址

				if(!bleName || !bleId) {
					return false;
				}

                if(arr_mac.indexOf(bleId)>= 0){
                    return;
                }
                arr_mac.push(bleId);
				//判断是否配对
				if(BleDevice.getBondState() === bdevice.BOND_BONDED) {
					debug && console.log("已配对蓝牙设备：" + bleName + '    ' + bleId);

					self.SetpairedListHtml(pairedList, bleName, bleId);
					//如果缓存保存的设备ID和该ID一致则配对
					if(savedBleId == bleId) {
						BleDevice.createBond();
					}

				} else {
					debug && console.log("未配对蓝牙设备：" + bleName + '    ' + bleId);

					BleDeviceObjAry.push(BleDevice);
					self.SetpairedListHtml(unpairedList, bleName, bleId);
				}

			}

			function isRepeat(arr){
                var hash = {};
                    for(var i in arr) {
                    if(hash[arr[i]]) //hash 哈希
                    return true;
                    hash[arr[i]] = true;
                    }
                return false;
                }
		},

		//设置设备列表HTML
		SetpairedListHtml: function(parentEl, bleName, bleId) {
			var li = document.createElement('li');
			li.setAttribute("data-id", bleId);
			li.innerHTML = bleName + "<span>" + bleId + "</span>";
			parentEl.appendChild(li);
		},

		//更改按钮状态
		SetButtonStatus: function(tipText, isDisabled) {
			if(isDisabled) {
				BluetoothBtn.innerHTML = loadImgHtml + tipText;
				BluetoothBtn.classList.add("mui-disabled");
			} else {
				BluetoothBtn.innerHTML = tipText;
				BluetoothBtn.classList.remove("mui-disabled");
			}
		}
	}
}();
//连接打印机和打印
function  dayin(window) {
	window.ConnectPrinter = function(bleId) {

		var plusMain = plus.android.runtimeMainActivity(),
			BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter"),
			UUID = plus.android.importClass("java.util.UUID"),
			uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"),
			BAdapter = BluetoothAdapter.getDefaultAdapter(),
			device = BAdapter.getRemoteDevice(bleId);

		plus.android.importClass(device);

		var bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
		plus.android.importClass(bluetoothSocket);
            if(!bluetoothSocket.isConnected()) {
                    connectStart = bluetoothSocket.connect();
                }


        var outputStream = bluetoothSocket.getOutputStream();
        plus.android.importClass(outputStream);
        var bytes = [0x1b,0x61,0x01];
        outputStream.write(bytes);
        var bytes = [0x1c,0x21,(0x04|0x08)];
        outputStream.write(bytes);
        var bytes = [0x1b,0x21,0x08];
        outputStream.write(bytes);
        var bytes = [0x0d,0x0a];
        outputStream.write(bytes);
        var bytes = plus.android.invoke('华夏银行', 'getBytes', 'gbk');
        outputStream.write(bytes);
        var bytes = [0x0d,0x0a];
        outputStream.write(bytes);
        var bytes = plus.android.invoke('XX支行', 'getBytes', 'gbk');
        outputStream.write(bytes);
        var bytes = [0x0d,0x0a];
        outputStream.write(bytes);
        var bytes = [0x1c,0x21,0x00];
        outputStream.write(bytes);
        var bytes = [0x1b,0x21,0x00];
        outputStream.write(bytes);
        var bytes = [0x0d,0x0a];
        outputStream.write(bytes);
        var bytes = plus.android.invoke('欢迎您的光临', 'getBytes', 'gbk');
        outputStream.write(bytes);
        var bytes = [0x0d,0x0a];
        outputStream.write(bytes);
        var bytes = plus.android.invoke('您的排队号码', 'getBytes', 'gbk');
        outputStream.write(bytes);
        var bytes = [0x0d,0x0a];
        outputStream.write(bytes);
        var bytes = [0x1b,0x21,(0x08|0x20|0x10)];
        outputStream.write(bytes);
        var bytes = plus.android.invoke('1006', 'getBytes', 'gbk');
        outputStream.write(bytes);
        var bytes = [0x0d,0x0a];
        outputStream.write(bytes);
        var bytes = [0x1b,0x21,0x00];
        outputStream.write(bytes);
        var bytes = [0x0d,0x0a];
        outputStream.write(bytes);
        var bytes = plus.android.invoke('      现有8人在等候，请您到休息厅等候呼叫', 'getBytes', 'gbk');
        outputStream.write(bytes);
        var bytes = [0x0d,0x0a];
        outputStream.write(bytes);
        var bytes = plus.android.invoke('2019-9-12 10:15:51', 'getBytes', 'gbk');
        outputStream.write(bytes);
        var bytes = [0x0d,0x0a];
        outputStream.write(bytes);
        outputStream.flush();
		
		this.closeBtSocket = function() {
					bluetoothSocket.close();
					bluetoothSocket = null;
					device = null;
				};
	};
};