devices-hardware.vue 29 KB

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