Browse Source

1.添加部分命令

slambb 3 years ago
parent
commit
6d4522794a
3 changed files with 98 additions and 39 deletions
  1. 41 4
      pages/game/subGame/subGame.nvue
  2. 39 28
      pages/index/index.vue
  3. 18 7
      util/util-js/store.js

+ 41 - 4
pages/game/subGame/subGame.nvue

@@ -63,8 +63,16 @@
 				_self.sendMessage("onSubHide", {
 					msg: '退出页面'
 				});
-
+				uni.$off('updateBLEDeviceData', _self.BLECallback);
 				uni.$off('updateBLEDeviceData', _self.gWatchBLEUpdate);
+				
+				if (_self.BLEConnectDevice && _self.BLEConnectDevice.deviceType == "BLEHandle") {
+					//开启设备回调 3/4关闭
+					_self.onWriteBLEConnectionValue({
+						value: "4"
+					});
+				} 
+				
 				_self.$store.state.bGamePlaying = false;
 				uni.$emit("game-unload", {});
 
@@ -87,7 +95,7 @@
 			uni.$emit("game-unload");
 		},
 		methods: {
-			...mapMutations([]),
+			...mapMutations(["onWriteBLEConnectionValue"]),
 			navBack() {
 				this.$store.state.bGamePlaying = false;
 				// uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
@@ -138,12 +146,23 @@
 					// setTimeout(() => {
 					// 	uni.$on('updateBLEDeviceData', this.gWatchBLEUpdate);
 					// }, 500)
+					uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
 					uni.$on('updateBLEDeviceData', this.gWatchBLEUpdate);
 				} else if (temp.funName == "onStopAccAndGyro") {
 					this.bMyAttitudeListen = false;
 					//监听蓝牙回调
 					uni.$off('updateBLEDeviceData', this.gWatchBLEUpdate);
-				}
+				} else if (temp.funName == "addDeviceUpdateListener") {
+					uni.$off('updateBLEDeviceData', this.BLECallback);
+					uni.$on('updateBLEDeviceData', this.BLECallback);
+				} else if (temp.funName == "closeDeviceUpdateListener") {
+					uni.$off('updateBLEDeviceData', this.BLECallback);
+				} else if (temp.funName == "writeBLEConnectionValue") {
+					if (!gameData.value || gameData.value == "") return;
+
+					//蓝牙写入数据
+					this.onWriteBLEConnectionValue(gameData);
+				} 
 			},
 			onPageStart: function(e) {
 				// 监听页面加载成功
@@ -173,7 +192,25 @@
 				//添加计数
 				data.count = ++this.count;
 				this.sendMessage("updateAccAndGyro", data);
-			}
+			},
+			
+			BLECallback(data) {
+				let dataType = 'Box';
+				// if (data.hasOwnProperty("ax")) {
+				// 	dataType = "Accelerometer"
+				// }else if(data.hasOwnProperty("gx")){
+				// 	dataType = "Gyroscope"
+				// }
+				// 设备回调后面用这个接口
+				let BLEState = {
+					device: this.BLEConnectDevice, //连接的蓝牙设备
+					instructionState: this.instructionState, //开启的指令状态
+					currentInstruction: this.currentInstruction, //当前发送的指令记录
+					dataType: dataType, //回调的数据类型
+					data: data //蓝牙回调的数据
+				}
+				this.sendMessage("onDeviceUpdateData", BLEState);
+			},
 		}
 	}
 </script>

+ 39 - 28
pages/index/index.vue

@@ -1,6 +1,6 @@
 <template>
 	<view>
-		<view style="height: 50px;"></view>
+		<view style="height: 20px;"></view>
 		<view>
 			<view class="title">Cocos预览地址:{{LocationGameUrl}}</view>
 			<input class="uni-input" :value="LocationGameUrl" @input="onKeyInput" placeholder="输入cocos预览地址" />
@@ -33,8 +33,10 @@
 			<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>
+			<!-- <button style="margin: 10rpx;" type="primary" @click="writeBLEValue('H')">开启高速</button> -->
+			<!-- <button style="margin: 10rpx;" type="primary" @click="writeBLEValue('h')">关闭高速</button> -->
+			<button style="margin: 10rpx;" type="primary" @click="writeBLEValue('3')">开启</button>
+			<button style="margin: 10rpx;" type="primary" @click="writeBLEValue('4')">关闭</button>
 		</view>
 		<scroll-view class="text-box" scroll-y="true">
 			<scroll-view scroll-x="true">
