devices-hardware.vue 30 KB

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