devices-hardware.vue 35 KB

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