devices-hardware.vue 31 KB

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