devices-hardware.vue 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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:', 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. ConnectionSuccess() {
  375. // console.log(this.cIndex, this.BLEDeviceShowList, this.BLEConnectDevice);
  376. // uni.showToast({
  377. // icon: "none",
  378. // title: "连接设备成功!",
  379. // duration: 2000
  380. // })
  381. for (let i = 0; i < this.devicesList.length; i++) {
  382. let eq = this.devicesList[i];
  383. if (
  384. (eq.ename.indexOf("mobilePhoneBandage") > -1 && this.BLEConnectDevice.id == 0) ||
  385. (eq.ename.indexOf("hotman") > -1 && this.BLEConnectDevice.id == 1) ||
  386. (eq.ename.indexOf("BLEHandle") > -1 && this.BLEConnectDevice.id == 2) ||
  387. (eq.ename.indexOf("rope") > -1 && this.BLEConnectDevice.id == 3)) {
  388. eq.bRatio = true;
  389. this.currentItem = eq;
  390. /**
  391. * 已验证的连接设置true
  392. */
  393. this.$store.state.bVerifiedConnection = true;
  394. // setTimeout(() => {
  395. // this.onCheckBondDevice();
  396. // }, 5000)
  397. }
  398. }
  399. },
  400. onCheckBondDevice() {
  401. console.log("onCheckBondDevice", this.bTestBondConnect);
  402. if (this.bTestBondConnect && this.bShow) {
  403. /**
  404. * 假如手机没有匹配,断开连接
  405. */
  406. console.log(2, this.finallyUseDevice);
  407. this.B_GetBondedDevices({
  408. deviceId: this.finallyUseDevice.deviceId,
  409. success: (bondedDevice) => {
  410. uni.hideToast();
  411. if (bondedDevice == null) {
  412. // if (plus.os.name == 'Android')
  413. {
  414. //此问题 华为手机容易出现
  415. //android手机已配对的设备 不存在,但是app 又直接连接成功了。提示,并且断开app连接
  416. //1.关闭当前连接
  417. this.onOnlyCloseBLEConnection({
  418. getSuccess: () => {
  419. this.currentItem.bRatio = false;
  420. this.currentItem = null;
  421. this.saveObj = null;
  422. }
  423. });
  424. //2.跳转蓝牙设置
  425. uni.showModal({
  426. title: '蓝牙配对失败',
  427. content: '请跳转后点击配对BGBox_2020,成功后手动跳转回哔蹦重新连接。',
  428. success: (res) => {
  429. if (res.confirm) {
  430. this.B_OpenBLESetting();
  431. }
  432. }
  433. })
  434. }
  435. }
  436. }
  437. });
  438. this.bTestBondConnect = false;
  439. } else {
  440. uni.hideToast();
  441. }
  442. },
  443. onRetryConnectBLESuccess() {
  444. this.ConnectionSuccess();
  445. },
  446. hardCallbackCloseBLE() {
  447. if (this.BLEConnectDevice == null && this.currentItem && !this.bSwitch) {
  448. this.currentItem.bRatio = false;
  449. this.currentItem = null;
  450. this.saveObj = null;
  451. }
  452. //如果限制不走重连
  453. if (this.bLimitClose) return;
  454. console.log(this.currentItem, this.oldItem);
  455. if (this.oldItem && this.currentItem && this.currentItem.id == this.oldItem.id) {
  456. // this.$store.state.bConnection = false;
  457. if (this.currentItem.bRatio) {
  458. uni.showToast({
  459. title: '设备断开连接!',
  460. icon: 'none',
  461. duration: 2000,
  462. mask: true
  463. })
  464. this.currentItem.bRatio = false;
  465. this.oldItem = null;
  466. this.currentItem = null;
  467. //断开连接
  468. this.$store.state.bVerifiedConnection = false;
  469. }
  470. } else if (this.bConnection && this.BLEConnectDevice) {
  471. //假如匹配过程中断开连接
  472. this.$store.state.BLEConnectDevice = null;
  473. this.$store.state.bConnection = false;
  474. // uni.hideToast();
  475. // console.log("//假如匹配过程中断开连接");
  476. uni.showToast({
  477. title: '连接失败,尝试重新连接。',
  478. icon: 'none',
  479. mask: true,
  480. duration: 2000
  481. })
  482. if (this.getServicesTimeout) {
  483. clearTimeout(this.getServicesTimeout);
  484. this.getServicesTimeout = null;
  485. }
  486. }
  487. },
  488. /**
  489. * 开始查找设备
  490. * */
  491. startBluetoothDeviceDiscovery() {
  492. //在页面显示的时候判断是都已经初始化完成蓝牙适配器若成功,则开始查找设备
  493. let _self = this;
  494. if (_self.bOpenBluetooth) {
  495. //1.第一步还是先进行设备搜索
  496. _self.onCanStart();
  497. } else {
  498. _self.initAdapter(() => {
  499. _self.startBluetoothDeviceDiscovery();
  500. });
  501. }
  502. },
  503. /**
  504. * 通过检测手机连接的设备进行连接,null 的话进行搜索操作
  505. */
  506. onBondedDeviceConnect() {
  507. let _self = this;
  508. //获取手机本身已连接的硬件,
  509. //示例
  510. // {
  511. // "deviceId": "C5:5C:19:04:00:30",
  512. // "name": "BGBox_202012",
  513. // "RSSI": -74,
  514. // "localName": "BGBox_20201",
  515. // "advertisServiceUUIDs": ["00001812-0000-1000-8000-00805F9B34FB", "0000FFF0-0000-1000-8000-00805F9B34FB"]
  516. // }
  517. console.log("onBoded ***************:", _self.finallyUseDevice);
  518. _self.B_GetBondedDevices({
  519. deviceId: _self.finallyUseDevice == null ? null : _self.finallyUseDevice.deviceId,
  520. success: (bondedDevice) => {
  521. console.log("bondedDevice:", bondedDevice);
  522. //获取已和蓝牙连接的设备
  523. if (bondedDevice != null) {
  524. //如果用户匹配了对应的设备,直接用对应的设备来连接
  525. let setDevice = {
  526. "deviceId": bondedDevice.address,
  527. "name": bondedDevice.name,
  528. "RSSI": -74,
  529. "localName": "",
  530. "advertisServiceUUIDs": ["00001812-0000-1000-8000-00805F9B34FB",
  531. "0000FFF0-0000-1000-8000-00805F9B34FB"
  532. ]
  533. }
  534. let obj = Object.assign({}, setDevice, _self.currentItem);
  535. //finallyUserDevice 就是最后一次使用搜索到的设备信息
  536. _self.saveObj = Object.assign({}, setDevice, {
  537. id: _self.currentItem.id
  538. });
  539. uni.showToast({
  540. title: '设备连接中...',
  541. icon: 'loading',
  542. duration: 10000,
  543. mask: true
  544. })
  545. console.log("GetBondedDevices:::===", setDevice, _self.currentItem, obj);
  546. // 先直连,然后判断版本
  547. _self._onConnectDevice(obj);
  548. //getBond后发起的连接,不需要检测了
  549. _self.bTestBondConnect = false;
  550. } else {
  551. console.log("没有获取到绑定的设备");
  552. uni.showToast({
  553. title: '获取匹配设备失败',
  554. icon: 'none',
  555. mask: true
  556. })
  557. }
  558. }
  559. });
  560. },
  561. //开始搜索
  562. onCanStart() {
  563. let _self = this;
  564. uni.showToast({
  565. title: '设备连接中...',
  566. icon: 'loading',
  567. duration: 15000,
  568. mask: true
  569. })
  570. // 1.已经搜索到的,根据蓝牙回调的mac 地址判断合法性。
  571. uni.startBluetoothDevicesDiscovery({
  572. allowDuplicatesKey: true,
  573. success: res => {
  574. console.log("startBluetoothDevicesDiscovery:", res);
  575. _self.bSwitch = false;
  576. _self.onBluetoothDeviceFound();
  577. },
  578. fail: res => {
  579. console.log("搜索失败!");
  580. _self.initAdapter(() => {
  581. _self.startBluetoothDeviceDiscovery();
  582. });
  583. }
  584. });
  585. // 2.没有搜索到的,一段时间后处理。比如手机已经连接了设备,但是app 里面搜索不到,也没记录使用过的。
  586. if (_self.searchTimeOut) {
  587. clearTimeout(_self.searchTimeOut);
  588. _self.searchTimeOut = null;
  589. }
  590. _self.searchTimeOut = setTimeout(() => {
  591. //1.搜索12秒钟之后,停止搜索
  592. _self.stopBluetoothDevicesDiscovery();
  593. //搜索失败后,再检查是否和手机配对的设备连接
  594. // _self.onBondedDeviceConnect();
  595. }, 8000)
  596. },
  597. /**
  598. * 停止搜索蓝牙设备
  599. */
  600. stopBluetoothDevicesDiscovery() {
  601. uni.stopBluetoothDevicesDiscovery({
  602. success: e => {
  603. console.log('停止搜索蓝牙设备:' + e.errMsg);
  604. },
  605. fail: e => {
  606. console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
  607. }
  608. });
  609. },
  610. /**
  611. * 发现外围设备
  612. */
  613. onBluetoothDeviceFound() {
  614. let _self = this;
  615. _self.searchObj = null;
  616. uni.onBluetoothDeviceFound(res => {
  617. /**
  618. * 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
  619. */
  620. // console.log("onBluetoothDeviceFound:", res);
  621. res.devices.forEach(device => {
  622. if (device.name.indexOf('PBox') > -1 || device.name.indexOf('BGBox') > -1 || device
  623. .name.indexOf('Rope') > -1) {
  624. //如果搜索的设备名 不是对应当前设备类型,过滤
  625. // if (device.name.indexOf(_self.currentItem.deviceName) == -1) return;
  626. if (_self.currentItem.deviceName.indexOf('PBox') == -1 &&
  627. _self.currentItem.deviceName.indexOf('BGBox') == -1 &&
  628. _self.currentItem.deviceName.indexOf('Rope') == -1
  629. ) return;
  630. //寻找到对应设备时候,其余的返回
  631. if (_self.searchObj) return;
  632. _self.searchObj = device;
  633. if (_self.searchTimeOut) {
  634. clearTimeout(_self.searchTimeOut);
  635. _self.searchTimeOut = null;
  636. }
  637. //currentItem 是mode 页面选中的item
  638. let obj = Object.assign({}, device, _self.currentItem);
  639. _self.saveObj = Object.assign({}, {
  640. id: _self.currentItem.id
  641. }, device);
  642. console.log(device, "****", obj, _self.saveObj, _self.currentItem)
  643. // 先直连,然后判断版本
  644. _self._onConnectDevice(obj);
  645. _self.stopBluetoothDevicesDiscovery();
  646. }
  647. })
  648. })
  649. },
  650. onBack() {
  651. uni.navigateBack({
  652. delta: 1
  653. })
  654. },
  655. // 提示点击连接设备
  656. _onConnectDevice(item) {
  657. //servicesTimeout
  658. if (this.getServicesTimeout) {
  659. clearTimeout(this.getServicesTimeout);
  660. this.getServicesTimeout = null;
  661. }
  662. //写入指令writeMacTimeout
  663. if (this.writeMacTimeout) {
  664. clearTimeout(this.writeMacTimeout);
  665. this.writeMacTimeout = null;
  666. }
  667. //如果已经连接,加上服务不存在,直接获取服务
  668. if (this.bConnection && this.BLEGetServices && this.BLEGetServices.length == 0) {
  669. console.log("***直接获取服务*******");
  670. this.onGetBLEDeviceServices({
  671. item: item,
  672. success: (res) => {
  673. console.log("*****getBLEDeviceServices************");
  674. //连接成功了,设置旧的item
  675. this.oldItem = this.currentItem;
  676. uni.showToast({
  677. title: '正在验证设备信息...',
  678. icon: 'loading',
  679. duration: 10000,
  680. mask: true
  681. })
  682. // 初始化服务后,获取版本,判断
  683. // 停止蓝牙加速计
  684. this.writeMacTimeout = setTimeout(() => {
  685. //停止蓝牙加速计
  686. this.onWriteBLEConnectionValue({
  687. value: "4"
  688. });
  689. // 发送获取mac
  690. this.onWriteBLEConnectionValue({
  691. value: "M"
  692. });
  693. if (this.searchMac) {
  694. clearTimeout(this.searchMac);
  695. this.searchMac = null;
  696. }
  697. this.searchMac = setTimeout(() => {
  698. uni.hideToast();
  699. uni.showModal({
  700. title: '提示',
  701. content: '检测设备失败。\r\n1.检查手机蓝牙是否已连接上设备。\r\n2.再次尝试连接或者 重启手柄(Reset键位也可)后再尝试连接'
  702. })
  703. this.onOnlyCloseBLEConnection({
  704. getSuccess: () => {
  705. if (this.currentItem)
  706. this.currentItem.bRatio = false;
  707. this.currentItem = null;
  708. }
  709. });
  710. }, 6000)
  711. }, 3000)
  712. }
  713. });
  714. return;
  715. }
  716. //创建一个连接,需要对应close
  717. this.onCreateBLESuccess({
  718. item: item,
  719. getSuccess: () => {
  720. console.log("****创建一个连接*******");
  721. this.getServicesTimeout = setTimeout(() => {
  722. this.onGetBLEDeviceServices({
  723. item: item,
  724. success: (res) => {
  725. // console.log("******getBLEDeviceServices************", res);
  726. //连接成功了,设置旧的item
  727. this.oldItem = this.currentItem;
  728. uni.showToast({
  729. title: '正在验证设备信息...',
  730. icon: 'loading',
  731. duration: 10000,
  732. mask: true
  733. })
  734. // 初始化服务后,获取版本,判断
  735. // 停止蓝牙加速计
  736. this.writeMacTimeout = setTimeout(() => {
  737. //停止蓝牙加速计
  738. this.onWriteBLEConnectionValue({
  739. value: "4"
  740. });
  741. // 发送获取mac
  742. this.onWriteBLEConnectionValue({
  743. value: "M"
  744. });
  745. if (this.searchMac) {
  746. clearTimeout(this.searchMac);
  747. this.searchMac = null;
  748. }
  749. this.searchMac = setTimeout(() => {
  750. uni.hideToast();
  751. uni.showModal({
  752. title: '提示',
  753. content: '检测设备失败。\r\n1.检查手机蓝牙是否已连接上设备。\r\n2.再次尝试连接或者 重启手柄(Reset键位也可)后再尝试连接'
  754. })
  755. this.onOnlyCloseBLEConnection({
  756. getSuccess: () => {
  757. if (this
  758. .currentItem
  759. )
  760. this
  761. .currentItem
  762. .bRatio =
  763. false;
  764. this.currentItem =
  765. null;
  766. }
  767. });
  768. }, 6000)
  769. }, 3000)
  770. }
  771. });
  772. }, 2500);
  773. }
  774. })
  775. },
  776. _onRadio(item, event) {
  777. if (!item.bRatio) {
  778. //设置默认值
  779. this.bTestBondConnect = true;
  780. if (this.BLEConnectDevice) {
  781. this.onOnlyCloseBLEConnection({
  782. getSuccess: () => {
  783. if (this.currentItem)
  784. this.currentItem.bRatio = false;
  785. this.currentItem = null;
  786. this.currentItem = item;
  787. this.bSwitch = true;
  788. console.log("this.currentItem1 ==:", this.currentItem, this.bOpenBluetooth,
  789. this.BLEConnectDevice);
  790. this.startBluetoothDeviceDiscovery();
  791. }
  792. });
  793. return;
  794. }
  795. this.currentItem = null;
  796. this.currentItem = item;
  797. console.log("this.currentItem ==2:", this.currentItem, this.bOpenBluetooth, this.BLEConnectDevice);
  798. this.startBluetoothDeviceDiscovery();
  799. }
  800. },
  801. //跳转进入升级页面,
  802. onNavUpdateDevice() {
  803. // console.log('onNavUpdateDevice');
  804. // if (!this.option) {
  805. // uni.showToast({
  806. // title: 'option null',
  807. // icon: 'none'
  808. // })
  809. // return;
  810. // }
  811. //需要连接设备后,才能进入升级
  812. if (!this.currentItem || !this.currentItem.bRatio || !this.BLEConnectDevice) {
  813. uni.showToast({
  814. title: '请先连接硬件!',
  815. icon: 'none'
  816. })
  817. return;
  818. }
  819. // if (this.BLEConnectDevice.deviceType != this.option.deviceType) {
  820. // uni.showToast({
  821. // title: '设备类型不对',
  822. // icon: 'none'
  823. // })
  824. // return;
  825. // }
  826. this.bLimitClose = true;
  827. uni.navigateTo({
  828. // url: "../devices-update/devices-update?deviceType=" + this.option.deviceType
  829. url: "../devices-update/devices-update"
  830. })
  831. },
  832. onGetDevice() {
  833. uni.getBluetoothDevices({
  834. success(res) {
  835. console.log("getBluetoothDevices:", res)
  836. }
  837. })
  838. uni.getConnectedBluetoothDevices({
  839. success(res) {
  840. console.log("getConnectedBluetoothDevices:", res)
  841. }
  842. })
  843. },
  844. onSwiperChange(e) {
  845. let currentIndex = e.detail.current;
  846. // console.log(currentIndex);
  847. if (currentIndex === this._videoIndex) {
  848. return;
  849. }
  850. if (currentIndex === 2) {
  851. setTimeout(() => {
  852. this.threeZIndex = 2;
  853. }, 200)
  854. } else {
  855. this.threeZIndex = 0;
  856. }
  857. // let isNext = false;
  858. // if (currentIndex === 0 && this._videoIndex === this.videoList.length - 1) {
  859. // isNext = true;
  860. // } else if (currentIndex === this.videoList.length - 1 && this._videoIndex === 0) {
  861. // isNext = false;
  862. // } else if (currentIndex > this._videoIndex) {
  863. // isNext = true;
  864. // }
  865. // if (isNext) {
  866. // this._videoDataIndex++;
  867. // } else {
  868. // this._videoDataIndex--;
  869. // }
  870. // if (this._videoDataIndex < 0) {
  871. // this._videoDataIndex = this.videoDataList.length - 1;
  872. // } else if (this._videoDataIndex >= this.videoDataList.length) {
  873. // this._videoDataIndex = 0;
  874. // }
  875. // this.circular = (this._videoDataIndex != 0);
  876. // if (this._videoIndex >= 0) {
  877. // this._videoContextList[this._videoIndex].pause();
  878. // this._videoContextList[this._videoIndex].seek(0);
  879. // }
  880. // this._videoIndex = currentIndex;
  881. },
  882. //禁止用户手动滑动
  883. stopTouchMove() {
  884. // return;
  885. },
  886. onSwiperClick() {
  887. console.log('onSwiperClick');
  888. this.currentIndex++;
  889. if (this.currentIndex === 2) {
  890. setTimeout(() => {
  891. this.threeZIndex = 2;
  892. }, 200)
  893. } else {
  894. this.threeZIndex = 0;
  895. }
  896. },
  897. onSwiperItemClick(e) {
  898. console.log('onSwiperItemClick');
  899. }
  900. }
  901. }
  902. </script>
  903. <style>
  904. .hardware-border {
  905. border: 1rpx solid #9898FF;
  906. box-sizing: border-box;
  907. }
  908. .swiper {
  909. flex: 1;
  910. background-color: rgba(0, 0, 0, 0.32);
  911. }
  912. .swiper-item {
  913. flex: 1;
  914. }
  915. .bluetooth-guide-number {
  916. border-radius: 18px;
  917. border-width: 1rpx;
  918. width: 30px;
  919. height: 30px;
  920. text-align: center;
  921. line-height: 30px;
  922. color: #000000;
  923. /* border-color: #FFFFFF; */
  924. background-color: #FFFFFF;
  925. margin-right: 13px;
  926. }
  927. </style>