<template>
	<view>
		<view style="height: 50px;"></view>
		<view>
			<view class="title">Cocos预览地址：{{LocationGameUrl}}</view>
			<input class="uni-input" :value="LocationGameUrl" @input="onKeyInput" placeholder="输入cocos预览地址" />
		</view>
		<view class="card-view padding-top padding-bottom" v-for="(item,index) in devicesList" :key="index"
			:id="'task_'+index" :class="item.bRatio?'hardware-border':''" :style="{'z-index': threeZIndex}">
			<view class="flex justify-between align-center">
				<view class="flex justify-start align-center">
					<image style="margin-left: 20rpx; width: 200rpx;height: 120rpx;" :src="item.icon" mode="aspectFit">
					</image>
					<view style="width: 350rpx;margin-left: 10rpx;">
						<view style="margin: 20rpx 0rpx; font-weight: bold; font-size: 18px; color: #565656;">
							{{item.cname}}
						</view>
						<view style="font-size: 12px; white-space:pre-wrap;" class="make-text-bPurple">
							{{item.describe}}{{item.bOldDevice?'(旧手柄)':''}}
						</view>
					</view>

				</view>
				<image style="margin-right: 60rpx; width: 60rpx;height: 60rpx;"
					:src="item.bRatio?'/static/devicesOther/radio-b.png':'/static/devicesOther/radio-g.png'"
					mode="aspectFit" @tap="_onRadio(item,$event)"></image>
			</view>
		</view>

		<view v-if="currentItem" style="display: flex;flex-direction: row;">
			<button style="margin: 10rpx;" type="primary" @click="B_OnSetMTU">MTU</button>
			<button style="margin: 10rpx;" type="primary" @click="writeBLEValue('V')">版本</button>
			<button style="margin: 10rpx;" type="primary" @click="writeBLEValue('M')">mac</button>
		</view>
		<view v-if="currentItem" style="display: flex;flex-direction: row;">
			<button style="margin: 10rpx;" type="primary" @click="writeBLEValue('H')">开启高速</button>
			<button style="margin: 10rpx;" type="primary" @click="writeBLEValue('h')">关闭高速</button>
		</view>
		<scroll-view class="text-box" scroll-y="true">
			<scroll-view scroll-x="true">
				<text selectable="true" style="background-color: greenyellow;">{{text}}</text>
			</scroll-view>
		</scroll-view>

		<button type="warn" :disabled="extraLine.length<=0" @click="remove">
			清除输入框数据
		</button>
		<button style="margin-top: 2px;" type="warn" :disabled="!currentItem" @click="onShowGame">
			显示游戏
		</button>

		<button style="margin-top: 2px;" type="warn" @click="onEmit">
			模拟发送
		</button>

		<!-- <button style="margin-top: 2px;" type="warn" @click="onClise">
			close
		</button> -->


		<!-- <view class="content">
		</view> -->
	</view>


</template>

