Selaa lähdekoodia

1.修改测试工具

slambb 3 vuotta sitten
vanhempi
sitoutus
9ff170f2e3
3 muutettua tiedostoa jossa 354 lisäystä ja 119 poistoa
  1. 160 15
      pages/HeartRate/HeartRate.vue
  2. 101 100
      util/util-js/devices.js
  3. 93 4
      util/util-js/store.js

+ 160 - 15
pages/HeartRate/HeartRate.vue

@@ -86,7 +86,8 @@
 							<view style="margin-bottom: 20rpx; font-weight: bold; font-size: 18px; color: #606162;">
 								{{item.cname}}
 							</view>
-							<view v-if="item.bRatio" style="font-size: 12px; white-space:pre-wrap;" class="make-text-bPurple">
+							<view v-if="item.bRatio" style="font-size: 12px; white-space:pre-wrap;"
+								class="make-text-bPurple">
 								已连接
 							</view>
 							<view v-else style="color: #8D8D8D;">未连接</view>
@@ -94,8 +95,9 @@
 
 					</view>
 					<view class="position-absolute-right-top">
-						<image style="width: 239rpx;height: 173rpx;" :src="item.bRatio?'/static/heart/radio-r@2x.png':'/static/heart/radio-g@2x.png'"
-						mode="aspectFit" @tap="_onRadio(item,$event)"></image>
+						<image style="width: 239rpx;height: 173rpx;"
+							:src="item.bRatio?'/static/heart/radio-r@2x.png':'/static/heart/radio-g@2x.png'"
+							mode="aspectFit" @tap="_onRadio(item,$event)"></image>
 					</view>
 					<!-- <image style="margin-right: 60rpx; width: 60rpx;height: 60rpx;"
 						:src="item.bRatio?'/static/devicesOther/radio-b.png':'/static/devicesOther/radio-g.png'"
@@ -113,11 +115,11 @@
 				</view>
 				<view class="position-absolute-right-top">
 					<image style="width: 495rpx ;height: 173rpx;" src="../../static/heart/b_start@2x.png"></image>
-					<view v-if="timer" class="position-absolute-center flex text-18px text-bold"  @click="pauseTime()">
+					<view v-if="timer" class="position-absolute-center flex text-18px text-bold" @click="pauseTime()">
 						<image style="width: 53rpx;height: 53rpx;" src="../../static/heart/pause@2x.png"></image>
 						<view style="color: #4D857C;width: 150rpx;margin-left: 30rpx;">暂停计时</view>
 					</view>
-					<view v-else class="position-absolute-center flex text-18px text-bold"  @click="startTimer()">
+					<view v-else class="position-absolute-center flex text-18px text-bold" @click="startTimer()">
 						<image style="width: 53rpx;height: 53rpx;" src="../../static/heart/play@2x.png"></image>
 						<view style="color: #4D857C;width: 150rpx;margin-left: 30rpx;">开始计时</view>
 					</view>
@@ -131,8 +133,19 @@
 
 		</view>
 
-
-
+		<!-- <button class="margin-xl" @click="B_OnReadBLECharacteristicValue">B_OnReadBLECharacteristicValue</button>
+		<button class="margin-xl" @click="B_WriteBLECharacteristicValueFromArray">ValueFromArray</button> -->
+		<!-- <button class="margin-xl" @click="HeartBLE_Init">HeartBLE_Init</button> -->
+		<button class="margin-xl" @click="HeartBLE_StartScan">HeartBLE_StartScan</button>
+		<button class="margin-xl" @click="HeartBLE_StopScan">HeartBLE_StopScan</button>
+		<button class="margin-xl" @click="HeartBLE_DisConnect">HeartBLE_DisConnect</button>
+		<button class="margin-xl" @click="HeartBLE_Normal">HeartBLE_Normal</button>
+		<button class="margin-xl" @click="HeartBLE_SetLightEnable">HeartBLE_SetLightEnable</button>
+		<button class="margin-xl" @click="HeartBLE_SetLightRGB">HeartBLE_SetLightRGB</button>
+		<button class="margin-xl" @click="HeartBLE_GetLightStatus">HeartBLE_GetLightStatus</button>
+		<button class="margin-xl" @click="HeartBLE_SetLightBrightness">HeartBLE_SetLightBrightness</button>
+		<button class="margin-xl" @click="HeartBLE_Shutdown">HeartBLE_Shutdown</button>
+		<view class="margin-bottom-xl"></view>
 
 
 
