| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250 |
- <template>
- <view>
- <view style="height: 50px;text-align: left;padding-left: 20px;">
- <text>
- 1.播放唤醒词:<span style="color:#F00; font-weight:bold;font-size: 15px; ">实时心率,播放,停止播报</span>。
- 2.心率区间 <span style="color:#F00; font-weight:bold;font-size: 20px; ">{{rateMin}} </span>-<span
- style="color:#F00; font-weight:bold;font-size: 20px;">{{rateMax}}</span>,超出区间每隔3秒钟语音播报。
- </text>
- </view>
- <!-- @click="onRateChange('min',-1)" -->
- <view style="display: flex;flex-direction: row;width: 260px;margin-left: 20px;">
- <button style="margin-top: 2px;" @tap="showModal" data-target="showPickerModal" data-type="min">
- 设置最小值
- </button>
- <button style="margin-top: 2px;" @tap="showModal" data-target="showPickerModal" data-type="max">
- 设置最大值
- </button>
- </view>
- <view style="align-items: center;display: flex;margin: 10px 20px;">
- <view>3.播报状态,关闭后需要手动开启播报:</view>
- <switch :checked="treatmentStatu.checked" @change="switchChange"></switch>
- </view>
- <!-- <view>
- <view class="title">Cocos预览地址:{{LocationGameUrl}}</view>
- <input class="uni-input" :value="LocationGameUrl" @input="onKeyInput" placeholder="输入cocos预览地址" />
- </view> -->
- <scroll-view scroll-y="true" style="height: 200px;background-color: #c6c6c6;">
- <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>
- </scroll-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> -->
- <!-- <button style="margin: 10rpx;" type="primary" @click="writeBLEValue('3')">开启</button>
- <button style="margin: 10rpx;" type="primary" @click="writeBLEValue('4')">关闭</button> -->
- </view>
- <!-- <button style="margin-top: 2px;" type="warn" :disabled="!currentItem" @click="onShowGame">
- 显示游戏
- </button> -->
- <!-- <button type="warn" :disabled="extraLine.length<=0" @click="remove">
- 清除输入框数据
- </button> -->
- <!-- <button @click="initTextToSpeech()">initTextToSpeech</button> -->
- <!-- <button @click="playSpeak('当前心率123')">playSpeak</button> -->
- <!-- <button @click="stopSpeak()">stopSpeak</button> -->
- <!-- <button @touchstart="recordSpeakDown()" @touchend="recordSpeakUp()">recordSpeak</button> -->
- <!-- <button @click="recordSpeakDown()">recordSpeakDown</button> -->
- <!-- <button @click="recordSpeakUp()">recordSpeakUp</button> -->
- <!-- <button @click="releaseToSpeech()">releaseToSpeech</button> -->
- <!-- <button style="margin-top: 2px;" type="warn" @click="onEmit">
- 模拟发送
- </button> -->
- <!-- <button style="margin-top: 2px;" type="warn" @click="onClise">
- close
- </button> -->
- <view style="text-align: center;">
- <!-- :class="?'active':''" :style="[{ width:loading?'60%':''}]" -->
- <view>稳定心率进度:</view>
- <view class="cu-progress round sm striped active">
- <view class="bg-green" :style="[{ width:loading}]"></view>
- </view>
- <!-- -->
- <text v-if="heartRate === 0 || !bTimerPlay" style="font-size: 80px;">--</text>
- <block v-else>
- <text style="font-size: 80px;">{{heartRate}}</text>
- <text style="font-size: 20px;">bpm</text>
- </block>
- </view>
- <view style="text-align: center;margin: 10px 0px;">
- <text style="font-size: 40px;color: blue;">{{nums}}</text>
- </view>
- <view style="display: flex;flex-direction: row;">
- <button style="margin-top: 2px;" type="warn" @click="startTimer()">
- 开始计时
- </button>
- <button style="margin-top: 2px;" type="warn" @click="pauseTime()">
- 暂停计时
- </button>
- <button style="margin-top: 2px;" type="warn" @click="closeTimer()">
- 清零计时
- </button>
- <!-- <button @click="onGetRecordAudioPermission()">test</button> -->
- </view>
- <!-- <scroll-view class="text-box" scroll-y="true" >
- <scroll-view scroll-x="true">
- <text selectable="true" style="font-size: 20px;">{{text}} \n {{nums}}</text>
- </scroll-view>
- </scroll-view> -->
- <!-- <view>
- <button @click="startLuyin" class="recordingStyle">按住开始说话</button>
- <view>识别的结果 : {{ searchText }}</view>
- </view> -->
- <!-- <view class="content">
- </view> -->
- <view class="cu-modal bottom-modal" :class="modalName == 'showPickerModal' ? 'show' : ''"
- @touchmove.stop.prevent="moveHandle">
- <view class="cu-dialog" style="border-top-right-radius: 20rpx; border-top-left-radius: 20rpx;">
- <myPicker v-if="modalName == 'showPickerModal' ? true:false" :pickerObj="pickerObj"
- @confirmEvent="onConfirm" @cancelEvent="onCancel"></myPicker>
- </view>
- </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 myPicker from '@/components/slambb-picker/slambb-picker.vue';
- import pickerData from '@/components/slambb-picker/picker.js';
- import {
- mapState,
- mapMutations
- } from 'vuex';
- // 获取 module
- var testModule = uni.requireNativePlugin("TestModule")
- export default {
- computed: mapState(['bOpenBluetooth', 'bOpenSuccess', 'bListenAdapterStateChange', 'bListenDeviceFound',
- 'BLEConnectDevice', 'BLEGetServices', 'cIndex', 'bConnection', 'bVerifiedConnection', 'BLEInfoList',
- 'BLEDeviceShowList', 'finallyUseDevice', 'systemInfo', 'guideUnlockState', 'currentModeIndex',
- "bListenerAccArray", 'LocationGameUrl', 'bGamePlaying'
- ]),
- components: {
- myPicker
- },
- 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,
- /**
- * 模块
- */
- modalName: null,
- pikerType: '',
- pickerObj: {
- pickerType: 'dateItem',
- pickerTitle: '记时间'
- },
- //测试
- text: '',
- extraLine: [],
- bShowGame: false,
- bPlayText: false,
- playTime: 0,
- /**
- * 计时器部分
- */
- timer: null,
- nums: '0时0分0秒',
- bTimerPlay: false,
- bTimerPause: false,
- hour: 0,
- minute: 0,
- second: 0,
- millisecond: 0,
- loading: '0%',
- loadingCount: 0,
- bLoading: false,
- /**
- * 录音
- */
- speechEngine: 'baidu',
- searchText: '',
- /**
- * 当前心率值
- */
- // rateText: "0",
- heartRate: 0,
- bReach: false,
- rateMax: 140,
- rateMin: 120,
- /**
- * switch
- */
- treatmentStatu: {
- checked: false
- },
- //检测录音权限
- getRecordAudioPermissionInterval: null
- }
- },
- onLoad(op) {
- this.add("初始化");
- uni.$on('retryConnectBLESuccess', this.onRetryConnectBLESuccess);
- uni.$on('callbackCloseBLE', this.hardCallbackCloseBLE);
- uni.$on('listenerBLE', this.onListenerBLE);
- this.BLEInfoList.forEach((item, index, selfarr) => {
- if (
- item.usageMode == 'CL809' ||
- item.usageMode == 'CL806' ||
- item.usageMode == 'CL831' ||
- item.usageMode == 'CL838') {
- 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);
- //清除定时器
- this.onClearTimeout();
- uni.$off("log", this.add);
- //停止语音
- this.stopSpeak();
- //释放唤醒资源
- this.releaseToSpeech();
- },
- onShow() {
- this.bShow = true;
- uni.$on('updateBLEDeviceData', this.callbackUpdateBLEData);
- },
- onReady() {
- // #ifdef APP-PLUS
- //初始化语音
- this.initTextToSpeech();
- this.$set(this.treatmentStatu, 'checked', true);
- //如果没有开启麦克风。可能导致唤醒失败
- //轮询检测麦克风权限
- if (this.getRecordAudioPermissionInterval) {
- clearInterval(this.getRecordAudioPermissionInterval);
- this.getRecordAudioPermissionInterval = null;
- }
- //开始检测
- this.getRecordAudioPermissionInterval = setInterval(() => {
- var ret = testModule.getRecordAudioPermission();
- // console.log(ret);
- if (ret.bPermission) {
- if (this.getRecordAudioPermissionInterval) {
- clearInterval(this.getRecordAudioPermissionInterval);
- this.getRecordAudioPermissionInterval = null;
- }
- this.initWakeup();
- }
- }, 1000)
- // this.initWakeup();
- // #endif
- },
- onHide() {
- //如果蓝牙弹出匹配框,会触发onHide。这时候处理蓝牙连接流程检测应等onShow 时候,再检测
- this.bShow = false;
- uni.$off('updateBLEDeviceData', this.callbackUpdateBLEData);
- },
- 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)
- function getIntValue(formatType, offset) {
- console.log("getIntValue:" + formatType + " = " + offset);
- let array = new Uint8Array(data.ab);
- 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);
- let hearRateType = (flags & 1) == 0 ? 17 : 18;
- console.log("hearRateType:" + hearRateType);
- offset = offset + 1;
- this.heartRate = getIntValue(hearRateType, offset);
- console.log("heartRate:" + this.heartRate);
- this.text = JSON.stringify(data.hex) + "\n" + "心率:" + this.heartRate + "bpm" + "\n";
- // this.rateText = this.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();
- }
- },
- 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);
- },
- /**
- * 连接设备成功
- */
- ConnectionSuccess() {
- for (let i = 0; i < this.devicesList.length; i++) {
- let eq = this.devicesList[i];
- //CL806_Bandage
- if ((eq.ename.indexOf("CL806") > -1 && this.BLEConnectDevice.id == 0) ||
- (eq.ename.indexOf("CL831") > -1 && this.BLEConnectDevice.id == 1) ||
- (eq.ename.indexOf("CL809") > -1 && this.BLEConnectDevice.id == 2) ||
- (eq.ename.indexOf("CL838") > -1 && this.BLEConnectDevice.id == 3)
- ) {
- eq.bRatio = true;
- this.currentItem = eq;
- console.log('连接成功ConnectionSuccess:', eq);
- 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); Fitcent_
- res.devices.forEach(device => {
- if (device.name.indexOf('CL809') > -1 ||
- device.name.indexOf('CL806') > -1 ||
- device.name.indexOf('CL831') > -1 ||
- device.name.indexOf('CL838') > -1
- ) {
- //如果搜索的设备名 不是对应当前设备类型,过滤
- // if (_self.currentItem.deviceName.indexOf('CL806') == -1 || _self.currentItem.deviceName.indexOf('CL831') == -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;
- }
- //创建一个连接,需要对应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)
- }
- });
- }, 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);
- },
- initTextToSpeech() {
- // 调用同步方法
- var ret = testModule.initTextToSpeech();
- console.log("initTextToSpeech:", ret);
- // uni.showToast({
- // title: "initTextToSpeech"
- // })
- },
- onGetRecordAudioPermission() {
- var ret = testModule.getRecordAudioPermission();
- console.log(ret);
- console.log(ret.bPermission)
- },
- initWakeup() {
- console.log("initWakeup!");
- // 调用同步方法
- testModule.initWakeup(null, (data) => {
- if (data.code === 200) {
- console.log("开启唤醒失败");
- uni.showToast({
- title: "初始化唤醒失败!",
- icon: 'none'
- })
- //停止语音
- this.stopSpeak();
- this.$set(this.treatmentStatu, 'checked', false);
- } else if (data.code === 0) {
- uni.showToast({
- title: "初始化唤醒成功!",
- icon: 'none'
- })
- this.$set(this.treatmentStatu, 'checked', true);
- //记录一下回调
- plus.globalEvent.addEventListener("onWakeup", this.onWakeupCallback);
- setTimeout(() => {
- this.recordSpeakDown();
- }, 1000)
- }
- });
- },
- onWakeupCallback(data) {
- // this.text = JSON.stringify(data);
- console.log("onWakeupCallback:" + JSON.stringify(data));
- if (data.word == "实时心率" || data.word == "播放") {
- uni.showToast({
- icon: 'none',
- title: data.word
- })
- this.playSpeak('当前心率' + this.heartRate);
- } else if (data.word == "停止播报") {
- this.$set(this.treatmentStatu, 'checked', false) // 手动修改switch的状态,视图会同步更新
- //停止语音
- this.stopSpeak();
- //停止唤醒
- this.releaseToSpeech();
- }
- },
- releaseToSpeech() {
- // 调用同步方法
- var ret = testModule.releaseToSpeech();
- console.log(ret);
- uni.showToast({
- title: "releaseToSpeech"
- })
- },
- playSpeak(text) {
- let currentTime = new Date().getTime() - this.playTime;
- if (currentTime > 3000) {
- // 调用同步方法
- var ret = testModule.playSpeak(text);
- console.log(ret);
- this.playTime = new Date().getTime();
- }
- },
- stopSpeak() {
- // 调用同步方法
- var ret = testModule.stopSpeak();
- console.log(ret);
- uni.showToast({
- title: "stopSpeak"
- })
- },
- startLuyin() {
- console.log('语音输入')
- let _this = this;
- let options = {};
- options.engine = _this.speechEngine
- options.punctuation = false; // 是否需要标点符号
- options.timeout = 10 * 1000; //超时时间
- plus.speech.startRecognize(options, function(s) {
- console.log(s) //识别的结果
- _this.searchText = s
- plus.speech.stopRecognize(); // 关闭
- });
- },
- recordSpeakDown() {
- // 调用同步方法
- var ret = testModule.recordSpeakDown();
- console.log("recordSpeakDown" + JSON.stringify(ret));
- // uni.showToast({
- // title: "recordSpeakDown"
- // })
- },
- recordSpeakUp() {
- // 调用同步方法
- var ret = testModule.recordSpeakUp();
- console.log("recordSpeakUp" + JSON.stringify(ret));
- // uni.showToast({
- // title: "recordSpeakUp"
- // })
- },
- /**
- * 计时器
- */
- startTimer() {
- //用心率判断是否佩戴上
- if(this.heartRate === 0){
- uni.showToast({
- title:"请佩戴好手表后开始",
- icon:'none'
- })
- return;
- }
- if (this.timer) {
- clearInterval(this.timer);
- this.timer = null;
- }
- this.timer = setInterval(() => {
- this.millisecond = this.millisecond + 50;
- if (this.millisecond >= 1000) {
- this.millisecond = 0;
- this.second = this.second + 1;
-
- //计算进度条数据
- if (this.bLoading && this.loadingCount < 60) {
- this.loadingCount++;
- this.loading = Math.round((this.loadingCount / 60) *100) + "%";
- }
- }
- if (this.second >= 60) {
- this.second = 0;
- this.minute = this.minute + 1;
- }
- if (this.minute >= 60) {
- this.minute = 0;
- this.hour = this.hour + 1;
- }
- this.nums = this.hour + '时' + this.minute + '分' + this.second + '秒';
- }, 50);
- this.bTimerPlay = true;
- },
- pauseTime() {
- if (this.timer) {
- clearInterval(this.timer);
- this.timer = null;
- }
- },
- closeTimer() {
- this.hour = this.minute = this.second = 0; //初始化
- this.millisecond = 0; //毫秒
- if (this.timer) {
- clearInterval(this.timer);
- this.timer = null;
- }
- this.nums = 0 + '时' + 0 + '分' + 0 + '秒';
- this.bTimerPlay = false;
- },
- onRateChange(type, value) {
- switch (type) {
- case "min":
- if ((value < 0 && this.rateMin > 0) || (value > 0 && this.rateMin + 1 < this.rateMax)) {
- this.rateMin += value;
- } else {
- uni.showToast({
- icon: 'none',
- title: 'rateMin 不能超过最大值或者小于0',
- })
- }
- break;
- case "max":
- if ((value < 0 && this.rateMin + 1 < this.rateMax) || value > 0) {
- this.rateMax += value;
- } else {
- uni.showToast({
- icon: 'none',
- title: 'rateMax 不能小于最小值',
- })
- }
- break;
- }
- },
- onConfirm(args) {
- this.hasUpdate = true;
- console.log('onConfirm', args);
- let tempValue = parseInt(args.value);
- if (this.pikerType == 'min') {
- if (tempValue < this.rateMax) {
- this.rateMin = parseInt(args.value);
- } else {
- uni.showToast({
- icon: 'none',
- title: '不能大于或等于最大值',
- })
- }
- } else if (this.pikerType == 'max') {
- if (tempValue > this.rateMin) {
- this.rateMax = parseInt(args.value);
- } else {
- uni.showToast({
- icon: 'none',
- title: '不能小于或等于最小值',
- })
- }
- }
- this.hideModal();
- },
- onCancel(value) {
- console.log('onCancel', value);
- this.modalName = null;
- },
- showModal(e) {
- // console.log(e.currentTarget.type);
- this.modalName = e.currentTarget.dataset.target;
- this.pikerType = e.currentTarget.dataset.type;
- switch (this.pikerType) {
- case 'min':
- this.$set(this.pickerObj, 'pickerLeftList', pickerData.getRateList().rateList);
- this.$set(this.pickerObj, 'pickerRightList', []);
- this.$set(this.pickerObj, 'pickerUnit', '');
- this.$set(this.pickerObj, 'pickerType', 'singleItem');
- this.$set(this.pickerObj, 'pickerTitle', '最小心率');
- this.$set(this.pickerObj, 'defaultValue', this.rateMin);
- this.$set(this.pickerObj, 'showInput', true);
- break;
- case 'max':
- this.$set(this.pickerObj, 'pickerLeftList', pickerData.getRateList().rateList);
- this.$set(this.pickerObj, 'pickerRightList', []);
- this.$set(this.pickerObj, 'pickerUnit', '');
- this.$set(this.pickerObj, 'pickerType', 'singleItem');
- this.$set(this.pickerObj, 'pickerTitle', '最大心率');
- this.$set(this.pickerObj, 'defaultValue', this.rateMax);
- this.$set(this.pickerObj, 'showInput', true);
- break;
- }
- },
- hideModal(e) {
- this.modalName = null;
- this.pikerType = '';
- },
- moveHandle() {
- return;
- },
- switchChange(e) {
- let value = e.target.value
- let that = this
- this.$set(this.treatmentStatu, 'checked', value) // 将点击改变的状态赋给treatmentStatu.checked
- if (value) {
- uni.showModal({
- title: '提示',
- content: '是否开启语音播报?',
- success: function(res) {
- if (res.confirm) {
- //todo 打开语音播报
- console.log('用户点击确定')
- that.initTextToSpeech();
- that.initWakeup();
- } else if (res.cancel) {
- that.$set(that.treatmentStatu, 'checked', false) // 手动修改switch的状态,视图会同步更新
- }
- }
- });
- } else {
- uni.showModal({
- title: '提示',
- content: '是否关闭语音播报?',
- success: function(res) {
- if (res.confirm) {
- //todo 关闭语音播报
- console.log('用户点击确定')
- //停止语音
- that.stopSpeak();
- that.releaseToSpeech();
- } else if (res.cancel) {
- that.$set(that.treatmentStatu, 'checked', true) // 手动修改switch的状态,视图会同步更新
- }
- }
- });
- }
- }
- }
- }
- </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;
- }
- .recordingStyle {
- border-radius: 20px;
- text-align: center;
- color: #fff;
- font-size: 15px;
- background-color: #409eff;
- margin-bottom: 15px;
- }
- </style>
|