devices-hardware.vue 35 KB

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