devices-hardware.vue 35 KB

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