@@ -42,16 +44,17 @@
 			</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 type="warn" :disabled="extraLine.length<=0" @click="remove">
+			清除输入框数据
+		</button>
 
-		<button style="margin-top: 2px;" type="warn" @click="onEmit">
+
+		<!-- <button style="margin-top: 2px;" type="warn" @click="onEmit">
 			模拟发送
-		</button>
+		</button> -->
 
 		<!-- <button style="margin-top: 2px;" type="warn" @click="onClise">
 			close
@@ -79,7 +82,7 @@
 		computed: mapState(['bOpenBluetooth', 'bOpenSuccess', 'bListenAdapterStateChange', 'bListenDeviceFound',
 			'BLEConnectDevice', 'BLEGetServices', 'cIndex', 'bConnection', 'bVerifiedConnection', 'BLEInfoList',
 			'BLEDeviceShowList', 'finallyUseDevice', 'systemInfo', 'guideUnlockState', 'currentModeIndex',
-			"bListenerAccArray", 'LocationGameUrl','bGamePlaying'
+			"bListenerAccArray", 'LocationGameUrl', 'bGamePlaying'
 		]),
 		data() {
 			return {
@@ -128,8 +131,7 @@
 			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], {
@@ -145,8 +147,6 @@
 			 * $on 之后要调用 $off,不然会重复绑定
 			 */
 			this.onGetLocationGameUrl();
-			
-
 			uni.$on("log", this.add)
 
 		},
@@ -154,21 +154,22 @@
 			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;
+			uni.$on('updateBLEDeviceData', this.callbackUpdateBLEData);
+			
 		},
 		onReady() {},
 		onHide() {
 			//如果蓝牙弹出匹配框,会触发onHide。这时候处理蓝牙连接流程检测应等onShow 时候,再检测
 			this.bShow = false;
+			
+			uni.$off('updateBLEDeviceData', this.callbackUpdateBLEData);
 		},
 		methods: {
 			...mapMutations(['initAdapter', 'onCreateBLESuccess', 'onGetBLEDeviceServices', 'onOnlyCloseBLEConnection',
@@ -187,7 +188,7 @@
 				// uni.$emit("showGame");
 				// this.bShowGame = true;
 				uni.navigateTo({
-					url:"../game/game"
+					url: "../game/game"
 				})
 			},
 			//设备回调事件
@@ -197,17 +198,27 @@
 				// 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" ;
-				}
+				// 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" ;
+				// }
+				let str =
+					"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" +
+					"min:" + data.min + "\n" +
+					"s:" + data.s + "\n" +
+					"ms:" + data.ms + "\n";
 				this.text = str;
 				// this.text =
 				// 	"ax:" + data.acc.ax + "\n" +

+ 18 - 7
util/util-js/store.js

@@ -320,7 +320,7 @@ const store = new Vuex.Store({
 		//本地游戏地址
 		// http://192.168.0.112:7456/build/index.html
 		// http://127.0.0.1:7456/build/index.html
-		LocationGameUrl: "", //192.168.1.12:7456
+		LocationGameUrl: "192.168.0.108:7456", //192.168.1.12:7456
 
 		//快速打击对象
 		filter: null,
@@ -1867,15 +1867,26 @@ const store = new Vuex.Store({
 					//秒
 					let s = BLE.hex2int(resValue.substr(34, 2));
 					box["handle"] = BLE.hexToString(handle);
+					// box["acc"] = {
+					// 	ax,
+					// 	ay,
+					// 	az
+					// };
+					// box["gyro"] = {
+					// 	gx,
+					// 	gy,
+					// 	gz
+					// };
+					
 					box["acc"] = {
-						ax,
-						ay,
-						az
+						ax: -ay,
+						ay: -ax,
+						az: az
 					};
 					box["gyro"] = {
-						gx,
-						gy,
-						gz
+						gx: -gy,
+						gy: -gx,
+						gz: gz
 					};
 
 					box["min"] = min;