devices-hardware.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. <template>
  2. <view>
  3. <uni-nav-bar id="nav-bar" status-bar="true" @clickLeft="onBack()" @clickRight="onNavUpdateDevice()" title="选择设备类型"
  4. 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"></image>
  13. </view>
  14. </view>
  15. </uni-nav-bar>
  16. <view class="card-view padding-top padding-bottom" v-for="(item,index) in devicesList" :key="index" :class="item.bRatio?'hardware-border':''">
  17. <view class="flex justify-between align-center">
  18. <view class="flex justify-start align-center">
  19. <image style="margin-left: 30rpx; width: 200rpx;height: 120rpx;" :src="item.icon" mode="aspectFit"></image>
  20. <view>
  21. <view style="margin: 20rpx 50rpx; font-weight: bold; font-size: 18px; color: #565656;">{{item.cname}}</view>
  22. <view style="margin-left: 50rpx; font-size: 12px;" class="make-text-bPurple">{{item.describe}}</view>
  23. </view>
  24. </view>
  25. <image style="margin-right: 60rpx; width: 60rpx;height: 60rpx;" :src="item.bRatio?'/static/devicesOther/radio-b.png':'/static/devicesOther/radio-g.png'"
  26. mode="aspectFit" @tap="_onRadio(item,$event)"></image>
  27. </view>
  28. </view>
  29. <!-- <button class="margin" @click="onWriteBLEConnectionValue({value:'M'})">设备版本</button> -->
  30. <!-- <button class="margin" @click="onWriteBLEConnectionValue({value:'4'})">加速计关</button> -->
  31. </view>
  32. </template>
  33. <script>
  34. import config from '@/common/config.js'
  35. import reqUtil from '@/util/util-js/requstUtil.js';
  36. import {
  37. mapState,
  38. mapMutations
  39. } from 'vuex';
  40. export default {
  41. computed: mapState(['bOpenBluetooth', 'bOpenSuccess', 'bListenAdapterStateChange', 'bListenDeviceFound',
  42. 'BLEConnectDevice', 'BLEGetServices', 'cIndex', 'bConnection', 'BLEInfoList', 'BLEDeviceShowList', 'finallyUseDevice'
  43. ]),
  44. data() {
  45. return {
  46. // 设备列表
  47. devicesList: [],
  48. currentItem: null,
  49. //设置一个旧的连接item
  50. oldItem: null,
  51. searchObj: null,
  52. //设置搜索超时时间
  53. searchTimeOut: null,
  54. option: null,
  55. saveObj: null,
  56. //搜索提示定时器
  57. searchMac: null,
  58. //
  59. getServicesTimeout:null,
  60. writeMacTimeout:null,
  61. //限制关闭连接
  62. bLimitClose:false
  63. }
  64. },
  65. onLoad(op) {
  66. // if (op.deviceType) {
  67. // console.log(op);
  68. // this.option = op;
  69. // }
  70. //目前是固定这个值,去掉了一级页面
  71. this.option = {
  72. cname: "手柄盒子",
  73. deviceType: 'BLEHandle'
  74. };
  75. if (this.option.deviceType == 'BLEHandle') {
  76. this.BLEInfoList.forEach((item, index, selfarr) => {
  77. if (item.deviceType == 'BLEHandle') {
  78. let item = Object.assign({}, selfarr[index]);
  79. this.devicesList.push(item);
  80. }
  81. })
  82. }
  83. uni.$on('callbackCloseBLE', this.hardCallbackCloseBLE);
  84. uni.$on('listenerBLE', this.onListenerBLE);
  85. },
  86. onUnload() {
  87. uni.$off('callbackCloseBLE', this.hardCallbackCloseBLE);
  88. uni.$off('listenerBLE', this.onListenerBLE);
  89. //清除定时器
  90. this.onClearTimeout();
  91. },
  92. onShow() {
  93. this.bLimitClose = false;
  94. // console.log(this.cIndex, this.BLEDeviceShowList, this.BLEConnectDevice,this.devicesList);
  95. //this.bConnection &&
  96. if (this.BLEConnectDevice) {
  97. for (let i = 0; i < this.devicesList.length; i++) {
  98. let eq = this.devicesList[i];
  99. if (
  100. (eq.ename.indexOf("mobilePhoneBandage") > -1 && this.BLEConnectDevice.id == 0) ||
  101. (eq.ename.indexOf("hotman") > -1 && this.BLEConnectDevice.id == 1) ||
  102. (eq.ename.indexOf("BLEHandle") > -1 && this.BLEConnectDevice.id == 2)) {
  103. eq.bRatio = true;
  104. this.currentItem = eq;
  105. }
  106. }
  107. } else {
  108. //如果没连接的话,设置一下显示状态
  109. for (let i = 0; i < this.devicesList.length; i++) {
  110. let eq = this.devicesList[i];
  111. eq.bRatio = false;
  112. }
  113. // this.currentItem = null;
  114. }
  115. },
  116. onHide() {},
  117. methods: {
  118. ...mapMutations(['initAdapter', 'onCreateBLESuccess', 'onGetBLEDeviceServices', 'onOnlyCloseBLEConnection',
  119. 'onGetRSSITransDistance',
  120. 'addBLEDevice', 'onWriteBLEConnectionValue', 'deleteBLEDevice'
  121. ]),
  122. //
  123. onClearTimeout() {
  124. // 退出后,清除计时器
  125. if (this.searchMac) {
  126. clearTimeout(this.searchMac);
  127. this.searchMac = null;
  128. }
  129. //servicesTimeout
  130. if (this.getServicesTimeout) {
  131. clearTimeout(this.getServicesTimeout);
  132. this.getServicesTimeout = null;
  133. }
  134. //写入指令writeMacTimeout
  135. if (this.writeMacTimeout) {
  136. clearTimeout(this.writeMacTimeout);
  137. this.writeMacTimeout = null;
  138. }
  139. if (this.searchTimeOut) {
  140. clearTimeout(this.searchTimeOut);
  141. this.searchTimeOut = null;
  142. }
  143. },
  144. //监听回调
  145. onListenerBLE(res) {
  146. console.log('onListenerBLE:',res);
  147. if (res.type !== 'mac') return;
  148. //如果mac 回调了不用提示
  149. if (this.searchMac) {
  150. clearTimeout(this.searchMac);
  151. this.searchMac = null;
  152. }
  153. let _self = this;
  154. let mac = res.value;
  155. console.log('mac =====', mac);
  156. //用返回的mac 判断,如果非法,则断开连接
  157. //就判断一下是否
  158. //测试
  159. // mac="BB:34:24:22:77:88";
  160. reqUtil.requestData(config.URL.BLEFINDHASBIND, {
  161. mac: mac,
  162. }).then(
  163. res => {
  164. console.log('BLEFINDHASBIND:', res);
  165. // uni.hideToast();
  166. if (res.code == 0) {
  167. //code = 0 是新的地址,没绑定过
  168. //绑定设备使用者
  169. reqUtil.requestData(config.URL.BLEBIND, {
  170. mac: mac,
  171. }).then(
  172. res => {
  173. // console.log('BLEBIND:', res);
  174. if (res.code == 0) {
  175. //绑定成功,添加设备
  176. if (_self.saveObj == null) return;
  177. _self.addBLEDevice(_self.saveObj);
  178. _self.ConnectionSuccess();
  179. }
  180. },
  181. e => {
  182. console.log(e);
  183. //绑定失败, 关闭当前连接
  184. _self.onOnlyCloseBLEConnection({
  185. getSuccess: () => {
  186. _self.currentItem.bRatio = false;
  187. _self.currentItem = null;
  188. _self.saveObj = null;
  189. }
  190. });
  191. }
  192. );
  193. } else if (res.code == 721) {
  194. //是自己绑定的,添加设备
  195. if (_self.saveObj == null) return;
  196. _self.addBLEDevice(_self.saveObj);
  197. _self.ConnectionSuccess();
  198. } else if (res.code == 711 || res.code == 722) {
  199. //res.code = 711 蓝牙设备不是我们自己的
  200. //res.code = 722 蓝牙设备别人使用过
  201. uni.hideToast();
  202. uni.showModal({
  203. title: '绑定蓝牙失败',
  204. content: res.msg + ',可重新尝试连接。如有疑问请联系官方客服。'
  205. })
  206. //关闭当前连接
  207. _self.onOnlyCloseBLEConnection({
  208. getSuccess: () => {
  209. _self.currentItem = null;
  210. _self.saveObj = null;
  211. }
  212. });
  213. }
  214. },
  215. e => {
  216. console.log(e);
  217. }
  218. );
  219. },
  220. ConnectionSuccess() {
  221. // console.log(this.cIndex, this.BLEDeviceShowList, this.BLEConnectDevice);
  222. uni.showToast({
  223. icon: "none",
  224. title: "连接设备成功!",
  225. duration: 2000
  226. })
  227. if (this.BLEConnectDevice) {
  228. for (let i = 0; i < this.devicesList.length; i++) {
  229. let eq = this.devicesList[i];
  230. if (
  231. (eq.ename.indexOf("mobilePhoneBandage") > -1 && this.BLEConnectDevice.id == 0) ||
  232. (eq.ename.indexOf("hotman") > -1 && this.BLEConnectDevice.id == 1) ||
  233. (eq.ename.indexOf("BLEHandle") > -1 && this.BLEConnectDevice.id == 2)) {
  234. eq.bRatio = true;
  235. this.currentItem = eq;
  236. }
  237. }
  238. }
  239. },
  240. hardCallbackCloseBLE() {
  241. //如果限制不走重连
  242. if(this.bLimitClose)return;
  243. console.log(this.currentItem, this.oldItem);
  244. if (this.oldItem &&this.currentItem&& this.currentItem.id == this.oldItem.id) {
  245. // this.$store.state.bConnection = false;
  246. if (this.currentItem.bRatio) {
  247. uni.showToast({
  248. title: '设备断开连接!',
  249. icon: 'none',
  250. duration: 2000,
  251. mask: true
  252. })
  253. this.currentItem.bRatio = false;
  254. this.oldItem = null;
  255. this.currentItem = null;
  256. }
  257. }else if(this.bConnection && this.BLEConnectDevice){
  258. //假如匹配过程中断开连接
  259. this.$store.state.BLEConnectDevice = null;
  260. this.$store.state.bConnection = false;
  261. uni.hideToast();
  262. }
  263. },
  264. /**
  265. * 开始查找设备
  266. * */
  267. startBluetoothDeviceDiscovery() {
  268. //在页面显示的时候判断是都已经初始化完成蓝牙适配器若成功,则开始查找设备
  269. let _self = this;
  270. if (_self.bOpenBluetooth) {
  271. //先查询最近使用的硬件,如果最近使用的和当前需要连接的item id一样,则认为是
  272. if (_self.finallyUseDevice !== null) {
  273. if (_self.finallyUseDevice.id == _self.currentItem.id) {
  274. //currentItem 是mode 页面选中的item
  275. let obj = Object.assign({}, _self.finallyUseDevice, _self.currentItem);
  276. //finallyUserDevice 就是最后一次使用搜索到的设备信息
  277. _self.saveObj = _self.finallyUseDevice;
  278. uni.showToast({
  279. title: '设备连接中...',
  280. icon: 'loading',
  281. duration: 10000,
  282. mask: true
  283. })
  284. // 先直连,然后判断版本
  285. _self._onConnectDevice(obj);
  286. } else {
  287. uni.showModal({
  288. title: '连接提示!',
  289. content: '检测到使用过旧手柄设备,是否使用旧设备切换新模式。否则继续搜索新手柄设备...',
  290. success: (res) => {
  291. if (res.confirm) {
  292. //currentItem 是mode 页面选中的item
  293. let obj = Object.assign({}, _self.finallyUseDevice, _self.currentItem);
  294. //finallyUserDevice 就是最后一次使用搜索到的设备信息
  295. _self.saveObj = Object.assign({}, _self.finallyUseDevice, {
  296. id: _self.currentItem.id
  297. });
  298. uni.showToast({
  299. title: '设备连接中...',
  300. icon: 'loading',
  301. duration: 10000,
  302. mask: true
  303. })
  304. console.log("_onConnectDevice:::===", obj);
  305. // 先直连,然后判断版本
  306. _self._onConnectDevice(obj);
  307. } else if (res.cancel) {
  308. _self.onCanStart();
  309. }
  310. }
  311. })
  312. }
  313. return;
  314. }
  315. _self.onCanStart();
  316. } else {
  317. _self.initAdapter(() => {
  318. _self.startBluetoothDeviceDiscovery();
  319. });
  320. }
  321. },
  322. //开始搜索
  323. onCanStart() {
  324. let _self = this;
  325. uni.showToast({
  326. title: '设备连接中...',
  327. icon: 'loading',
  328. duration: 10000,
  329. mask: true
  330. })
  331. // 1.已经搜索到的,根据蓝牙回调的mac 地址判断合法性。
  332. uni.startBluetoothDevicesDiscovery({
  333. allowDuplicatesKey: true,
  334. success: res => {
  335. console.log("startBluetoothDevicesDiscovery:", res);
  336. _self.onBluetoothDeviceFound();
  337. },
  338. fail: res => {
  339. console.log("搜索失败!");
  340. _self.initAdapter(() => {
  341. _self.startBluetoothDeviceDiscovery();
  342. });
  343. }
  344. });
  345. // 2.没有搜索到的,一段时间后处理。比如手机已经连接了设备,但是app 里面搜索不到,也没记录使用过的。
  346. if (_self.searchTimeOut) {
  347. clearTimeout(_self.searchTimeOut);
  348. _self.searchTimeOut = null;
  349. }
  350. _self.searchTimeOut = setTimeout(() => {
  351. //1.搜索10秒钟之后,停止搜索,走其他流程判断
  352. _self.stopBluetoothDevicesDiscovery();
  353. uni.showModal({
  354. title: '搜索失败',
  355. content: '请开启设备或断开手机匹配,直到设备灯闪,再重新搜索连接。'
  356. })
  357. }, 10000)
  358. },
  359. /**
  360. * 停止搜索蓝牙设备
  361. */
  362. stopBluetoothDevicesDiscovery() {
  363. uni.stopBluetoothDevicesDiscovery({
  364. success: e => {
  365. console.log('停止搜索蓝牙设备:' + e.errMsg);
  366. },
  367. fail: e => {
  368. console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
  369. }
  370. });
  371. },
  372. /**
  373. * 发现外围设备
  374. */
  375. onBluetoothDeviceFound() {
  376. let _self = this;
  377. _self.searchObj = null;
  378. uni.onBluetoothDeviceFound(res => {
  379. /**
  380. * 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
  381. */
  382. // console.log("onBluetoothDeviceFound:", res);
  383. res.devices.forEach(device => {
  384. if (device.name.indexOf('PBox') > -1 || device.name.indexOf('BGBox') > -1) {
  385. //如果搜索的设备名 不是对应当前设备类型,过滤
  386. // if (device.name.indexOf(_self.currentItem.deviceName) == -1) return;
  387. if (_self.currentItem.deviceName.indexOf('PBox') == -1 && _self.currentItem.deviceName.indexOf('BGBox') == -
  388. 1) return;
  389. //寻找到对应设备时候,其余的返回
  390. if (_self.searchObj) return;
  391. _self.searchObj = device;
  392. if (_self.searchTimeOut) {
  393. clearTimeout(_self.searchTimeOut);
  394. _self.searchTimeOut = null;
  395. }
  396. //currentItem 是mode 页面选中的item
  397. let obj = Object.assign({}, device, _self.currentItem);
  398. _self.saveObj = Object.assign({}, {
  399. id: _self.currentItem.id
  400. }, device);
  401. console.log("****", obj, _self.saveObj, _self.currentItem)
  402. // 先直连,然后判断版本
  403. _self._onConnectDevice(obj);
  404. _self.stopBluetoothDevicesDiscovery();
  405. }
  406. })
  407. })
  408. },
  409. onBack() {
  410. uni.navigateBack({
  411. delta: 1
  412. })
  413. },
  414. // 提示点击连接设备
  415. _onConnectDevice(item) {
  416. //servicesTimeout
  417. if (this.getServicesTimeout) {
  418. clearTimeout(this.getServicesTimeout);
  419. this.getServicesTimeout = null;
  420. }
  421. //写入指令writeMacTimeout
  422. if (this.writeMacTimeout) {
  423. clearTimeout(this.writeMacTimeout);
  424. this.writeMacTimeout = null;
  425. }
  426. //如果已经连接,加上服务不存在,直接获取服务
  427. if(this.bConnection &&this.BLEGetServices&& this.BLEGetServices.length == 0){
  428. // console.log('onGetBLEDeviceServices');
  429. console.log("***直接获取服务*******");
  430. this.onGetBLEDeviceServices({
  431. item: item,
  432. success: (res) => {
  433. console.log("onGetBLEDeviceServices************");
  434. //连接成功了,设置旧的item
  435. this.oldItem = this.currentItem;
  436. // 初始化服务后,获取版本,判断
  437. // 停止蓝牙加速计
  438. this.writeMacTimeout = setTimeout(() => {
  439. // 发送获取mac
  440. this.onWriteBLEConnectionValue({
  441. value: "M"
  442. });
  443. if (this.searchMac) {
  444. clearTimeout(this.searchMac);
  445. this.searchMac = null;
  446. }
  447. this.searchMac = setTimeout(() => {
  448. uni.showModal({
  449. title: '提示',
  450. content: '检测设备失败,再次尝试连接或者 重启手柄(Reset键位也可)后再尝试重新连接'
  451. })
  452. this.onOnlyCloseBLEConnection({
  453. getSuccess: () => {
  454. if(this.currentItem)
  455. this.currentItem.bRatio = false;
  456. this.currentItem = null;
  457. }
  458. });
  459. }, 5000)
  460. }, 1500)
  461. }
  462. });
  463. return;
  464. }
  465. //创建一个连接,需要对应close
  466. this.onCreateBLESuccess({
  467. item: item,
  468. getSuccess: () => {
  469. console.log("****创建一个连接*******");
  470. this.getServicesTimeout = setTimeout(() => {
  471. this.onGetBLEDeviceServices({
  472. item: item,
  473. success: (res) => {
  474. console.log("onGetBLEDeviceServices************",res);
  475. //连接成功了,设置旧的item
  476. this.oldItem = this.currentItem;
  477. // 初始化服务后,获取版本,判断
  478. // 停止蓝牙加速计
  479. this.writeMacTimeout = setTimeout(() => {
  480. // 发送获取mac
  481. this.onWriteBLEConnectionValue({
  482. value: "M"
  483. });
  484. if (this.searchMac) {
  485. clearTimeout(this.searchMac);
  486. this.searchMac = null;
  487. }
  488. this.searchMac = setTimeout(() => {
  489. uni.showModal({
  490. title: '提示',
  491. content: '检测设备失败,再次尝试连接或者 重启手柄(Reset键位也可)后再尝试重新连接'
  492. })
  493. this.onOnlyCloseBLEConnection({
  494. getSuccess: () => {
  495. if(this.currentItem)
  496. this.currentItem.bRatio = false;
  497. this.currentItem = null;
  498. }
  499. });
  500. }, 5000)
  501. }, 1500)
  502. }
  503. });
  504. }, 2000);
  505. }
  506. })
  507. },
  508. _onRadio(item, event) {
  509. if (!item.bRatio) {
  510. console.log("this.currentItem ==:", this.currentItem, this.bOpenBluetooth, this.BLEConnectDevice);
  511. if (this.BLEConnectDevice) {
  512. this.onOnlyCloseBLEConnection({
  513. getSuccess: () => {
  514. this.currentItem.bRatio = false;
  515. this.currentItem = null;
  516. this.currentItem = item;
  517. this.startBluetoothDeviceDiscovery();
  518. }
  519. });
  520. return;
  521. }
  522. this.currentItem = null;
  523. this.currentItem = item;
  524. this.startBluetoothDeviceDiscovery();
  525. }
  526. },
  527. //跳转进入升级页面,
  528. onNavUpdateDevice() {
  529. // console.log('onNavUpdateDevice');
  530. if (!this.option) {
  531. uni.showToast({
  532. title: 'option null',
  533. icon: 'none'
  534. })
  535. return;
  536. }
  537. //需要连接设备后,才能进入升级
  538. if (!this.currentItem || !this.currentItem.bRatio || !this.BLEConnectDevice) {
  539. uni.showToast({
  540. title: '选择一个连接后进入',
  541. icon: 'none'
  542. })
  543. return;
  544. }
  545. if (this.BLEConnectDevice.deviceType != this.option.deviceType) {
  546. uni.showToast({
  547. title: '设备类型不对',
  548. icon: 'none'
  549. })
  550. return;
  551. }
  552. this.bLimitClose = true;
  553. uni.navigateTo({
  554. url: "../devices-update/devices-update?deviceType=" + this.option.deviceType
  555. })
  556. }
  557. }
  558. }
  559. </script>
  560. <style>
  561. .hardware-border {
  562. border: 1rpx solid #9898FF;
  563. box-sizing: border-box;
  564. }
  565. </style>