@@ -317,6 +330,11 @@
 			// #ifdef APP-PLUS
 			//初始化语音
 			this.initTextToSpeech();
+
+			//初始化蓝牙
+			this.HeartBLE_Init();
+			plus.globalEvent.addEventListener("WearManagerCallbacks", this.WearManagerCallbacks);
+
 			this.$set(this.treatmentStatu, 'checked', true);
 			//如果没有开启麦克风。可能导致唤醒失败
 			//轮询检测麦克风权限
@@ -350,7 +368,9 @@
 				'onGetRSSITransDistance',
 				'addBLEDevice', 'onWriteBLEConnectionValue', 'deleteBLEDevice', 'B_GetBondedDevices',
 				'B_OpenBLESetting', 'setGuideUnlockState', 'switchLevelList', 'onSetLocationGameUrl',
-				'onGetLocationGameUrl', 'onTestEmit', 'B_CloseBLEConnection', 'B_OnSetMTU'
+				'onGetLocationGameUrl', 'onTestEmit', 'B_CloseBLEConnection', 'B_OnSetMTU',
+				'B_OnReadBLECharacteristicValue',
+				'B_WriteBLECharacteristicValueFromArray'
 			]),
 			onEmit() {
 				this.onTestEmit();
@@ -708,9 +728,9 @@
 					// console.log("onBluetoothDeviceFound:", res); Fitcent_
 					res.devices.forEach(device => {
 						if (
-						// device.name.indexOf('CL809') > -1 ||
-						// 	device.name.indexOf('CL806') > -1 ||
-						// 	device.name.indexOf('CL831') > -1 ||
+							// device.name.indexOf('CL809') > -1 ||
+							// 	device.name.indexOf('CL806') > -1 ||
+							// 	device.name.indexOf('CL831') > -1 ||
 							//CL838
 							device.name.indexOf('CL') > -1
 						) {
@@ -956,7 +976,7 @@
 				// console.log(ret);
 				uni.showToast({
 					title: "releaseToSpeech",
-					icon:'none'
+					icon: 'none'
 				})
 			},
 			playSpeak(text) {
@@ -975,7 +995,7 @@
 				console.log(ret);
 				uni.showToast({
 					title: "stopSpeak",
-					icon:'none'
+					icon: 'none'
 				})
 			},
 
@@ -1010,6 +1030,131 @@
 				// })
 			},
 
+
+			HeartBLE_Init() {
+				// 调用同步方法
+				var ret = testModule.HeartBLE_Init();
+				console.log("HeartBLE_Init:", ret);
+			},
+
+			HeartBLE_StartScan() {
+				// 调用同步方法
+				var ret = testModule.HeartBLE_StartScan();
+				console.log("HeartBLE_StartScan:", ret);
+			},
+			HeartBLE_StopScan() {
+				// 调用同步方法
+				var ret = testModule.HeartBLE_StopScan();
+				console.log("HeartBLE_StopScan:", ret);
+			},
+			HeartBLE_DisConnect() {
+				// 调用同步方法
+				var ret = testModule.HeartBLE_DisConnect();
+				console.log("HeartBLE_DisConnect:", ret);
+			},
+
+			HeartBLE_Normal() {
+				var ret = testModule.HeartBLE_Normal();
+				console.log("HeartBLE_Normal:", ret);
+			},
+
+			HeartBLE_SetLightEnable() {
+				var ret = testModule.HeartBLE_SetLightEnable({
+					enable: false
+				});
+				console.log("HeartBLE_SetLightEnable:", ret);
+			},
+
+			HeartBLE_SetLightRGB() {
+				var ret = testModule.HeartBLE_SetLightRGB({
+					red: Math.random() * 255,
+					green: Math.random() * 255,
+					blue: Math.random() * 255
+				});
+				console.log("HeartBLE_SetLightRGB:", ret);
+			},
+
+			HeartBLE_GetLightStatus() {
+				var ret = testModule.HeartBLE_GetLightStatus();
+				console.log("HeartBLE_GetLightStatus:", ret);
+			},
+
+			HeartBLE_SetLightBrightness() {
+				var ret = testModule.HeartBLE_SetLightBrightness({
+					brightness: Math.random() * 100
+				}); //0-100
+				console.log("HeartBLE_SetLightBrightness:", ret);
+			},
+
+			HeartBLE_Shutdown() {
+				var ret = testModule.HeartBLE_Shutdown();
+				console.log("HeartBLE_Shutdown:", ret);
+			},
+
+			WearManagerCallbacks(data) {
+				console.log("WearManagerCallbacks:" + JSON.stringify(data));
+				switch (data.type) {
+					case "onBatteryLevelChanged":
+						this.onBatteryLevelChangedCallback(data);
+						break;
+					case "onKeyEventCallback":
+						this.onKeyEventCallback(data);
+						break;
+					case "onHeartRateMeasurementReceived":
+						this.onHeartRateMeasurementReceivedCallback(data);
+						break;
+					case "onConnecting":
+						console.log(data);
+						break;
+					default:
+						break;
+				}
+			},
+			onBatteryLevelChangedCallback(data) {
+
+				uni.showToast({
+					title: '电量:' + data.batteryLevel + '%',
+					icon: 'none'
+				})
+			},
+
+			onKeyEventCallback(data) {
+				uni.showToast({
+					title: data.name,
+					icon: 'none'
+				})
+				if (data.isKeyDown) {
+					this.playSpeak('当前心率' + this.heartRate);
+				}
+			},
+
+			onHeartRateMeasurementReceivedCallback(data) {
+				this.heartRate = data.heartRate;
+				if (this.heartRate != 0) {
+					if (!this.bLoading) {
+						this.bLoading = true;
+						this.loadingCount = 0;
+					}
+					if (!this.bReach) {
+						if (this.heartRate >= this.rateMin && this.heartRate <= this.rateMax) {
+							this.playSpeak("达到运动心率" + this.heartRate);
+							// console.log("达到运动心率");
+							//达到一次目标后播报
+							this.bReach = true;
+						}
+					} else {
+						if (this.heartRate < this.rateMin || this.heartRate > this.rateMax) {
+							this.playSpeak("当前心率" + this.heartRate);
+						}
+					}
+				} else if (this.bTimerPlay) {
+					this.bLoading = false;
+					//暂停心率
+					this.pauseTime();
+				}
+
+			},
+
 			/**
 			 * 计时器
 			 */
@@ -1202,10 +1347,10 @@
 </script>
 
 <style>
-	page{
+	page {
 		background-color: #FFFFFF;
 	}
-	
+
 	.hardware-border {
 		border: 1rpx solid #9898FF;
 		box-sizing: border-box;

+ 101 - 100
util/util-js/devices.js

@@ -1,104 +1,105 @@
-
 const getDeviceList = function() {
-	
-	let deviceList = [
-			{
-				id: 0,
-				cname: "CL806",
-				ename: "CL806_Bandage",
-				icon: "/static/devicesIcon/bandage.png",
-				mIcon: "/static/devicesIcon/bandage.png",
-				bRatio: false,
-				usageMode: 'CL806',
-				describe: '胸口绑带',
-				limitType: 'rebound', //限制回弹版本
-				deviceType: 'Hardware', //指的是使用手机本身加速计计算
-				deviceName: 'CL806', //连接硬件名称
-				limitDis: 0.5,
-				/**
-				 * 主服务的 uuid Cofing
-				 */
-				primaryUUID: '', //蓝牙主服务id
-				PRIMARY_SERVICE: '0000180D-0000-1000-8000-00805f9b34fb',
-				PRIMARY_WRITE: '00002A38-0000-1000-8000-00805f9b34fb',
-				PRIMARY_NOTIFY: '00002A37-0000-1000-8000-00805f9b34fb',
-				/***********/
-				currentVersion: '', //当前固件版本
-				latestVersion: '' //服务器最新固件版本
-			},
-			{
-				id: 1,
-				cname: "CL831",
-				ename: "CL831_Bandage",
-				icon: "/static/devicesIcon/bandage.png",
-				mIcon: "/static/devicesIcon/bandage.png",
-				bRatio: false,
-				usageMode: 'CL831',
-				describe: '手部绑带',
-				limitType: 'rebound', //限制回弹版本
-				deviceType: 'Hardware', //指的是使用手机本身加速计计算
-				deviceName: 'CL831', //连接硬件名称
-				limitDis: 0.5,
-				/**
-				 * 主服务的 uuid Cofing
-				 */
-				primaryUUID: '', //蓝牙主服务id
-				PRIMARY_SERVICE: '0000180D-0000-1000-8000-00805f9b34fb',
-				PRIMARY_WRITE: '00002A38-0000-1000-8000-00805f9b34fb',
-				PRIMARY_NOTIFY: '00002A37-0000-1000-8000-00805f9b34fb',
-				/***********/
-				currentVersion: '', //当前固件版本
-				latestVersion: '' //服务器最新固件版本
-			},
-			{
-				id: 2,
-				cname: "CL809",
-				ename: "CL809_Bandage",
-				icon: "/static/devicesIcon/bandage.png",
-				mIcon: "/static/devicesIcon/bandage.png",
-				bRatio: false,
-				usageMode: 'CL809',
-				describe: '胸口绑带',
-				limitType: 'rebound', //限制回弹版本
-				deviceType: 'Hardware', //指的是使用手机本身加速计计算
-				deviceName: 'CL809', //连接硬件名称
-				limitDis: 0.5,
-				/**
-				 * 主服务的 uuid Cofing
-				 */
-				primaryUUID: '', //蓝牙主服务id
-				PRIMARY_SERVICE: '0000180D-0000-1000-8000-00805f9b34fb',
-				PRIMARY_WRITE: '00002A38-0000-1000-8000-00805f9b34fb',
-				PRIMARY_NOTIFY: '00002A37-0000-1000-8000-00805f9b34fb',
-				/***********/
-				currentVersion: '', //当前固件版本
-				latestVersion: '' //服务器最新固件版本
-			},
-			{
-				id: 3,
-				cname: "心率带",//CL838
-				ename: "CL_Bandage", //CL838_Bandage
-				icon: "/static/heart/device838@2x.png",
-				mIcon: "/static/heart/device838@2x.png",
-				bRatio: false,
-				usageMode: 'CL', //CL838
-				describe: '心率带',
-				limitType: 'rebound', //限制回弹版本
-				deviceType: 'Hardware', //指的是使用手机本身加速计计算
-				deviceName: 'CL', //连接硬件名称 CL838
-				limitDis: 0.5,
-				/**
-				 * 主服务的 uuid Cofing
-				 */
-				primaryUUID: '', //蓝牙主服务id
-				PRIMARY_SERVICE: '0000180D-0000-1000-8000-00805f9b34fb',
-				PRIMARY_WRITE: '00002A38-0000-1000-8000-00805f9b34fb',
-				PRIMARY_NOTIFY: '00002A37-0000-1000-8000-00805f9b34fb',
-				/***********/
-				currentVersion: '', //当前固件版本
-				latestVersion: '' //服务器最新固件版本
-			}
-		];
+
+	let deviceList = [{
+			id: 0,
+			cname: "CL806",
+			ename: "CL806_Bandage",
+			icon: "/static/devicesIcon/bandage.png",
+			mIcon: "/static/devicesIcon/bandage.png",
+			bRatio: false,
+			usageMode: 'CL806',
+			describe: '胸口绑带',
+			limitType: 'rebound', //限制回弹版本
+			deviceType: 'Hardware', //指的是使用手机本身加速计计算
+			deviceName: 'CL806', //连接硬件名称
+			limitDis: 0.5,
+			/**
+			 * 主服务的 uuid Cofing
+			 */
+			primaryUUID: '', //蓝牙主服务id
+			PRIMARY_SERVICE: '0000180D-0000-1000-8000-00805f9b34fb',
+			PRIMARY_WRITE: '00002A38-0000-1000-8000-00805f9b34fb',
+			PRIMARY_NOTIFY: '00002A37-0000-1000-8000-00805f9b34fb',
+			/***********/
+			currentVersion: '', //当前固件版本
+			latestVersion: '' //服务器最新固件版本
+		},
+		{
+			id: 1,
+			cname: "CL831",
+			ename: "CL831_Bandage",
+			icon: "/static/devicesIcon/bandage.png",
+			mIcon: "/static/devicesIcon/bandage.png",
+			bRatio: false,
+			usageMode: 'CL831',
+			describe: '手部绑带',
+			limitType: 'rebound', //限制回弹版本
+			deviceType: 'Hardware', //指的是使用手机本身加速计计算
+			deviceName: 'CL831', //连接硬件名称
+			limitDis: 0.5,
+			/**
+			 * 主服务的 uuid Cofing
+			 */
+			primaryUUID: '', //蓝牙主服务id
+			PRIMARY_SERVICE: '0000180D-0000-1000-8000-00805f9b34fb',
+			PRIMARY_WRITE: '00002A38-0000-1000-8000-00805f9b34fb',
+			PRIMARY_NOTIFY: '00002A37-0000-1000-8000-00805f9b34fb',
+			/***********/
+			currentVersion: '', //当前固件版本
+			latestVersion: '' //服务器最新固件版本
+		},
+		{
+			id: 2,
+			cname: "CL809",
+			ename: "CL809_Bandage",
+			icon: "/static/devicesIcon/bandage.png",
+			mIcon: "/static/devicesIcon/bandage.png",
+			bRatio: false,
+			usageMode: 'CL809',
+			describe: '胸口绑带',
+			limitType: 'rebound', //限制回弹版本
+			deviceType: 'Hardware', //指的是使用手机本身加速计计算
+			deviceName: 'CL809', //连接硬件名称
+			limitDis: 0.5,
+			/**
+			 * 主服务的 uuid Cofing
+			 */
+			primaryUUID: '', //蓝牙主服务id
+			PRIMARY_SERVICE: '0000180D-0000-1000-8000-00805f9b34fb',
+			PRIMARY_WRITE: '00002A38-0000-1000-8000-00805f9b34fb',
+			PRIMARY_NOTIFY: '00002A37-0000-1000-8000-00805f9b34fb',
+			/***********/
+			currentVersion: '', //当前固件版本
+			latestVersion: '' //服务器最新固件版本
+		},
+		{
+			id: 3,
+			cname: "心率带", //CL838
+			ename: "CL_Bandage", //CL838_Bandage
+			icon: "/static/heart/device838@2x.png",
+			mIcon: "/static/heart/device838@2x.png",
+			bRatio: false,
+			usageMode: 'CL', //CL838
+			describe: '心率带',
+			limitType: 'rebound', //限制回弹版本
+			deviceType: 'Hardware', //指的是使用手机本身加速计计算
+			deviceName: 'CL', //连接硬件名称 CL838
+			limitDis: 0.5,
+			/**
+			 * 主服务的 uuid Cofing
+			 */
+			primaryUUID: '', //蓝牙主服务id
+			PRIMARY_SERVICE: '0000180D-0000-1000-8000-00805f9b34fb',
+			PRIMARY_WRITE: '00002A38-0000-1000-8000-00805f9b34fb',
+			PRIMARY_NOTIFY: '00002A37-0000-1000-8000-00805f9b34fb',
+			//电量
+			BATTERY_SERVICE_UUID: '0000180F-0000-1000-8000-00805f9b34fb',
+			BATTERY_LEVEL_CHARACTERISTIC_UUID: '00002A19-0000-1000-8000-00805f9b34fb',
+			/***********/
+			currentVersion: '', //当前固件版本
+			latestVersion: '' //服务器最新固件版本
+		}
+	];
 
 	return deviceList;
 }

+ 93 - 4
util/util-js/store.js

@@ -1740,10 +1740,32 @@ const store = new Vuex.Store({
 				var resValue = BLE.ab2hext(res.value); //16进制字符串
 
 				if (res.serviceId.toLocaleLowerCase() === "0000180d-0000-1000-8000-00805f9b34fb") {
-					uni.$emit('updateBLEDeviceData', {
-						ab:mValue,
-						hex:resValue
-					});
+					// console.log("1="+res.serviceId.toLocaleLowerCase());
+					// uni.$emit('updateBLEDeviceData', {
+					// 	ab:mValue,
+					// 	hex:resValue
+					// });
+					return;
+				} else {
+					console.log(res.serviceId.toLocaleLowerCase());
+
+					function getIntValue(formatType, offset) {
+						console.log("getIntValue:" + formatType + " = " + offset);
+						let array = new Uint8Array(mValue);
+						console.log(array);
+						console.log(array[offset]);
+
+						switch (formatType) {
+							case 17:
+								return ble.unsignedByteToInt(array[offset]);
+							case 18:
+								return ble.unsignedBytesToInt(array[offset], array[offset + 1]);
+						}
+					}
+					// console.log(res, res.serviceId.toLocaleLowerCase());
+					let offset = 0;
+					let flags = getIntValue(17, offset);
+					console.log("flags:", flags);
 					return;
 				}
 
@@ -1808,6 +1830,20 @@ const store = new Vuex.Store({
 			});
 		},
 
+		B_OnReadBLECharacteristicValue(state) {
+			uni.readBLECharacteristicValue({
+				// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
+				deviceId: state.deviceId,
+				// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
+				serviceId: '0000180F-0000-1000-8000-00805f9b34fb',
+				// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
+				characteristicId: '00002A19-0000-1000-8000-00805f9b34fb',
+				success(res) {
+					console.log('readBLECharacteristicValue:', res.errCode)
+				}
+			})
+		},
+
 		onTestEmit(state) {
 			let box = {};
 			let accArray = [];
@@ -1861,6 +1897,59 @@ const store = new Vuex.Store({
 			}
 		},
 
+		B_WriteBLECharacteristicValueFromArray(state, context) {
+			let {
+				type
+			} = context;
+			type = 'setLightEnabled';
+			let _sendData;
+			switch (type) {
+				case "setLightEnabled":
+					//设置开关 sendCommand((byte)116, 0, 10, enabled ? 1 : 0)
+					let values = [0, 10, 1];
+					let command = [255, values.length + 4, 116];
+					let end = command.concat(values);
+					console.log(JSON.stringify(end));
+					let _sendData1 = new Uint8Array(end);
+					var s = 0;
+
+					for (var i = _sendData1.length - 1; i >= 0; i--) {
+
+						s += _sendData1[i];
+
+					}
+					
+					_sendData = new Uint8Array(_sendData1.length+1);
+					for (var i =0;i<_sendData.length -1;i++) {
+					
+						 _sendData[i] = _sendData1[i];
+					
+					}
+					_sendData[_sendData.length -1] = s;
+					console.log(_sendData.length );
+					console.log(JSON.stringify(_sendData));
+					break;
+				default:
+					break;
+			}
+			let SERVICE_UUID = "AAE28F00-71B5-42A1-8C3C-F9CF6AC969D0";
+			let RX_CHAR_UUID = "AAE28F01-71B5-42A1-8C3C-F9CF6AC969D0";
+			let TX_CHAR_UUID = "AAE28F02-71B5-42A1-8C3C-F9CF6AC969D0";
+			uni.writeBLECharacteristicValue({
+				deviceId: state.deviceId,
+				serviceId: SERVICE_UUID,
+				characteristicId: TX_CHAR_UUID,
+				value: _sendData,
+				success: (res) => {
+					console.log('B_WriteBLECharacteristicValueFromArray success', res);
+
+				},
+				fail: (fail) => {
+					console.log(fail);
+				}
+			})
+		},
+
 		//向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持 write 才可以成功调用。
 		B_WriteBLECharacteristicValue(state, context) {
 			let {