devices-hardware.vue 34 KB

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