devices-hardware.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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: 20rpx; width: 200rpx;height: 120rpx;" :src="item.icon" mode="aspectFit"></image>
  20. <view style="width: 350rpx;margin-left: 10rpx;">
  21. <view style="margin: 20rpx 0rpx; font-weight: bold; font-size: 18px; color: #565656;">{{item.cname}}</view>
  22. <view style="font-size: 12px; white-space:pre-wrap;" 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 @click="onGetDevice()"> onGetDevice</button> -->
  30. <!-- <view class="margin">
  31. <view class="make-text-bPurple text-14px">** 匹配步骤 **</view>
  32. <view class="make-text-bPurple text-14px">1.长按游戏手柄直到蓝灯正常闪烁</view>
  33. <view class="make-text-bPurple text-14px">2.蓝灯正常闪烁后,选择下面模式匹配手柄</view>
  34. </view> -->
  35. </view>
  36. </template>
  37. <script>
  38. import config from '@/common/config.js'
  39. import reqUtil from '@/util/util-js/requstUtil.js';
  40. import {
  41. mapState,
  42. mapMutations
  43. } from 'vuex';
  44. export default {
  45. computed: mapState(['bOpenBluetooth', 'bOpenSuccess', 'bListenAdapterStateChange', 'bListenDeviceFound',
  46. 'BLEConnectDevice', 'BLEGetServices', 'cIndex', 'bConnection', 'bVerifiedConnection', 'BLEInfoList',
  47. 'BLEDeviceShowList', 'finallyUseDevice'
  48. ]),
  49. data() {
  50. return {
  51. // 设备列表
  52. devicesList: [],
  53. currentItem: null,
  54. //设置一个旧的连接item
  55. oldItem: null,
  56. searchObj: null,
  57. //设置搜索超时时间
  58. searchTimeOut: null,
  59. option: null,
  60. saveObj: null,
  61. //搜索提示定时器
  62. searchMac: null,
  63. //
  64. getServicesTimeout: null,
  65. writeMacTimeout: null,
  66. //限制关闭连接
  67. bLimitClose: false,
  68. //是否需要检测发起的连接蓝牙是否匹配上
  69. bTestBondConnect: true,
  70. //是否显示
  71. bShow: true,
  72. //切换连接
  73. bSwitch: false
  74. }
  75. },
  76. onLoad(op) {
  77. // if (op.deviceType) {
  78. // console.log(op);
  79. // this.option = op;
  80. // }
  81. //目前是固定这个值,去掉了一级页面
  82. // this.option = {
  83. // cname: "手柄盒子",
  84. // deviceType: 'BLEHandle'
  85. // };
  86. // if (this.option.deviceType == 'BLEHandle') {
  87. // this.BLEInfoList.forEach((item, index, selfarr) => {
  88. // if (item.deviceType == 'BLEHandle' || item.deviceType == 'BLERope') {
  89. // let item = Object.assign({}, selfarr[index]);
  90. // this.devicesList.push(item);
  91. // }
  92. // })
  93. // }
  94. this.BLEInfoList.forEach((item, index, selfarr) => {
  95. //item.deviceType == 'BLEHandle' ||
  96. if (item.deviceType == 'BLERope') {
  97. let item = Object.assign({}, selfarr[index]);
  98. this.devicesList.push(item);
  99. }
  100. })
  101. uni.$on('retryConnectBLESuccess', this.onRetryConnectBLESuccess);
  102. uni.$on('callbackCloseBLE', this.hardCallbackCloseBLE);
  103. uni.$on('listenerBLE', this.onListenerBLE);
  104. },
  105. onUnload() {
  106. uni.$off('retryConnectBLESuccess', this.onRetryConnectBLESuccess);
  107. uni.$off('callbackCloseBLE', this.hardCallbackCloseBLE);
  108. uni.$off('listenerBLE', this.onListenerBLE);
  109. //清除定时器
  110. this.onClearTimeout();
  111. },
  112. onShow() {
  113. this.bShow = true;
  114. this.bLimitClose = false;
  115. console.log(this.cIndex, this.BLEDeviceShowList, this.BLEConnectDevice, this.devicesList);
  116. //this.bConnection &&
  117. if (this.bVerifiedConnection && this.BLEConnectDevice) {
  118. for (let i = 0; i < this.devicesList.length; i++) {
  119. let eq = this.devicesList[i];
  120. if (
  121. (eq.ename.indexOf("mobilePhoneBandage") > -1 && this.BLEConnectDevice.id == 0) ||
  122. (eq.ename.indexOf("hotman") > -1 && this.BLEConnectDevice.id == 1) ||
  123. (eq.ename.indexOf("BLEHandle") > -1 && this.BLEConnectDevice.id == 2)||
  124. (eq.ename.indexOf("rope") > -1 && this.BLEConnectDevice.id == 3)) {
  125. eq.bRatio = true;
  126. this.currentItem = eq;
  127. // setTimeout(() => {
  128. // this.onCheckBondDevice();
  129. // }, 5000)
  130. }
  131. }
  132. } else {
  133. //如果没连接的话,设置一下显示状态
  134. for (let i = 0; i < this.devicesList.length; i++) {
  135. let eq = this.devicesList[i];
  136. eq.bRatio = false;
  137. }
  138. // this.currentItem = null;
  139. }
  140. },
  141. onHide() {
  142. //如果蓝牙弹出匹配框,会触发onHide。这时候处理蓝牙连接流程检测应等onShow 时候,再检测
  143. this.bShow = false;
  144. },
  145. methods: {
  146. ...mapMutations(['initAdapter', 'onCreateBLESuccess', 'onGetBLEDeviceServices', 'onOnlyCloseBLEConnection',
  147. 'onGetRSSITransDistance',
  148. 'addBLEDevice', 'onWriteBLEConnectionValue', 'deleteBLEDevice', 'B_GetBondedDevices', 'B_OpenBLESetting'
  149. ]),
  150. //
  151. onClearTimeout() {
  152. // 退出后,清除计时器
  153. if (this.searchMac) {
  154. clearTimeout(this.searchMac);
  155. this.searchMac = null;
  156. }
  157. //servicesTimeout
  158. if (this.getServicesTimeout) {
  159. clearTimeout(this.getServicesTimeout);
  160. this.getServicesTimeout = null;
  161. }
  162. //写入指令writeMacTimeout
  163. if (this.writeMacTimeout) {
  164. clearTimeout(this.writeMacTimeout);
  165. this.writeMacTimeout = null;
  166. }
  167. if (this.searchTimeOut) {
  168. clearTimeout(this.searchTimeOut);
  169. this.searchTimeOut = null;
  170. }
  171. },
  172. //监听回调
  173. onListenerBLE(res) {
  174. // console.log('onListenerBLE:', res);
  175. if (res.type !== 'mac') return;
  176. //如果mac 回调了不用提示
  177. if (this.searchMac) {
  178. clearTimeout(this.searchMac);
  179. this.searchMac = null;
  180. }
  181. let _self = this;
  182. let mac = res.value;
  183. console.log('mac =====', mac);
  184. //用返回的mac 判断,如果非法,则断开连接
  185. //就判断一下是否
  186. //测试
  187. // mac="BB:34:24:22:77:88";
  188. // mac="较è¾";
  189. //判断mac地址是否是合理的格式
  190. var tempMacRegExp = /[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}/;
  191. if (!tempMacRegExp.test(mac)) {
  192. uni.hideToast();
  193. uni.showModal({
  194. title: '验证信息',
  195. content: '获取Mac地址失败,请重新连接。'
  196. })
  197. //关闭当前连接
  198. _self.onOnlyCloseBLEConnection({
  199. getSuccess: () => {
  200. _self.currentItem = null;
  201. _self.saveObj = null;
  202. }
  203. });
  204. return;
  205. }
  206. reqUtil.requestData(config.URL.BLEFINDHASBIND, {
  207. mac: mac,
  208. }).then(
  209. res => {
  210. console.log('BLEFINDHASBIND:', res);
  211. if (res.code == 0) {
  212. //code = 0 是新的地址,没绑定过
  213. //绑定设备使用者
  214. reqUtil.requestData(config.URL.BLEBIND, {
  215. mac: mac,
  216. }).then(
  217. res => {
  218. // console.log('BLEBIND:', res);
  219. if (res.code == 0) {
  220. //绑定成功,添加设备
  221. if (_self.saveObj == null) return;
  222. _self.addBLEDevice(_self.saveObj);
  223. _self.ConnectionSuccess();
  224. }
  225. },
  226. e => {
  227. console.log(e);
  228. //绑定失败, 关闭当前连接
  229. _self.onOnlyCloseBLEConnection({
  230. getSuccess: () => {
  231. _self.currentItem.bRatio = false;
  232. _self.currentItem = null;
  233. _self.saveObj = null;
  234. }
  235. });
  236. }
  237. );
  238. } else if (res.code == 721) {
  239. // console.log("~~:",_self.saveObj);
  240. //是自己绑定的,添加设备
  241. if (_self.saveObj == null) return;
  242. _self.addBLEDevice(_self.saveObj);
  243. _self.ConnectionSuccess();
  244. } else if (res.code == 711 || res.code == 722) {
  245. //res.code = 711 蓝牙设备不是我们自己的
  246. //res.code = 722 蓝牙设备别人使用过
  247. uni.hideToast();
  248. uni.showModal({
  249. title: '连接失败',
  250. content: '当前手柄已经被其它账号绑定。'
  251. })
  252. //关闭当前连接
  253. _self.onOnlyCloseBLEConnection({
  254. getSuccess: () => {
  255. _self.currentItem = null;
  256. _self.saveObj = null;
  257. }
  258. });
  259. }
  260. },
  261. e => {
  262. console.log(e);
  263. }
  264. );
  265. },
  266. ConnectionSuccess() {
  267. // console.log(this.cIndex, this.BLEDeviceShowList, this.BLEConnectDevice);
  268. // uni.showToast({
  269. // icon: "none",
  270. // title: "连接设备成功!",
  271. // duration: 2000
  272. // })
  273. for (let i = 0; i < this.devicesList.length; i++) {
  274. let eq = this.devicesList[i];
  275. if (
  276. (eq.ename.indexOf("mobilePhoneBandage") > -1 && this.BLEConnectDevice.id == 0) ||
  277. (eq.ename.indexOf("hotman") > -1 && this.BLEConnectDevice.id == 1) ||
  278. (eq.ename.indexOf("BLEHandle") > -1 && this.BLEConnectDevice.id == 2)||
  279. (eq.ename.indexOf("rope") > -1 && this.BLEConnectDevice.id == 3)) {
  280. eq.bRatio = true;
  281. this.currentItem = eq;
  282. /**
  283. * 已验证的连接设置true
  284. */
  285. this.$store.state.bVerifiedConnection = true;
  286. // setTimeout(() => {
  287. // this.onCheckBondDevice();
  288. // }, 5000)
  289. }
  290. }
  291. },
  292. onCheckBondDevice() {
  293. console.log("onCheckBondDevice", this.bTestBondConnect);
  294. if (this.bTestBondConnect && this.bShow) {
  295. /**
  296. * 假如手机没有匹配,断开连接
  297. */
  298. console.log(2, this.finallyUseDevice);
  299. this.B_GetBondedDevices({
  300. deviceId: this.finallyUseDevice.deviceId,
  301. success: (bondedDevice) => {
  302. uni.hideToast();
  303. if (bondedDevice == null) {
  304. // if (plus.os.name == 'Android')
  305. {
  306. //此问题 华为手机容易出现
  307. //android手机已配对的设备 不存在,但是app 又直接连接成功了。提示,并且断开app连接
  308. //1.关闭当前连接
  309. this.onOnlyCloseBLEConnection({
  310. getSuccess: () => {
  311. this.currentItem.bRatio = false;
  312. this.currentItem = null;
  313. this.saveObj = null;
  314. }
  315. });
  316. //2.跳转蓝牙设置
  317. uni.showModal({
  318. title: '蓝牙配对失败',
  319. content: '请跳转后点击配对BGBox_2020,成功后手动跳转回哔蹦重新连接。',
  320. success: (res) => {
  321. if (res.confirm) {
  322. this.B_OpenBLESetting();
  323. }
  324. }
  325. })
  326. }
  327. }
  328. }
  329. });
  330. this.bTestBondConnect = false;
  331. } else {
  332. uni.hideToast();
  333. }
  334. },
  335. onRetryConnectBLESuccess() {
  336. this.ConnectionSuccess();
  337. },
  338. hardCallbackCloseBLE() {
  339. if (this.BLEConnectDevice == null && this.currentItem && !this.bSwitch) {
  340. this.currentItem.bRatio = false;
  341. this.currentItem = null;
  342. this.saveObj = null;
  343. }
  344. //如果限制不走重连
  345. if (this.bLimitClose) return;
  346. console.log(this.currentItem, this.oldItem);
  347. if (this.oldItem && this.currentItem && this.currentItem.id == this.oldItem.id) {
  348. // this.$store.state.bConnection = false;
  349. if (this.currentItem.bRatio) {
  350. uni.showToast({
  351. title: '设备断开连接!',
  352. icon: 'none',
  353. duration: 2000,
  354. mask: true
  355. })
  356. this.currentItem.bRatio = false;
  357. this.oldItem = null;
  358. this.currentItem = null;
  359. //断开连接
  360. this.$store.state.bVerifiedConnection = false;
  361. }
  362. } else if (this.bConnection && this.BLEConnectDevice) {
  363. //假如匹配过程中断开连接
  364. this.$store.state.BLEConnectDevice = null;
  365. this.$store.state.bConnection = false;
  366. // uni.hideToast();
  367. // console.log("//假如匹配过程中断开连接");
  368. uni.showToast({
  369. title: '连接失败,尝试重新连接。',
  370. icon: 'none',
  371. mask: true,
  372. duration: 2000
  373. })
  374. if (this.getServicesTimeout) {
  375. clearTimeout(this.getServicesTimeout);
  376. this.getServicesTimeout = null;
  377. }
  378. }
  379. },
  380. /**
  381. * 开始查找设备
  382. * */
  383. startBluetoothDeviceDiscovery() {
  384. //在页面显示的时候判断是都已经初始化完成蓝牙适配器若成功,则开始查找设备
  385. let _self = this;
  386. if (_self.bOpenBluetooth) {
  387. //1.第一步还是先进行设备搜索
  388. _self.onCanStart();
  389. } else {
  390. _self.initAdapter(() => {
  391. _self.startBluetoothDeviceDiscovery();
  392. });
  393. }
  394. },
  395. /**
  396. * 通过检测手机连接的设备进行连接,null 的话进行搜索操作
  397. */
  398. onBondedDeviceConnect() {
  399. let _self = this;
  400. //获取手机本身已连接的硬件,
  401. //示例
  402. // {
  403. // "deviceId": "C5:5C:19:04:00:30",
  404. // "name": "BGBox_202012",
  405. // "RSSI": -74,
  406. // "localName": "BGBox_20201",
  407. // "advertisServiceUUIDs": ["00001812-0000-1000-8000-00805F9B34FB", "0000FFF0-0000-1000-8000-00805F9B34FB"]
  408. // }
  409. console.log("onBoded ***************:", _self.finallyUseDevice);
  410. _self.B_GetBondedDevices({
  411. deviceId: _self.finallyUseDevice == null ? null : _self.finallyUseDevice.deviceId,
  412. success: (bondedDevice) => {
  413. console.log("bondedDevice:", bondedDevice);
  414. //获取已和蓝牙连接的设备
  415. if (bondedDevice != null) {
  416. //如果用户匹配了对应的设备,直接用对应的设备来连接
  417. let setDevice = {
  418. "deviceId": bondedDevice.address,
  419. "name": bondedDevice.name,
  420. "RSSI": -74,
  421. "localName": "",
  422. "advertisServiceUUIDs": ["00001812-0000-1000-8000-00805F9B34FB", "0000FFF0-0000-1000-8000-00805F9B34FB"]
  423. }
  424. let obj = Object.assign({}, setDevice, _self.currentItem);
  425. //finallyUserDevice 就是最后一次使用搜索到的设备信息
  426. _self.saveObj = Object.assign({}, setDevice, {
  427. id: _self.currentItem.id
  428. });
  429. uni.showToast({
  430. title: '设备连接中...',
  431. icon: 'loading',
  432. duration: 10000,
  433. mask: true
  434. })
  435. console.log("GetBondedDevices:::===", setDevice, _self.currentItem, obj);
  436. // 先直连,然后判断版本
  437. _self._onConnectDevice(obj);
  438. //getBond后发起的连接,不需要检测了
  439. _self.bTestBondConnect = false;
  440. } else {
  441. console.log("没有获取到绑定的设备");
  442. uni.showToast({
  443. title: '获取匹配设备失败',
  444. icon: 'none',
  445. mask: true
  446. })
  447. }
  448. }
  449. });
  450. },
  451. //开始搜索
  452. onCanStart() {
  453. let _self = this;
  454. uni.showToast({
  455. title: '设备连接中...',
  456. icon: 'loading',
  457. duration: 15000,
  458. mask: true
  459. })
  460. // 1.已经搜索到的,根据蓝牙回调的mac 地址判断合法性。
  461. uni.startBluetoothDevicesDiscovery({
  462. allowDuplicatesKey: true,
  463. success: res => {
  464. console.log("startBluetoothDevicesDiscovery:", res);
  465. _self.bSwitch = false;
  466. _self.onBluetoothDeviceFound();
  467. },
  468. fail: res => {
  469. console.log("搜索失败!");
  470. _self.initAdapter(() => {
  471. _self.startBluetoothDeviceDiscovery();
  472. });
  473. }
  474. });
  475. // 2.没有搜索到的,一段时间后处理。比如手机已经连接了设备,但是app 里面搜索不到,也没记录使用过的。
  476. if (_self.searchTimeOut) {
  477. clearTimeout(_self.searchTimeOut);
  478. _self.searchTimeOut = null;
  479. }
  480. _self.searchTimeOut = setTimeout(() => {
  481. //1.搜索12秒钟之后,停止搜索
  482. _self.stopBluetoothDevicesDiscovery();
  483. //搜索失败后,再检查是否和手机配对的设备连接
  484. // _self.onBondedDeviceConnect();
  485. }, 8000)
  486. },
  487. /**
  488. * 停止搜索蓝牙设备
  489. */
  490. stopBluetoothDevicesDiscovery() {
  491. uni.stopBluetoothDevicesDiscovery({
  492. success: e => {
  493. console.log('停止搜索蓝牙设备:' + e.errMsg);
  494. },
  495. fail: e => {
  496. console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
  497. }
  498. });
  499. },
  500. /**
  501. * 发现外围设备
  502. */
  503. onBluetoothDeviceFound() {
  504. let _self = this;
  505. _self.searchObj = null;
  506. uni.onBluetoothDeviceFound(res => {
  507. /**
  508. * 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
  509. */
  510. // console.log("onBluetoothDeviceFound:", res);
  511. res.devices.forEach(device => {
  512. if (device.name.indexOf('PBox') > -1 || device.name.indexOf('BGBox') > -1 || device.name.indexOf('Rope') > -1) {
  513. //如果搜索的设备名 不是对应当前设备类型,过滤
  514. // if (device.name.indexOf(_self.currentItem.deviceName) == -1) return;
  515. if (_self.currentItem.deviceName.indexOf('PBox') == -1
  516. && _self.currentItem.deviceName.indexOf('BGBox') == -1
  517. && _self.currentItem.deviceName.indexOf('Rope') == -1
  518. ) return;
  519. //寻找到对应设备时候,其余的返回
  520. if (_self.searchObj) return;
  521. _self.searchObj = device;
  522. if (_self.searchTimeOut) {
  523. clearTimeout(_self.searchTimeOut);
  524. _self.searchTimeOut = null;
  525. }
  526. //currentItem 是mode 页面选中的item
  527. let obj = Object.assign({}, device, _self.currentItem);
  528. _self.saveObj = Object.assign({}, {
  529. id: _self.currentItem.id
  530. }, device);
  531. console.log(device, "****", obj, _self.saveObj, _self.currentItem)
  532. // 先直连,然后判断版本
  533. _self._onConnectDevice(obj);
  534. _self.stopBluetoothDevicesDiscovery();
  535. }
  536. })
  537. })
  538. },
  539. onBack() {
  540. uni.navigateBack({
  541. delta: 1
  542. })
  543. },
  544. // 提示点击连接设备
  545. _onConnectDevice(item) {
  546. //servicesTimeout
  547. if (this.getServicesTimeout) {
  548. clearTimeout(this.getServicesTimeout);
  549. this.getServicesTimeout = null;
  550. }
  551. //写入指令writeMacTimeout
  552. if (this.writeMacTimeout) {
  553. clearTimeout(this.writeMacTimeout);
  554. this.writeMacTimeout = null;
  555. }
  556. //如果已经连接,加上服务不存在,直接获取服务
  557. if (this.bConnection && this.BLEGetServices && this.BLEGetServices.length == 0) {
  558. console.log("***直接获取服务*******");
  559. this.onGetBLEDeviceServices({
  560. item: item,
  561. success: (res) => {
  562. console.log("*****getBLEDeviceServices************");
  563. //连接成功了,设置旧的item
  564. this.oldItem = this.currentItem;
  565. uni.showToast({
  566. title: '正在验证设备信息...',
  567. icon: 'loading',
  568. duration: 10000,
  569. mask: true
  570. })
  571. // 初始化服务后,获取版本,判断
  572. // 停止蓝牙加速计
  573. this.writeMacTimeout = setTimeout(() => {
  574. //停止蓝牙加速计
  575. this.onWriteBLEConnectionValue({
  576. value: "4"
  577. });
  578. // 发送获取mac
  579. this.onWriteBLEConnectionValue({
  580. value: "M"
  581. });
  582. if (this.searchMac) {
  583. clearTimeout(this.searchMac);
  584. this.searchMac = null;
  585. }
  586. this.searchMac = setTimeout(() => {
  587. uni.hideToast();
  588. uni.showModal({
  589. title: '提示',
  590. content: '检测设备失败。\r\n1.检查手机蓝牙是否已连接上设备。\r\n2.再次尝试连接或者 重启手柄(Reset键位也可)后再尝试连接'
  591. })
  592. this.onOnlyCloseBLEConnection({
  593. getSuccess: () => {
  594. if (this.currentItem)
  595. this.currentItem.bRatio = false;
  596. this.currentItem = null;
  597. }
  598. });
  599. }, 6000)
  600. }, 3000)
  601. }
  602. });
  603. return;
  604. }
  605. //创建一个连接,需要对应close
  606. this.onCreateBLESuccess({
  607. item: item,
  608. getSuccess: () => {
  609. console.log("****创建一个连接*******");
  610. this.getServicesTimeout = setTimeout(() => {
  611. this.onGetBLEDeviceServices({
  612. item: item,
  613. success: (res) => {
  614. // console.log("******getBLEDeviceServices************", res);
  615. //连接成功了,设置旧的item
  616. this.oldItem = this.currentItem;
  617. uni.showToast({
  618. title: '正在验证设备信息...',
  619. icon: 'loading',
  620. duration: 10000,
  621. mask: true
  622. })
  623. // 初始化服务后,获取版本,判断
  624. // 停止蓝牙加速计
  625. this.writeMacTimeout = setTimeout(() => {
  626. //停止蓝牙加速计
  627. this.onWriteBLEConnectionValue({
  628. value: "4"
  629. });
  630. // 发送获取mac
  631. this.onWriteBLEConnectionValue({
  632. value: "M"
  633. });
  634. if (this.searchMac) {
  635. clearTimeout(this.searchMac);
  636. this.searchMac = null;
  637. }
  638. this.searchMac = setTimeout(() => {
  639. uni.hideToast();
  640. uni.showModal({
  641. title: '提示',
  642. content: '检测设备失败。\r\n1.检查手机蓝牙是否已连接上设备。\r\n2.再次尝试连接或者 重启手柄(Reset键位也可)后再尝试连接'
  643. })
  644. this.onOnlyCloseBLEConnection({
  645. getSuccess: () => {
  646. if (this.currentItem)
  647. this.currentItem.bRatio = false;
  648. this.currentItem = null;
  649. }
  650. });
  651. }, 6000)
  652. }, 3000)
  653. }
  654. });
  655. }, 2500);
  656. }
  657. })
  658. },
  659. _onRadio(item, event) {
  660. if (!item.bRatio) {
  661. //设置默认值
  662. this.bTestBondConnect = true;
  663. if (this.BLEConnectDevice) {
  664. this.onOnlyCloseBLEConnection({
  665. getSuccess: () => {
  666. if (this.currentItem)
  667. this.currentItem.bRatio = false;
  668. this.currentItem = null;
  669. this.currentItem = item;
  670. this.bSwitch = true;
  671. console.log("this.currentItem1 ==:", this.currentItem, this.bOpenBluetooth, this.BLEConnectDevice);
  672. this.startBluetoothDeviceDiscovery();
  673. }
  674. });
  675. return;
  676. }
  677. this.currentItem = null;
  678. this.currentItem = item;
  679. console.log("this.currentItem ==2:", this.currentItem, this.bOpenBluetooth, this.BLEConnectDevice);
  680. this.startBluetoothDeviceDiscovery();
  681. }
  682. },
  683. //跳转进入升级页面,
  684. onNavUpdateDevice() {
  685. // console.log('onNavUpdateDevice');
  686. // if (!this.option) {
  687. // uni.showToast({
  688. // title: 'option null',
  689. // icon: 'none'
  690. // })
  691. // return;
  692. // }
  693. //需要连接设备后,才能进入升级
  694. if (!this.currentItem || !this.currentItem.bRatio || !this.BLEConnectDevice) {
  695. uni.showToast({
  696. title: '请先连接硬件!',
  697. icon: 'none'
  698. })
  699. return;
  700. }
  701. // if (this.BLEConnectDevice.deviceType != this.option.deviceType) {
  702. // uni.showToast({
  703. // title: '设备类型不对',
  704. // icon: 'none'
  705. // })
  706. // return;
  707. // }
  708. this.bLimitClose = true;
  709. uni.navigateTo({
  710. // url: "../devices-update/devices-update?deviceType=" + this.option.deviceType
  711. url: "../devices-update/devices-update"
  712. })
  713. },
  714. onGetDevice() {
  715. uni.getBluetoothDevices({
  716. success(res) {
  717. console.log("getBluetoothDevices:", res)
  718. }
  719. })
  720. uni.getConnectedBluetoothDevices({
  721. success(res) {
  722. console.log("getConnectedBluetoothDevices:", res)
  723. }
  724. })
  725. }
  726. }
  727. }
  728. </script>
  729. <style>
  730. .hardware-border {
  731. border: 1rpx solid #9898FF;
  732. box-sizing: border-box;
  733. }
  734. </style>