devices-hardware.vue 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. <template>
  2. <view>
  3. <uni-nav-bar id="nav-bar" status-bar="true" @clickLeft="onBack()" @clickRight="onNavUpdateDevice()"
  4. title="选择设备类型" color="#000000" fixed="true" :border="false">
  5. <view slot="left">
  6. <view class=" flex align-center margin-left">
  7. <image class="p-left-arrow" src="../../../static/p-left-arrow.png"></image>
  8. </view>
  9. </view>
  10. <view slot="right">
  11. <view class=" flex align-center " style="margin-right: 10rpx;">
  12. <image style="width: 60rpx;height: 50rpx;" src="../../../static/devicesIconSimple/upgrade.png">
  13. </image>
  14. </view>
  15. </view>
  16. </uni-nav-bar>
  17. <view class="card-view padding-top padding-bottom" v-for="(item,index) in devicesList" :key="index"
  18. :id="'task_'+index" :class="item.bRatio?'hardware-border':''" :style="{'z-index': threeZIndex}">
  19. <view class="flex justify-between align-center">
  20. <view class="flex justify-start align-center">
  21. <image style="margin-left: 20rpx; width: 200rpx;height: 120rpx;" :src="item.icon" mode="aspectFit">
  22. </image>
  23. <view style="width: 350rpx;margin-left: 10rpx;">
  24. <view style="margin: 20rpx 0rpx; font-weight: bold; font-size: 18px; color: #565656;">
  25. {{item.cname}}
  26. </view>
  27. <view style="font-size: 12px; white-space:pre-wrap;" class="make-text-bPurple">
  28. {{item.describe}}{{item.bOldDevice?'(旧手柄)':''}}
  29. </view>
  30. </view>
  31. </view>
  32. <image style="margin-right: 60rpx; width: 60rpx;height: 60rpx;"
  33. :src="item.bRatio?'/static/devicesOther/radio-b.png':'/static/devicesOther/radio-g.png'"
  34. mode="aspectFit" @tap="_onRadio(item,$event)"></image>
  35. </view>
  36. </view>
  37. <!-- <view style="display: flex;justify-content: space-between;" class="margin-top margin-bottom">
  38. <button @click="switchList(1)">jumpList</button>
  39. <button @click="switchList(2)">hitList</button>
  40. </view> -->
  41. <view v-if="currentIndex<5" class="position-absolute-center" style="z-index: 1; width: 750rpx;display: flex;">
  42. <swiper class="swiper" :style="{height:currentHeight+'px'}" :circular="circular" :vertical="true"
  43. disable-touch="true" :current="currentIndex" @click="onSwiperClick" @change="onSwiperChange">
  44. <!-- 1 @touchmove.stop='stopTouchMove' -->
  45. <swiper-item class="flex justify-center align-center" @click="onSwiperItemClick">
  46. <view style="width: 590rpx; height: 626rpx; margin-top: 50rpx;" class="position-relative">
  47. <view class="bluetooth-guide-number" style="position: absolute;left: 94rpx;top: 8rpx;">1</view>
  48. <!-- <image style="width: 590rpx; height: 626rpx;"
  49. src="../../../static/guide/blutooth/guide-ble-1@2x.png">
  50. </image> -->
  51. <view style="width: 590rpx; height: 512rpx;" class="flex position-relative">
  52. <image style="width: 424rpx;height: 412rpx; margin-top: 100rpx;"
  53. src="../../../static/guide/blutooth/guide-ble-tip.png"></image>
  54. <image style="width: 102rpx;height: 102rpx; margin-top: 116rpx; margin-left: 56rpx;"
  55. src="../../../static/guide/blutooth/guide-ble-toggle.png"></image>
  56. <view class="position-absolute-right-top">
  57. <image style="width: 406rpx;height: 94rpx; "
  58. src="../../../static/guide/blutooth/guide-toggle-tip.png"></image>
  59. <view class="position-absolute-center text-regular"
  60. style="width: 438rpx;height: 96rpx;top: 26rpx;left: 40rpx;">
  61. 确保手机蓝牙处于开启状态</view>
  62. </view>
  63. </view>
  64. <view class="position-relative" style="top: 10rpx;">
  65. <image style="width: 520rpx; height:166rpx;top: 10rpx;left: 26rpx;"
  66. src="../../../static/guide/blutooth/boxMid@2x.png">
  67. </image>
  68. <view class="position-absolute-center text-regular"
  69. style="width: 438rpx;height: 96rpx;top: 18rpx;">
  70. 切记请不要直接连接蓝牙,确保手柄没被任何手机连接,如果有请断开连接。</view>
  71. </view>
  72. </view>
  73. </swiper-item>
  74. <!-- 2 -->
  75. <swiper-item class="flex justify-center align-center flex-direction">
  76. <image style="width: 354rpx; height: 306rpx; margin-bottom: 28rpx;"
  77. src="../../../static/guide/blutooth/guide-ble-device.png">
  78. </image>
  79. <view class="position-relative">
  80. <image style="width: 520rpx; height:166rpx;" src="../../../static/guide/blutooth/boxMid@2x.png">
  81. </image>
  82. <view class="position-absolute-center text-regular"
  83. style="width: 438rpx;height: 96rpx;top: 26rpx;">长按游戏设备开关3秒,设备灯连续闪烁为可连接状态</view>
  84. </view>
  85. <view class="bluetooth-guide-number" style="margin-top: 36rpx;">2</view>
  86. </swiper-item>
  87. <!-- 3 -->
  88. <swiper-item class="flex justify-start align-center flex-direction">
  89. <view :style="{height:threeTipHeight+'px'}"></view>
  90. <view class="flex justify-end">
  91. <view class="bluetooth-guide-number" style="margin-top: 32rpx;">3</view>
  92. <view class="position-relative">
  93. <image style="width: 462rpx; height:206rpx;"
  94. src="../../../static/guide/blutooth/boxRight@2x.png">
  95. </image>
  96. <view class="position-absolute-center text-regular"
  97. style="width: 392rpx;height: 144rpx;top: 44rpx;">选择设备类型连接,按钮勾选上表示手柄连接成功。如果连接失败请重试</view>
  98. </view>
  99. </view>
  100. </swiper-item>
  101. <!-- 4 -->
  102. <swiper-item class="flex justify-center align-center flex-direction">
  103. <image style="width: 446rpx; height: 384rpx; margin-bottom: 28rpx; margin-top: 100rpx;"
  104. src="../../../static/guide/blutooth/guide-ble-bind.png">
  105. </image>
  106. <view class="position-relative">
  107. <image style="width: 520rpx; height:166rpx;" src="../../../static/guide/blutooth/boxMid@2x.png">
  108. </image>
  109. <view class="position-absolute-center text-regular"
  110. style="width: 438rpx;height: 96rpx;top: 26rpx;">请将手柄十字键朝上插入绑带中,用绑带将手柄朝上绑在右脚脚踝上</view>
  111. </view>
  112. <view class="bluetooth-guide-number" style="margin-top: 36rpx;">4</view>
  113. </swiper-item>
  114. <!-- 5 -->
  115. <swiper-item class="flex justify-start align-center flex-direction">
  116. <view :style="{height:threeTipHeight+'px'}"></view>
  117. <view class="position-relative">
  118. <image style="width: 520rpx; height:162rpx;" src="../../../static/guide/blutooth/boxMid@2x.png">
  119. </image>
  120. <view class="position-absolute-center flex align-center justify-center"
  121. style="width: 438rpx;top: 32rpx;">
  122. <image style="width: 60rpx;height:60rpx;margin-right: 28rpx;"
  123. src="../../../static/guide/blutooth/guide-ble-finish@2x.png"></image>
  124. <view class=" text-regular " style="width: 336rpx;height: 90rpx;">
  125. 连接手柄成功后,返回主界面选择关卡开始游戏吧</view>
  126. </view>
  127. </view>
  128. </swiper-item>
  129. </swiper>
  130. </view>
  131. </view>
  132. </template>
  133. <script>
  134. import config from '@/common/config.js';
  135. import reqUtil from '@/util/util-js/requstUtil.js';
  136. import ble from '@/util/util-js/BLE.js';
  137. import {
  138. mapState,
  139. mapMutations
  140. } from 'vuex';
  141. export default {
  142. computed: mapState(['bOpenBluetooth', 'bOpenSuccess', 'bListenAdapterStateChange', 'bListenDeviceFound',
  143. 'BLEConnectDevice', 'BLEGetServices', 'cIndex', 'bConnection', 'bVerifiedConnection', 'BLEInfoList',
  144. 'BLEDeviceShowList', 'finallyUseDevice', 'systemInfo', 'guideUnlockState','currentModeIndex'
  145. ]),
  146. data() {
  147. return {
  148. // 设备列表
  149. devicesList: [],
  150. currentItem: null,
  151. //设置一个旧的连接item
  152. oldItem: null,
  153. searchObj: null,
  154. //设置搜索超时时间
  155. searchTimeOut: null,
  156. option: null,
  157. saveObj: null,
  158. //搜索提示定时器
  159. searchMac: null,
  160. //
  161. getServicesTimeout: null,
  162. writeMacTimeout: null,
  163. //限制关闭连接
  164. bLimitClose: false,
  165. //是否需要检测发起的连接蓝牙是否匹配上
  166. bTestBondConnect: true,
  167. //是否显示
  168. bShow: true,
  169. //切换连接
  170. bSwitch: false,
  171. //提示
  172. circular: false,
  173. currentHeight: 0,
  174. threeTipHeight: 0,
  175. threeZIndex: 0,
  176. currentIndex: 5,
  177. }
  178. },
  179. onLoad(op) {
  180. // if (op.deviceType) {
  181. // console.log(op);
  182. // this.option = op;
  183. // }
  184. //目前是固定这个值,去掉了一级页面
  185. // this.option = {
  186. // cname: "手柄盒子",
  187. // deviceType: 'BLEHandle'
  188. // };
  189. // if (this.option.deviceType == 'BLEHandle') {
  190. // this.BLEInfoList.forEach((item, index, selfarr) => {
  191. // if (item.deviceType == 'BLEHandle' || item.deviceType == 'BLERope') {
  192. // let item = Object.assign({}, selfarr[index]);
  193. // this.devicesList.push(item);
  194. // }
  195. // })
  196. // }
  197. this.BLEInfoList.forEach((item, index, selfarr) => {
  198. if ((item.deviceType == 'BLEHandle' && item.usageMode == 'hotman') || item.deviceType ==
  199. 'BLERope') {
  200. let item = Object.assign({}, selfarr[index], {
  201. bOldDevice: false
  202. });
  203. this.devicesList.push(item);
  204. }
  205. })
  206. uni.$on('retryConnectBLESuccess', this.onRetryConnectBLESuccess);
  207. uni.$on('callbackCloseBLE', this.hardCallbackCloseBLE);
  208. uni.$on('listenerBLE', this.onListenerBLE);
  209. this.currentHeight = this.systemInfo.windowHeight;
  210. if (this.guideUnlockState.firstDisconnectBluetooth) {
  211. this.currentIndex = 0;
  212. let guideUnlockState = Object.assign(this.guideUnlockState, {
  213. firstDisconnectBluetooth: false
  214. });
  215. this.setGuideUnlockState(guideUnlockState);
  216. }
  217. },
  218. onUnload() {
  219. uni.$off('retryConnectBLESuccess', this.onRetryConnectBLESuccess);
  220. uni.$off('callbackCloseBLE', this.hardCallbackCloseBLE);
  221. uni.$off('listenerBLE', this.onListenerBLE);
  222. //清除定时器
  223. this.onClearTimeout();
  224. },
  225. onShow() {
  226. this.bShow = true;
  227. this.bLimitClose = false;
  228. // console.log(this.cIndex, this.BLEDeviceShowList, this.BLEConnectDevice, this.devicesList);
  229. //this.bConnection &&
  230. if (this.bVerifiedConnection && this.BLEConnectDevice) {
  231. for (let i = 0; i < this.devicesList.length; i++) {
  232. let eq = this.devicesList[i];
  233. if (
  234. (eq.ename.indexOf("mobilePhoneBandage") > -1 && this.BLEConnectDevice.id == 0) ||
  235. (eq.ename.indexOf("hotman") > -1 && this.BLEConnectDevice.id == 1) ||
  236. (eq.ename.indexOf("BLEHandle") > -1 && this.BLEConnectDevice.id == 2) ||
  237. (eq.ename.indexOf("rope") > -1 && this.BLEConnectDevice.id == 3)) {
  238. eq.bRatio = true;
  239. this.currentItem = eq;
  240. // setTimeout(() => {
  241. // this.onCheckBondDevice();
  242. // }, 5000)
  243. }
  244. }
  245. } else {
  246. //如果没连接的话,设置一下显示状态
  247. for (let i = 0; i < this.devicesList.length; i++) {
  248. let eq = this.devicesList[i];
  249. eq.bRatio = false;
  250. }
  251. // this.currentItem = null;
  252. }
  253. },
  254. onReady() {
  255. let view = uni.createSelectorQuery().select('#task_0');
  256. view.boundingClientRect(data => {
  257. // console.log('参数:' + JSON.stringify(data));
  258. //13 是间隔
  259. this.threeTipHeight = data.top + data.height + 13;
  260. }).exec();
  261. },
  262. onHide() {
  263. //如果蓝牙弹出匹配框,会触发onHide。这时候处理蓝牙连接流程检测应等onShow 时候,再检测
  264. this.bShow = false;
  265. },
  266. methods: {
  267. ...mapMutations(['initAdapter', 'onCreateBLESuccess', 'onGetBLEDeviceServices', 'onOnlyCloseBLEConnection',
  268. 'onGetRSSITransDistance',
  269. 'addBLEDevice', 'onWriteBLEConnectionValue', 'deleteBLEDevice', 'B_GetBondedDevices',
  270. 'B_OpenBLESetting', 'setGuideUnlockState', 'switchLevelList'
  271. ]),
  272. //测试切换关卡
  273. switchList(event) {
  274. this.switchLevelList(event);
  275. //刷新首页事件
  276. uni.$emit("onSwitchTaskList");
  277. },
  278. //
  279. onClearTimeout() {
  280. // 退出后,清除计时器
  281. if (this.searchMac) {
  282. clearTimeout(this.searchMac);
  283. this.searchMac = null;
  284. }
  285. //servicesTimeout
  286. if (this.getServicesTimeout) {
  287. clearTimeout(this.getServicesTimeout);
  288. this.getServicesTimeout = null;
  289. }
  290. //写入指令writeMacTimeout
  291. if (this.writeMacTimeout) {
  292. clearTimeout(this.writeMacTimeout);
  293. this.writeMacTimeout = null;
  294. }
  295. if (this.searchTimeOut) {
  296. clearTimeout(this.searchTimeOut);
  297. this.searchTimeOut = null;
  298. }
  299. },
  300. //监听回调
  301. onListenerBLE(res) {
  302. console.log('onListenerBLE:', res, this.saveObj);
  303. if (res.type !== 'mac') return;
  304. //如果mac 回调了不用提示
  305. if (this.searchMac) {
  306. clearTimeout(this.searchMac);
  307. this.searchMac = null;
  308. }
  309. let _self = this;
  310. let mac = res.value;
  311. // console.log('mac =====', mac);
  312. //用返回的mac 判断,如果非法,则断开连接
  313. //就判断一下是否
  314. //测试
  315. // mac="BB:34:24:22:77:88";
  316. // mac="较è¾";
  317. //判断mac地址是否是合理的格式
  318. var tempMacRegExp =
  319. /[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}/;
  320. if (!tempMacRegExp.test(mac)) {
  321. uni.hideToast();
  322. uni.showModal({
  323. title: '验证信息',
  324. content: '获取Mac地址失败,请重新连接。'
  325. })
  326. //关闭当前连接
  327. _self.onOnlyCloseBLEConnection({
  328. getSuccess: () => {
  329. _self.currentItem = null;
  330. _self.saveObj = null;
  331. }
  332. });
  333. return;
  334. }
  335. reqUtil.requestData(config.URL.BLEFINDHASBIND, {
  336. mac: mac,
  337. }).then(
  338. res => {
  339. console.log('BLEFINDHASBIND:' + mac + " = " + JSON.stringify(res));
  340. if (res.code == 0) {
  341. //code = 0 是新的地址,没绑定过
  342. //绑定设备使用者
  343. reqUtil.requestData(config.URL.BLEBIND, {
  344. mac: mac,
  345. }).then(
  346. res => {
  347. // console.log('BLEBIND:', res);
  348. if (res.code == 0) {
  349. //绑定成功,添加设备
  350. if (_self.saveObj == null) return;
  351. //添加一个mac地址到saveObj
  352. console.log('添加了mac前saveObj', _self.saveObj);
  353. _self.saveObj = Object.assign({}, _self.saveObj, {
  354. deviceMac: mac,
  355. bOldDevice: res.data != null ? res.data.bOldDevice : false
  356. });
  357. console.log('添加了mac后saveObj:', _self.saveObj);
  358. _self.addBLEDevice(_self.saveObj);
  359. _self.ConnectionSuccess();
  360. }
  361. },
  362. e => {
  363. console.log(e);
  364. //绑定失败, 关闭当前连接
  365. _self.onOnlyCloseBLEConnection({
  366. getSuccess: () => {
  367. _self.currentItem.bRatio = false;
  368. _self.currentItem = null;
  369. _self.saveObj = null;
  370. }
  371. });
  372. }
  373. );
  374. } else if (res.code == 721) {
  375. // console.log("~~:",_self.saveObj);
  376. //是自己绑定的,添加设备
  377. if (_self.saveObj == null) return;
  378. //添加一个mac地址到saveObj
  379. console.log('code=721,添加了mac前saveObj', _self.saveObj);
  380. _self.saveObj = Object.assign({}, _self.saveObj, {
  381. deviceMac: mac,
  382. bOldDevice: res.data != null ? res.data.bOldDevice : false
  383. });
  384. console.log('code=721,添加了mac后saveObj:', _self.saveObj);
  385. _self.addBLEDevice(_self.saveObj);
  386. _self.ConnectionSuccess();
  387. } else if (res.code == 711 || res.code == 722) {
  388. //res.code = 711 蓝牙设备不是我们自己的
  389. //res.code = 722 蓝牙设备别人使用过
  390. uni.hideToast();
  391. uni.showModal({
  392. title: '连接失败',
  393. content: '当前手柄已经被其它账号绑定。'
  394. })
  395. //关闭当前连接
  396. _self.onOnlyCloseBLEConnection({
  397. getSuccess: () => {
  398. _self.currentItem = null;
  399. _self.saveObj = null;
  400. }
  401. });
  402. }
  403. },
  404. e => {
  405. console.log(e);
  406. }
  407. );
  408. },
  409. /**
  410. * 连接设备成功
  411. */
  412. ConnectionSuccess() {
  413. for (let i = 0; i < this.devicesList.length; i++) {
  414. let eq = this.devicesList[i];
  415. if (
  416. (eq.ename.indexOf("mobilePhoneBandage") > -1 && this.BLEConnectDevice.id == 0) ||
  417. (eq.ename.indexOf("hotman") > -1 && this.BLEConnectDevice.id == 1) ||
  418. (eq.ename.indexOf("BLEHandle") > -1 && this.BLEConnectDevice.id == 2) ||
  419. (eq.ename.indexOf("rope") > -1 && this.BLEConnectDevice.id == 3)) {
  420. if (eq.ename.indexOf("hotman") > -1) {
  421. //拳击
  422. this.switchList(2);
  423. } else if (eq.ename.indexOf("rope") > -1) {
  424. //跳绳
  425. this.switchList(1);
  426. }
  427. eq.bRatio = true;
  428. this.currentItem = eq;
  429. /**
  430. * 已验证的连接设置true
  431. */
  432. this.$store.state.bVerifiedConnection = true;
  433. /**
  434. * 根据名字兼容旧设备
  435. */
  436. let _tempMac = this.saveObj.deviceMac.replace(/:/g, "");
  437. let _fourStr = _tempMac.substring(_tempMac.length - 4, _tempMac.length);
  438. console.log("格式化去掉冒号后mac:", _tempMac, "==", _fourStr);
  439. let _tempInt = ble.hex2int(_fourStr);
  440. console.log("格式化去掉冒号后_tempInt:", _tempInt);
  441. //这里判断旧设备,因为目前这批新设备,用的也是202012的名字,所以这里处理兼容旧设备就行了
  442. //用mac判断区分,一起有100个是旧设备,还有部分非标准的mac,根据后台字段判断
  443. //如果后台存在bOldDevice 字段,则根据字段判断是否是旧设备
  444. if (this.saveObj.name === "BGBox_202012" &&
  445. _tempInt <= 101 && _tempInt >= 0 || this.saveObj.bOldDevice) {
  446. this.$store.state.bOldDeviceType = true;
  447. eq.bOldDevice = true;
  448. console.log("旧设备:", this.$store.state.bOldDeviceType);
  449. } else {
  450. console.log("旧设备", this.$store.state.bOldDeviceType);
  451. eq.bOldDevice = false;
  452. }
  453. uni.hideToast();
  454. }
  455. }
  456. },
  457. onCheckBondDevice() {
  458. console.log("onCheckBondDevice", this.bTestBondConnect);
  459. if (this.bTestBondConnect && this.bShow) {
  460. /**
  461. * 假如手机没有匹配,断开连接
  462. */
  463. console.log(2, this.finallyUseDevice);
  464. this.B_GetBondedDevices({
  465. deviceId: this.finallyUseDevice.deviceId,
  466. success: (bondedDevice) => {
  467. uni.hideToast();
  468. if (bondedDevice == null) {
  469. // if (plus.os.name == 'Android')
  470. {
  471. //此问题 华为手机容易出现
  472. //android手机已配对的设备 不存在,但是app 又直接连接成功了。提示,并且断开app连接
  473. //1.关闭当前连接
  474. this.onOnlyCloseBLEConnection({
  475. getSuccess: () => {
  476. this.currentItem.bRatio = false;
  477. this.currentItem = null;
  478. this.saveObj = null;
  479. }
  480. });
  481. //2.跳转蓝牙设置
  482. uni.showModal({
  483. title: '蓝牙配对失败',
  484. content: '请跳转后点击配对BGBox_2020,成功后手动跳转回哔蹦重新连接。',
  485. success: (res) => {
  486. if (res.confirm) {
  487. this.B_OpenBLESetting();
  488. }
  489. }
  490. })
  491. }
  492. }
  493. }
  494. });
  495. this.bTestBondConnect = false;
  496. } else {
  497. uni.hideToast();
  498. }
  499. },
  500. onRetryConnectBLESuccess() {
  501. this.ConnectionSuccess();
  502. },
  503. hardCallbackCloseBLE() {
  504. if (this.BLEConnectDevice == null && this.currentItem && !this.bSwitch) {
  505. this.currentItem.bRatio = false;
  506. this.currentItem = null;
  507. this.saveObj = null;
  508. }
  509. //如果限制不走重连
  510. if (this.bLimitClose) return;
  511. console.log(this.currentItem, this.oldItem);
  512. if (this.oldItem && this.currentItem && this.currentItem.id == this.oldItem.id) {
  513. // this.$store.state.bConnection = false;
  514. if (this.currentItem.bRatio) {
  515. uni.showToast({
  516. title: '设备断开连接!',
  517. icon: 'none',
  518. duration: 2000,
  519. mask: true
  520. })
  521. this.currentItem.bRatio = false;
  522. this.oldItem = null;
  523. this.currentItem = null;
  524. //断开连接
  525. this.$store.state.bVerifiedConnection = false;
  526. }
  527. } else if (this.bConnection && this.BLEConnectDevice) {
  528. //假如匹配过程中断开连接
  529. this.$store.state.BLEConnectDevice = null;
  530. this.$store.state.bConnection = false;
  531. // uni.hideToast();
  532. // console.log("//假如匹配过程中断开连接");
  533. uni.showToast({
  534. title: '连接失败,尝试重新连接。',
  535. icon: 'none',
  536. mask: true,
  537. duration: 2000
  538. })
  539. if (this.getServicesTimeout) {
  540. clearTimeout(this.getServicesTimeout);
  541. this.getServicesTimeout = null;
  542. }
  543. }
  544. },
  545. /**
  546. * 开始查找设备
  547. * */
  548. startBluetoothDeviceDiscovery() {
  549. //在页面显示的时候判断是都已经初始化完成蓝牙适配器若成功,则开始查找设备
  550. let _self = this;
  551. if (_self.bOpenBluetooth) {
  552. //1.第一步还是先进行设备搜索
  553. _self.onCanStart();
  554. } else {
  555. _self.initAdapter(() => {
  556. _self.startBluetoothDeviceDiscovery();
  557. });
  558. }
  559. },
  560. /**
  561. * 通过检测手机连接的设备进行连接,null 的话进行搜索操作
  562. */
  563. onBondedDeviceConnect(data) {
  564. let _self = this;
  565. //获取手机本身已连接的硬件,
  566. //示例
  567. // {
  568. // "deviceId": "C5:5C:19:04:00:30",
  569. // "name": "BGBox_202012",
  570. // "RSSI": -74,
  571. // "localName": "BGBox_20201",
  572. // "advertisServiceUUIDs": ["00001812-0000-1000-8000-00805F9B34FB", "0000FFF0-0000-1000-8000-00805F9B34FB"]
  573. // }
  574. console.log("onBoded ***************:", _self.finallyUseDevice);
  575. _self.B_GetBondedDevices({
  576. deviceId: _self.finallyUseDevice == null ? null : _self.finallyUseDevice.deviceId,
  577. success: (bondedDevice) => {
  578. console.log("bondedDevice:", bondedDevice);
  579. //获取已和蓝牙连接的设备
  580. if (bondedDevice != null) {
  581. //如果用户匹配了对应的设备,直接用对应的设备来连接
  582. let setDevice = {
  583. "deviceId": bondedDevice.address,
  584. "name": bondedDevice.name,
  585. "RSSI": -74,
  586. "localName": "",
  587. "advertisServiceUUIDs": ["00001812-0000-1000-8000-00805F9B34FB",
  588. "0000FFF0-0000-1000-8000-00805F9B34FB"
  589. ]
  590. }
  591. let obj = Object.assign({}, setDevice, _self.currentItem);
  592. //finallyUserDevice 就是最后一次使用搜索到的设备信息
  593. _self.saveObj = Object.assign({}, setDevice, {
  594. id: _self.currentItem.id
  595. });
  596. uni.showToast({
  597. title: '设备连接中...',
  598. icon: 'loading',
  599. duration: 10000,
  600. mask: true
  601. })
  602. console.log("GetBondedDevices:::===", setDevice, _self.currentItem, obj);
  603. // 先直连,然后判断版本
  604. _self._onConnectDevice(obj);
  605. //getBond后发起的连接,不需要检测了
  606. _self.bTestBondConnect = false;
  607. } else {
  608. console.log("没有获取到绑定的设备");
  609. uni.showToast({
  610. title: '获取匹配设备失败',
  611. icon: 'none',
  612. mask: true
  613. })
  614. }
  615. }
  616. });
  617. },
  618. //开始搜索
  619. onCanStart() {
  620. let _self = this;
  621. uni.showToast({
  622. title: '设备连接中...',
  623. icon: 'loading',
  624. duration: 15000,
  625. mask: true
  626. })
  627. // 1.已经搜索到的,根据蓝牙回调的mac 地址判断合法性。
  628. uni.startBluetoothDevicesDiscovery({
  629. allowDuplicatesKey: true,
  630. success: res => {
  631. // console.log("startBluetoothDevicesDiscovery:", res);
  632. _self.bSwitch = false;
  633. _self.onBluetoothDeviceFound();
  634. },
  635. fail: res => {
  636. console.log("搜索失败!");
  637. _self.initAdapter(() => {
  638. _self.startBluetoothDeviceDiscovery();
  639. });
  640. }
  641. });
  642. // 2.没有搜索到的,一段时间后处理。比如手机已经连接了设备,但是app 里面搜索不到,也没记录使用过的。
  643. if (_self.searchTimeOut) {
  644. clearTimeout(_self.searchTimeOut);
  645. _self.searchTimeOut = null;
  646. }
  647. //搜索一段时间后,停止搜索
  648. if (plus.os.name == 'iOS') {
  649. _self.searchTimeOut = setTimeout(() => {
  650. _self.stopBluetoothDevicesDiscovery();
  651. //搜索失败后,再检查是否和手机配对的设备连接
  652. _self.onBondedDeviceConnect();
  653. }, 4000)
  654. } else {
  655. _self.searchTimeOut = setTimeout(() => {
  656. _self.stopBluetoothDevicesDiscovery();
  657. }, 8000)
  658. }
  659. },
  660. /**
  661. * 停止搜索蓝牙设备
  662. */
  663. stopBluetoothDevicesDiscovery() {
  664. uni.stopBluetoothDevicesDiscovery({
  665. success: e => {
  666. // console.log('停止搜索蓝牙设备:' + e.errMsg);
  667. },
  668. fail: e => {
  669. console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
  670. }
  671. });
  672. },
  673. /**
  674. * 发现外围设备
  675. */
  676. onBluetoothDeviceFound() {
  677. let _self = this;
  678. _self.searchObj = null;
  679. uni.onBluetoothDeviceFound(res => {
  680. /**
  681. * 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
  682. */
  683. // console.log("onBluetoothDeviceFound:", res);
  684. res.devices.forEach(device => {
  685. if (device.name.indexOf('PBox') > -1 || device.name.indexOf('BGBox') > -1 || device
  686. .name.indexOf('Rope') > -1) {
  687. //如果搜索的设备名 不是对应当前设备类型,过滤
  688. // if (device.name.indexOf(_self.currentItem.deviceName) == -1) return;
  689. if (_self.currentItem.deviceName.indexOf('PBox') == -1 &&
  690. _self.currentItem.deviceName.indexOf('BGBox') == -1 &&
  691. _self.currentItem.deviceName.indexOf('Rope') == -1
  692. ) return;
  693. //寻找到对应设备时候,其余的返回
  694. if (_self.searchObj) return;
  695. _self.searchObj = device;
  696. if (_self.searchTimeOut) {
  697. clearTimeout(_self.searchTimeOut);
  698. _self.searchTimeOut = null;
  699. }
  700. //currentItem 是mode 页面选中的item
  701. let obj = Object.assign({}, device, _self.currentItem);
  702. _self.saveObj = Object.assign({}, {
  703. id: _self.currentItem.id
  704. }, device);
  705. // console.log(device, "****", obj, _self.saveObj, _self.currentItem)
  706. // 先直连,然后判断版本
  707. _self._onConnectDevice(obj);
  708. _self.stopBluetoothDevicesDiscovery();
  709. }
  710. })
  711. })
  712. },
  713. onBack() {
  714. uni.navigateBack({
  715. delta: 1
  716. })
  717. },
  718. // 提示点击连接设备
  719. _onConnectDevice(item) {
  720. //servicesTimeout
  721. if (this.getServicesTimeout) {
  722. clearTimeout(this.getServicesTimeout);
  723. this.getServicesTimeout = null;
  724. }
  725. //写入指令writeMacTimeout
  726. if (this.writeMacTimeout) {
  727. clearTimeout(this.writeMacTimeout);
  728. this.writeMacTimeout = null;
  729. }
  730. //如果已经连接,加上服务不存在,直接获取服务
  731. if (this.bConnection && this.BLEGetServices && this.BLEGetServices.length == 0) {
  732. console.log("***直接获取服务*******");
  733. this.onGetBLEDeviceServices({
  734. item: item,
  735. success: (res) => {
  736. console.log("*****getBLEDeviceServices************");
  737. //连接成功了,设置旧的item
  738. this.oldItem = this.currentItem;
  739. uni.showToast({
  740. title: '正在验证设备信息...',
  741. icon: 'loading',
  742. duration: 10000,
  743. mask: true
  744. })
  745. // 初始化服务后,获取版本,判断
  746. // 停止蓝牙加速计
  747. this.writeMacTimeout = setTimeout(() => {
  748. //todo 停止蓝牙加速计
  749. // this.onWriteBLEConnectionValue({
  750. // value: "4"
  751. // });
  752. // 发送获取mac
  753. this.onWriteBLEConnectionValue({
  754. value: "M"
  755. });
  756. if (this.searchMac) {
  757. clearTimeout(this.searchMac);
  758. this.searchMac = null;
  759. }
  760. this.searchMac = setTimeout(() => {
  761. uni.hideToast();
  762. uni.showModal({
  763. title: '提示',
  764. content: '检测设备失败。\r\n1.再次尝试连接。\r\n2.或者重启手柄(Reset键位也可)后再尝试连接'
  765. })
  766. this.onOnlyCloseBLEConnection({
  767. getSuccess: () => {
  768. if (this.currentItem)
  769. this.currentItem.bRatio = false;
  770. this.currentItem = null;
  771. }
  772. });
  773. }, 6000)
  774. }, 3000)
  775. }
  776. });
  777. return;
  778. }
  779. //创建一个连接,需要对应close
  780. this.onCreateBLESuccess({
  781. item: item,
  782. getSuccess: () => {
  783. // console.log("****创建一个连接*******");
  784. this.getServicesTimeout = setTimeout(() => {
  785. this.onGetBLEDeviceServices({
  786. item: item,
  787. success: (res) => {
  788. // console.log("******getBLEDeviceServices************", res);
  789. //连接成功了,设置旧的item
  790. this.oldItem = this.currentItem;
  791. uni.showToast({
  792. title: '正在验证设备信息...',
  793. icon: 'loading',
  794. duration: 10000,
  795. mask: true
  796. })
  797. // 初始化服务后,获取版本,判断
  798. // 停止蓝牙加速计
  799. this.writeMacTimeout = setTimeout(() => {
  800. //todo 停止蓝牙加速计
  801. // this.onWriteBLEConnectionValue({
  802. // value: "4"
  803. // });
  804. // 发送获取mac
  805. this.onWriteBLEConnectionValue({
  806. value: "M"
  807. });
  808. if (this.searchMac) {
  809. clearTimeout(this.searchMac);
  810. this.searchMac = null;
  811. }
  812. this.searchMac = setTimeout(() => {
  813. uni.hideToast();
  814. uni.showModal({
  815. title: '提示',
  816. content: '检测设备失败。\r\n1.再次尝试连接。\r\n2.或者重启手柄(Reset键位也可)后再尝试连接'
  817. })
  818. this.onOnlyCloseBLEConnection({
  819. getSuccess: () => {
  820. if (this
  821. .currentItem
  822. )
  823. this
  824. .currentItem
  825. .bRatio =
  826. false;
  827. this.currentItem =
  828. null;
  829. }
  830. });
  831. }, 6000)
  832. }, 3000)
  833. }
  834. });
  835. }, 2500);
  836. }
  837. })
  838. },
  839. _onRadio(item, event) {
  840. if (!item.bRatio) {
  841. //设置默认值
  842. this.bTestBondConnect = true;
  843. if (this.BLEConnectDevice) {
  844. this.onOnlyCloseBLEConnection({
  845. getSuccess: () => {
  846. if (this.currentItem)
  847. this.currentItem.bRatio = false;
  848. this.currentItem = null;
  849. this.currentItem = item;
  850. this.bSwitch = true;
  851. console.log("this.currentItem1 ==:", this.currentItem, this.bOpenBluetooth,
  852. this.BLEConnectDevice);
  853. this.startBluetoothDeviceDiscovery();
  854. }
  855. });
  856. return;
  857. }
  858. this.currentItem = null;
  859. this.currentItem = item;
  860. // console.log("this.currentItem ==2:", this.currentItem, this.bOpenBluetooth, this.BLEConnectDevice);
  861. this.startBluetoothDeviceDiscovery();
  862. }
  863. },
  864. //跳转进入升级页面,
  865. onNavUpdateDevice() {
  866. // console.log('onNavUpdateDevice');
  867. // if (!this.option) {
  868. // uni.showToast({
  869. // title: 'option null',
  870. // icon: 'none'
  871. // })
  872. // return;
  873. // }
  874. //需要连接设备后,才能进入升级
  875. if (!this.currentItem || !this.currentItem.bRatio || !this.BLEConnectDevice) {
  876. uni.showToast({
  877. title: '请先连接硬件!',
  878. icon: 'none'
  879. })
  880. return;
  881. }
  882. // if (this.BLEConnectDevice.deviceType != this.option.deviceType) {
  883. // uni.showToast({
  884. // title: '设备类型不对',
  885. // icon: 'none'
  886. // })
  887. // return;
  888. // }
  889. this.bLimitClose = true;
  890. uni.navigateTo({
  891. // url: "../devices-update/devices-update?deviceType=" + this.option.deviceType
  892. url: "../devices-update/devices-update"
  893. })
  894. },
  895. onGetDevice() {
  896. uni.getBluetoothDevices({
  897. success(res) {
  898. console.log("getBluetoothDevices:", res)
  899. }
  900. })
  901. uni.getConnectedBluetoothDevices({
  902. success(res) {
  903. console.log("getConnectedBluetoothDevices:", res)
  904. }
  905. })
  906. },
  907. onSwiperChange(e) {
  908. let currentIndex = e.detail.current;
  909. // console.log(currentIndex);
  910. if (currentIndex === this._videoIndex) {
  911. return;
  912. }
  913. if (currentIndex === 2) {
  914. setTimeout(() => {
  915. this.threeZIndex = 2;
  916. }, 200)
  917. } else {
  918. this.threeZIndex = 0;
  919. }
  920. // let isNext = false;
  921. // if (currentIndex === 0 && this._videoIndex === this.videoList.length - 1) {
  922. // isNext = true;
  923. // } else if (currentIndex === this.videoList.length - 1 && this._videoIndex === 0) {
  924. // isNext = false;
  925. // } else if (currentIndex > this._videoIndex) {
  926. // isNext = true;
  927. // }
  928. // if (isNext) {
  929. // this._videoDataIndex++;
  930. // } else {
  931. // this._videoDataIndex--;
  932. // }
  933. // if (this._videoDataIndex < 0) {
  934. // this._videoDataIndex = this.videoDataList.length - 1;
  935. // } else if (this._videoDataIndex >= this.videoDataList.length) {
  936. // this._videoDataIndex = 0;
  937. // }
  938. // this.circular = (this._videoDataIndex != 0);
  939. // if (this._videoIndex >= 0) {
  940. // this._videoContextList[this._videoIndex].pause();
  941. // this._videoContextList[this._videoIndex].seek(0);
  942. // }
  943. // this._videoIndex = currentIndex;
  944. },
  945. //禁止用户手动滑动
  946. stopTouchMove() {
  947. // return;
  948. },
  949. onSwiperClick() {
  950. console.log('onSwiperClick');
  951. this.currentIndex++;
  952. if (this.currentIndex === 2) {
  953. setTimeout(() => {
  954. this.threeZIndex = 2;
  955. }, 200)
  956. } else {
  957. this.threeZIndex = 0;
  958. }
  959. },
  960. onSwiperItemClick(e) {
  961. console.log('onSwiperItemClick');
  962. }
  963. }
  964. }
  965. </script>
  966. <style>
  967. .hardware-border {
  968. border: 1rpx solid #9898FF;
  969. box-sizing: border-box;
  970. }
  971. .swiper {
  972. flex: 1;
  973. background-color: rgba(0, 0, 0, 0.32);
  974. }
  975. .swiper-item {
  976. flex: 1;
  977. }
  978. .bluetooth-guide-number {
  979. border-radius: 18px;
  980. border-width: 1rpx;
  981. width: 30px;
  982. height: 30px;
  983. text-align: center;
  984. line-height: 30px;
  985. color: #000000;
  986. /* border-color: #FFFFFF; */
  987. background-color: #FFFFFF;
  988. margin-right: 13px;
  989. }
  990. </style>