index.vue 28 KB

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