index.vue 28 KB

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