<script>
	import config from '@/common/config.js';
	import reqUtil from '@/util/util-js/requstUtil.js';
	import ble from '@/util/util-js/BLE.js';

	import {
		mapState,
		mapMutations
	} from 'vuex';

	export default {
		computed: mapState(['bOpenBluetooth', 'bOpenSuccess', 'bListenAdapterStateChange', 'bListenDeviceFound',
			'BLEConnectDevice', 'BLEGetServices', 'cIndex', 'bConnection', 'bVerifiedConnection', 'BLEInfoList',
			'BLEDeviceShowList', 'finallyUseDevice', 'systemInfo', 'guideUnlockState', 'currentModeIndex',
			"bListenerAccArray", 'LocationGameUrl','bGamePlaying'
		]),
		data() {
			return {
				// 设备列表
				devicesList: [],
				currentItem: null,
				//设置一个旧的连接item
				oldItem: null,
				searchObj: null,
				//设置搜索超时时间
				searchTimeOut: null,
				option: null,
				saveObj: null,
				//搜索提示定时器
				searchMac: null,
				//
				getServicesTimeout: null,
				writeMacTimeout: null,
				//限制关闭连接
				bLimitClose: false,

				//是否需要检测发起的连接蓝牙是否匹配上
				bTestBondConnect: true,
				//是否显示
				bShow: true,
				//切换连接
				bSwitch: false,

				//提示
				circular: false,
				currentHeight: 0,
				threeTipHeight: 0,
				threeZIndex: 0,
				currentIndex: 5,


				//测试
				text: '',
				extraLine: [],

				bShowGame: false,
			}
		},
		onLoad(op) {
			this.add("初始化");
			uni.$on('retryConnectBLESuccess', this.onRetryConnectBLESuccess);
			uni.$on('callbackCloseBLE', this.hardCallbackCloseBLE);
			uni.$on('listenerBLE', this.onListenerBLE);
			uni.$on('updateBLEDeviceData', this.callbackUpdateBLEData);

			this.BLEInfoList.forEach((item, index, selfarr) => {
				if (item.deviceType == 'BLEHandle' && item.usageMode == 'hotman') {
					let item = Object.assign({}, selfarr[index], {
						bOldDevice: false
					});
					this.devicesList.push(item);
				}
			})


			//subview
			/**
			 * $on 之后要调用 $off,不然会重复绑定
			 */
			this.onGetLocationGameUrl();
			

			uni.$on("log", this.add)

		},
		onUnload() {
			uni.$off('retryConnectBLESuccess', this.onRetryConnectBLESuccess);
			uni.$off('callbackCloseBLE', this.hardCallbackCloseBLE);
			uni.$off('listenerBLE', this.onListenerBLE);
			uni.$off('updateBLEDeviceData', this.callbackUpdateBLEData);
			//清除定时器
			this.onClearTimeout();


			uni.$off("log", this.add)

		},
		onShow() {
			this.bShow = true;
		},
		onReady() {},
		onHide() {
			//如果蓝牙弹出匹配框，会触发onHide。这时候处理蓝牙连接流程检测应等onShow 时候，再检测
			this.bShow = false;
		},
		methods: {
			...mapMutations(['initAdapter', 'onCreateBLESuccess', 'onGetBLEDeviceServices', 'onOnlyCloseBLEConnection',
				'onGetRSSITransDistance',
				'addBLEDevice', 'onWriteBLEConnectionValue', 'deleteBLEDevice', 'B_GetBondedDevices',
				'B_OpenBLESetting', 'setGuideUnlockState', 'switchLevelList', 'onSetLocationGameUrl',
				'onGetLocationGameUrl', 'onTestEmit', 'B_CloseBLEConnection', 'B_OnSetMTU'
			]),
			onEmit() {
				this.onTestEmit();
			},
			/**
			 * 显示游戏
			 */
			onShowGame() {
				// uni.$emit("showGame");
				// this.bShowGame = true;
				uni.navigateTo({
					url:"../game/game"
				})
			},
			//设备回调事件
			callbackUpdateBLEData(data) {
				//如果在监听状态时候隐藏页面，返回
				// console.log(data)
				// if (!this.bShow) return;
				// this.add(JSON.stringify(data));
				if (this.bGamePlaying) return;
				let str = "";
				for (let i = 0; i < data.length; i++) {
					str +=
						"ax:" + data[i].acc.ax + "\n" +
						"ay:" + data[i].acc.ay + "\n" +
						"az:" + data[i].acc.az + "\n" +
						"gx:" + data[i].gyro.gx + "\n" +
						"gy:" + data[i].gyro.gy + "\n" +
						"gz:" + data[i].gyro.gz + "\n" +
						"ms:" + data[i].ms + "\n" ;
				}
				this.text = str;
				// this.text =
				// 	"ax:" + data.acc.ax + "\n" +
				// 	"ay:" + data.acc.ay + "\n" +
				// 	"az:" + data.acc.az + "\n" +
				// 	"gx:" + data.gyro.gx + "\n" +
				// 	"gy:" + data.gyro.gy + "\n" +
				// 	"gz:" + data.gyro.gz + "\n" +
				// 	"ms:" + data.ms;
				// if(this.bListenerAccArray)return;
				// if (this.BLEConnectDevice.usageMode == "hotman") {
				// 	this.gUpdateSandbagAlgorithm({
				// 		data: data,
				// 		callback: (res) => {
				// 			if (res.type == 'hit') {
				// 				console.log("hotman 打击：" + JSON.stringify(res));
				// 			}
				// 		}
				// 	});
				// } else if (this.BLEConnectDevice.usageMode == "general") {
				// 	//这个是普通打击模式
				// } else if (this.BLEConnectDevice.usageMode == "ropeSkipping") {
				// 	this.onConvertDeviceData({
				// 		data: data,
				// 		callback: (outData) => {
				// 			// this.outBLEData = {
				// 			// 	acc: outData.convertAcc,
				// 			// 	gyro: outData.convertGyro
				// 			// }
				// 			// console.log(JSON.stringify(outData),JSON.stringify(data));
				// 			console.log("hotman 打击：" + JSON.stringify(outData))
				// 			data.acc = outData.convertAcc;
				// 			data.gyro = outData.convertGyro;
				// 			//跳绳蓝牙反馈
				// 		}
				// 	});
				// }
			},

			onClearTimeout() {
				// 退出后，清除计时器
				if (this.searchMac) {
					clearTimeout(this.searchMac);
					this.searchMac = null;
				}
				//servicesTimeout
				if (this.getServicesTimeout) {
					clearTimeout(this.getServicesTimeout);
					this.getServicesTimeout = null;
				}
				//写入指令writeMacTimeout
				if (this.writeMacTimeout) {
					clearTimeout(this.writeMacTimeout);
					this.writeMacTimeout = null;
				}

				if (this.searchTimeOut) {
					clearTimeout(this.searchTimeOut);
					this.searchTimeOut = null;
				}
			},
			//监听回调
			onListenerBLE(res) {
				console.log('onListenerBLE:', res, this.saveObj);
				this.add(res.value);
				// if (res.type !== 'mac') return;
				// //如果mac 回调了不用提示
				// if (this.searchMac) {
				// 	clearTimeout(this.searchMac);
				// 	this.searchMac = null;
				// }
				// let _self = this;
				// let mac = res.value;
				// // console.log('mac =====', mac);
				// //用返回的mac 判断，如果非法，则断开连接
				// //就判断一下是否
				// //测试
				// // mac="BB:34:24:22:77:88";
				// // mac="è¾ƒè¾";
				// //判断mac地址是否是合理的格式
				// var tempMacRegExp =
				// 	/[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}/;
				// if (!tempMacRegExp.test(mac)) {
				// 	uni.hideToast();
				// 	uni.showModal({
				// 		title: '验证信息',
				// 		content: '获取Mac地址失败，请重新连接。'
				// 	})
				// 	//关闭当前连接
				// 	_self.onOnlyCloseBLEConnection({
				// 		getSuccess: () => {
				// 			_self.currentItem = null;
				// 			_self.saveObj = null;
				// 		}
				// 	});
				// 	return;
				// }
				// _self.saveObj = Object.assign({}, _self.saveObj, {
				// 	deviceMac: mac,
				// 	bOldDevice: false
				// });
				// _self.addBLEDevice(_self.saveObj);
				// _self.ConnectionSuccess();
			},
			/**
			 * 连接设备成功
			 */
			ConnectionSuccess() {
				for (let i = 0; i < this.devicesList.length; i++) {
					let eq = this.devicesList[i];
					if (
						(eq.ename.indexOf("mobilePhoneBandage") > -1 && this.BLEConnectDevice.id == 0) ||
						(eq.ename.indexOf("hotman") > -1 && this.BLEConnectDevice.id == 1) ||
						(eq.ename.indexOf("BLEHandle") > -1 && this.BLEConnectDevice.id == 2) ||
						(eq.ename.indexOf("rope") > -1 && this.BLEConnectDevice.id == 3)) {

						eq.bRatio = true;
						this.currentItem = eq;
						/**
						 * 已验证的连接设置true
						 */
						// this.$store.state.bVerifiedConnection = true;
						/**
						 * 根据名字兼容旧设备
						 */
						// let _tempMac = this.saveObj.deviceMac.replace(/:/g, "");
						// let _fourStr = _tempMac.substring(_tempMac.length - 4, _tempMac.length);
						// console.log("格式化去掉冒号后mac：", _tempMac, "==", _fourStr);
						// let _tempInt = ble.hex2int(_fourStr);
						// console.log("格式化去掉冒号后_tempInt：", _tempInt);
						// //这里判断旧设备，因为目前这批新设备，用的也是202012的名字，所以这里处理兼容旧设备就行了
						// //用mac判断区分，一起有100个是旧设备，还有部分非标准的mac，根据后台字段判断
						// //如果后台存在bOldDevice 字段，则根据字段判断是否是旧设备
						// if (this.saveObj.name === "BGBox_202012" &&
						// 	_tempInt <= 101 && _tempInt >= 0 || this.saveObj.bOldDevice) {
						// 	this.$store.state.bOldDeviceType = true;
						// 	eq.bOldDevice = true;
						// 	console.log("旧设备:", this.$store.state.bOldDeviceType);
						// } else {
						// 	console.log("旧设备", this.$store.state.bOldDeviceType);
						// 	eq.bOldDevice = false;
						// }

						uni.hideToast();
					}
				}
			},
			onCheckBondDevice() {
				console.log("onCheckBondDevice", this.bTestBondConnect);
				if (this.bTestBondConnect && this.bShow) {
					/**
					 * 假如手机没有匹配，断开连接
					 */
					console.log(2, this.finallyUseDevice);
					this.B_GetBondedDevices({
						deviceId: this.finallyUseDevice.deviceId,
						success: (bondedDevice) => {
							uni.hideToast();
							if (bondedDevice == null) {
								// if (plus.os.name == 'Android') 
								{
									//此问题 华为手机容易出现
									//android手机已配对的设备 不存在，但是app 又直接连接成功了。提示，并且断开app连接
									//1.关闭当前连接
									this.onOnlyCloseBLEConnection({
										getSuccess: () => {
											this.currentItem.bRatio = false;
											this.currentItem = null;
											this.saveObj = null;
										}
									});
									//2.跳转蓝牙设置
									uni.showModal({
										title: '蓝牙配对失败',
										content: '请跳转后点击配对BGBox_2020,成功后手动跳转回哔蹦重新连接。',
										success: (res) => {
											if (res.confirm) {
												this.B_OpenBLESetting();
											}
										}
									})
								}

							}
						}
					});
					this.bTestBondConnect = false;
				} else {
					uni.hideToast();
				}
			},
			onRetryConnectBLESuccess() {
				this.ConnectionSuccess();
			},
			hardCallbackCloseBLE() {

				if (this.BLEConnectDevice == null && this.currentItem && !this.bSwitch) {
					this.currentItem.bRatio = false;
					this.currentItem = null;
					this.saveObj = null;
				}
				//如果限制不走重连
				if (this.bLimitClose) return;

				console.log(this.currentItem, this.oldItem);
				if (this.oldItem && this.currentItem && this.currentItem.id == this.oldItem.id) {
					// this.$store.state.bConnection = false;
					if (this.currentItem.bRatio) {
						uni.showToast({
							title: '设备断开连接！',
							icon: 'none',
							duration: 2000,
							mask: true
						})
						this.currentItem.bRatio = false;
						this.oldItem = null;
						this.currentItem = null;
						//断开连接
						this.$store.state.bVerifiedConnection = false;
					}
				} else if (this.bConnection && this.BLEConnectDevice) {
					//假如匹配过程中断开连接
					this.$store.state.BLEConnectDevice = null;
					this.$store.state.bConnection = false;
					// uni.hideToast();
					// console.log("//假如匹配过程中断开连接");
					uni.showToast({
						title: '连接失败,尝试重新连接。',
						icon: 'none',
						mask: true,
						duration: 2000
					})
					if (this.getServicesTimeout) {
						clearTimeout(this.getServicesTimeout);
						this.getServicesTimeout = null;
					}
				}

			},
			/**
			 * 开始查找设备
			 * */
			startBluetoothDeviceDiscovery() {
				//在页面显示的时候判断是都已经初始化完成蓝牙适配器若成功，则开始查找设备
				let _self = this;
				if (_self.bOpenBluetooth) {
					//1.第一步还是先进行设备搜索
					_self.onCanStart();
				} else {
					_self.initAdapter(() => {
						_self.startBluetoothDeviceDiscovery();
					});
				}
			},
			/**
			 * 通过检测手机连接的设备进行连接，null 的话进行搜索操作
			 */
			onBondedDeviceConnect(data) {
				let _self = this;
				//获取手机本身已连接的硬件，
				//示例
				// {
				// 	"deviceId": "C5:5C:19:04:00:30",
				// 	"name": "BGBox_202012",
				// 	"RSSI": -74,
				// 	"localName": "BGBox_20201",
				// 	"advertisServiceUUIDs": ["00001812-0000-1000-8000-00805F9B34FB", "0000FFF0-0000-1000-8000-00805F9B34FB"]
				// }

				console.log("onBoded ***************:", _self.finallyUseDevice);

				_self.B_GetBondedDevices({
					deviceId: _self.finallyUseDevice == null ? null : _self.finallyUseDevice.deviceId,
					success: (bondedDevice) => {
						console.log("bondedDevice:", bondedDevice);
						//获取已和蓝牙连接的设备
						if (bondedDevice != null) {
							//如果用户匹配了对应的设备，直接用对应的设备来连接
							let setDevice = {
								"deviceId": bondedDevice.address,
								"name": bondedDevice.name,
								"RSSI": -74,
								"localName": "",
								"advertisServiceUUIDs": ["00001812-0000-1000-8000-00805F9B34FB",
									"0000FFF0-0000-1000-8000-00805F9B34FB"
								]
							}

							let obj = Object.assign({}, setDevice, _self.currentItem);
							//finallyUserDevice 就是最后一次使用搜索到的设备信息
							_self.saveObj = Object.assign({}, setDevice, {
								id: _self.currentItem.id
							});
							uni.showToast({
								title: '设备连接中...',
								icon: 'loading',
								duration: 10000,
								mask: true
							})
							console.log("GetBondedDevices:::===", setDevice, _self.currentItem, obj);
							// 先直连，然后判断版本
							_self._onConnectDevice(obj);

							//getBond后发起的连接，不需要检测了
							_self.bTestBondConnect = false;
						} else {
							console.log("没有获取到绑定的设备");
							uni.showToast({
								title: '获取匹配设备失败',
								icon: 'none',
								mask: true
							})
						}
					}
				});
			},
			//开始搜索
			onCanStart() {
				let _self = this;
				uni.showToast({
					title: '设备连接中...',
					icon: 'loading',
					duration: 15000,
					mask: true
				})
				// 1.已经搜索到的，根据蓝牙回调的mac 地址判断合法性。
				uni.startBluetoothDevicesDiscovery({
					allowDuplicatesKey: true,
					success: res => {
						// console.log("startBluetoothDevicesDiscovery:", res);

						_self.bSwitch = false;
						_self.onBluetoothDeviceFound();
					},
					fail: res => {
						console.log("搜索失败！");
						_self.initAdapter(() => {
							_self.startBluetoothDeviceDiscovery();
						});
					}
				});
				// 2.没有搜索到的，一段时间后处理。比如手机已经连接了设备，但是app 里面搜索不到，也没记录使用过的。
				if (_self.searchTimeOut) {
					clearTimeout(_self.searchTimeOut);
					_self.searchTimeOut = null;
				}
				//搜索一段时间后，停止搜索
				if (plus.os.name == 'iOS') {
					_self.searchTimeOut = setTimeout(() => {
						_self.stopBluetoothDevicesDiscovery();
						//搜索失败后,再检查是否和手机配对的设备连接
						_self.onBondedDeviceConnect();
					}, 4000)
				} else {
					_self.searchTimeOut = setTimeout(() => {
						_self.stopBluetoothDevicesDiscovery();
					}, 8000)
				}

			},
			/**
			 * 停止搜索蓝牙设备
			 */
			stopBluetoothDevicesDiscovery() {
				uni.stopBluetoothDevicesDiscovery({
					success: e => {
						// console.log('停止搜索蓝牙设备:' + e.errMsg);
					},
					fail: e => {
						console.log('停止搜索蓝牙设备失败，错误码：' + e.errCode);
					}
				});
			},
			/**
			 * 发现外围设备
			 */
			onBluetoothDeviceFound() {
				let _self = this;
				_self.searchObj = null;
				uni.onBluetoothDeviceFound(res => {
					/**
					 * 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
					 */
					// console.log("onBluetoothDeviceFound:", res);
					res.devices.forEach(device => {
						if (device.name.indexOf('PBox') > -1 || device.name.indexOf('BGBox') > -1 || device
							.name.indexOf('Rope') > -1) {
							//如果搜索的设备名 不是对应当前设备类型，过滤
							// if (device.name.indexOf(_self.currentItem.deviceName) == -1) return;
							if (_self.currentItem.deviceName.indexOf('PBox') == -1 &&
								_self.currentItem.deviceName.indexOf('BGBox') == -1 &&
								_self.currentItem.deviceName.indexOf('Rope') == -1
							) return;
							//寻找到对应设备时候，其余的返回
							if (_self.searchObj) return;
							_self.searchObj = device;
							if (_self.searchTimeOut) {
								clearTimeout(_self.searchTimeOut);
								_self.searchTimeOut = null;
							}
							//currentItem 是mode 页面选中的item
							let obj = Object.assign({}, device, _self.currentItem);

							_self.saveObj = Object.assign({}, {
								id: _self.currentItem.id
							}, device);

							// console.log(device, "****", obj, _self.saveObj, _self.currentItem)
							// 先直连，然后判断版本
							_self._onConnectDevice(obj);
							_self.stopBluetoothDevicesDiscovery();
						}
					})

				})
			},

			onBack() {
				uni.navigateBack({
					delta: 1
				})
			},

			// 提示点击连接设备
			_onConnectDevice(item) {
				//servicesTimeout
				if (this.getServicesTimeout) {
					clearTimeout(this.getServicesTimeout);
					this.getServicesTimeout = null;
				}
				//写入指令writeMacTimeout
				if (this.writeMacTimeout) {
					clearTimeout(this.writeMacTimeout);
					this.writeMacTimeout = null;
				}
				// //如果已经连接，加上服务不存在，直接获取服务
				// if (this.bConnection && this.BLEGetServices && this.BLEGetServices.length == 0) {
				// 	console.log("***直接获取服务*******");
				// 	this.onGetBLEDeviceServices({
				// 		item: item,
				// 		success: (res) => {
				// 			console.log("*****getBLEDeviceServices************");
				// 			//连接成功了,设置旧的item
				// 			this.oldItem = this.currentItem;

				// 			setTimeout(() => {
				// 				this.saveObj = Object.assign({}, this
				// 					.saveObj, {
				// 						deviceMac: '',
				// 						bOldDevice: false
				// 					});
				// 				this.addBLEDevice(this.saveObj);
				// 				this.ConnectionSuccess();

				// 			}, 2000)
				// 			// uni.showToast({
				// 			// 	title: '正在验证设备信息...',
				// 			// 	icon: 'loading',
				// 			// 	duration: 10000,
				// 			// 	mask: true
				// 			// })
				// 			// // 初始化服务后，获取版本，判断
				// 			// // 停止蓝牙加速计
				// 			// this.writeMacTimeout = setTimeout(() => {
				// 			// 	//todo 停止蓝牙加速计
				// 			// 	// this.onWriteBLEConnectionValue({
				// 			// 	// 	value: "4"
				// 			// 	// });
				// 			// 	// 发送获取mac
				// 			// 	this.onWriteBLEConnectionValue({
				// 			// 		value: "M"
				// 			// 	});
				// 			// 	if (this.searchMac) {
				// 			// 		clearTimeout(this.searchMac);
				// 			// 		this.searchMac = null;
				// 			// 	}
				// 			// 	this.searchMac = setTimeout(() => {
				// 			// 		uni.hideToast();
				// 			// 		uni.showModal({
				// 			// 			title: '提示',
				// 			// 			content: '检测设备失败。\r\n1.再次尝试连接。\r\n2.或者重启手柄(Reset键位也可)后再尝试连接'
				// 			// 		})
				// 			// 		this.onOnlyCloseBLEConnection({
				// 			// 			getSuccess: () => {
				// 			// 				if (this.currentItem)
				// 			// 					this.currentItem.bRatio = false;

				// 			// 				this.currentItem = null;
				// 			// 			}
				// 			// 		});
				// 			// 	}, 6000)
				// 			// }, 3000)

				// 		}
				// 	});
				// 	return;
				// }
				//创建一个连接，需要对应close
				this.onCreateBLESuccess({
					item: item,
					getSuccess: () => {
						// console.log("****创建一个连接*******");
						this.getServicesTimeout = setTimeout(() => {
							this.onGetBLEDeviceServices({
								item: item,
								success: (res) => {
									// console.log("******getBLEDeviceServices************", res);
									//连接成功了,设置旧的item
									this.oldItem = this.currentItem;
									setTimeout(() => {
										this.saveObj = Object.assign({}, this
											.saveObj, {
												deviceMac: '',
												bOldDevice: false
											});
										this.addBLEDevice(this.saveObj);
										this.ConnectionSuccess();

									}, 2000)

									// uni.showToast({
									// 	title: '正在验证设备信息...',
									// 	icon: 'loading',
									// 	duration: 10000,
									// 	mask: true
									// })
									// // 初始化服务后，获取版本，判断
									// // 停止蓝牙加速计
									// this.writeMacTimeout = setTimeout(() => {
									// 	//todo 停止蓝牙加速计
									// 	// this.onWriteBLEConnectionValue({
									// 	// 	value: "4"
									// 	// });
									// 	// 发送获取mac
									// 	this.onWriteBLEConnectionValue({
									// 		value: "M"
									// 	});
									// 	if (this.searchMac) {
									// 		clearTimeout(this.searchMac);
									// 		this.searchMac = null;
									// 	}
									// 	this.searchMac = setTimeout(() => {
									// 		uni.hideToast();
									// 		uni.showModal({
									// 			title: '提示',
									// 			content: '检测设备失败。\r\n1.再次尝试连接。\r\n2.或者重启手柄(Reset键位也可)后再尝试连接'
									// 		})
									// 		this.onOnlyCloseBLEConnection({
									// 			getSuccess: () => {
									// 				if (this
									// 					.currentItem
									// 				)
									// 					this
									// 					.currentItem
									// 					.bRatio =
									// 					false;

									// 				this.currentItem =
									// 					null;
									// 			}
									// 		});
									// 	}, 6000)
									// }, 3000)

								}
							});
						}, 2500);
					}
				})
			},

			_onRadio(item, event) {
				console.log(this.bConnection, this.BLEGetServices);
				if (this.bConnection && this.BLEGetServices && this.BLEGetServices.length == 0) {
					console.log("***直接获取服务*******");
					this.onGetBLEDeviceServices({
						item: item,
						success: (res) => {
							console.log("*****getBLEDeviceServices************");
							//连接成功了,设置旧的item
							this.oldItem = this.currentItem;

							setTimeout(() => {
								this.saveObj = Object.assign({}, this
									.saveObj, {
										deviceMac: '',
										bOldDevice: false
									});
								this.addBLEDevice(this.saveObj);
								this.ConnectionSuccess();

							}, 2000)

						}
					});
					return;
				}
				if (!item.bRatio) {
					//设置默认值
					this.bTestBondConnect = true;
					if (this.BLEConnectDevice) {
						this.onOnlyCloseBLEConnection({
							getSuccess: () => {
								if (this.currentItem)
									this.currentItem.bRatio = false;

								this.currentItem = null;
								this.currentItem = item;
								this.bSwitch = true;
								console.log("this.currentItem1 ==:", this.currentItem, this
									.bOpenBluetooth,
									this.BLEConnectDevice);
								this.startBluetoothDeviceDiscovery();
							}
						});
						return;
					}
					this.currentItem = null;
					this.currentItem = item;
					// console.log("this.currentItem ==2:", this.currentItem, this.bOpenBluetooth, this.BLEConnectDevice);
					this.startBluetoothDeviceDiscovery();
				}
			},
			onClise() {
				this.B_CloseBLEConnection({
					deviceId: "C5:5C:19:04:01:22"
				});
			},
			//跳转进入升级页面,
			onNavUpdateDevice() {
				//需要连接设备后，才能进入升级
				if (!this.currentItem || !this.currentItem.bRatio || !this.BLEConnectDevice) {
					uni.showToast({
						title: '请先连接硬件!',
						icon: 'none'
					})
					return;
				}
				this.bLimitClose = true;
				uni.navigateTo({
					// url: "../devices-update/devices-update?deviceType=" + this.option.deviceType
					url: "../devices-update/devices-update"
				})
			},

			add: function(data) {
				// console.log(data)

				if (this.extraLine.length > 500) {
					this.extraLine = [];
					this.text = this.extraLine.join('\n');
				}

				this.extraLine.push(data);
				this.text = this.extraLine.join('\n');


			},
			remove: function(e) {
				if (this.extraLine.length > 0) {
					// this.extraLine.pop();
					this.extraLine = [];
					this.text = this.extraLine.join('\n');
				}
			},
			writeBLEValue(str) {
				this.onWriteBLEConnectionValue({
					value: str
				})
			},
			onKeyInput: function(event) {
				this.onSetLocationGameUrl(event.target.value);
			}
		}

	}
</script>

<style>
	.hardware-border {
		border: 1rpx solid #9898FF;
		box-sizing: border-box;
	}

	.swiper {
		flex: 1;
		background-color: rgba(0, 0, 0, 0.32);
	}

	.swiper-item {
		flex: 1;
	}

	.bluetooth-guide-number {
		border-radius: 18px;
		border-width: 1rpx;
		width: 30px;
		height: 30px;
		text-align: center;
		line-height: 30px;
		color: #000000;
		/* border-color: #FFFFFF; */
		background-color: #FFFFFF;
		margin-right: 13px;
	}

	.text-box {
		margin: 20rpx;
		display: flex;
		width: 95%;
		min-height: 300rpx;
		max-height: 600rpx;
		background-color: #c6c6c6;
		justify-content: center;
		align-items: center;
		text-align: left;
		font-size: 30upx;
		color: #353535;
		line-height: 1.8;
		border: 1rpx solid #555555;
		overflow-y: hidden;
	}


	.content {
		align-content: center;
		height: 300rpx;
		background-color: #F4F5F6;
	}

	.uni-input {
		border: 1rpx solid #000000;
	}
</style>
