You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bluetooth.vue 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. <!--设备选择 -->
  2. <template>
  3. <view class="devices">
  4. <view class="device" v-for="(item, index) in deviceList" :key="index" @click="connectDevice(item)">
  5. <image :src="`${$imgUrl}bluetooth/card1.png`" class="head" mode="aspectFill"></image>
  6. <view class="center">
  7. <view class="name">{{ item._name }}</view>
  8. <view class="desc">编号 {{ item.name }}</view>
  9. </view>
  10. <image :src="item.selected ? selectedUrl : unSelectedUrl" class="icon" mode="aspectFill"></image>
  11. </view>
  12. <view class="hint">
  13. <view class="orange-txt as-layout-horizontal as-gravity-center-start">
  14. <image :src="`${$imgUrl}common/icon-hint.png`" mode="aspectFill"></image>
  15. <view>温馨提示:</view>
  16. </view>
  17. <view class="grey-txt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  18. 指令执行过程中,请勿关闭蓝牙,勿将手机远离电子标签,以免导致写卡、写签失败。</view>
  19. </view>
  20. <view class="btn">
  21. <submit-button title="点击重新搜索蓝牙" @submit="load"></submit-button>
  22. </view>
  23. </view>
  24. </template>
  25. <script setup lang="ts">
  26. import { ref, reactive } from "vue";
  27. import { onLoad, onReady } from "@dcloudio/uni-app";
  28. import { fileURL } from "@/datas/fileURL.js";
  29. const imgURL = `${fileURL}image/`;
  30. const selectedUrl = "common/selected.png`";
  31. const unSelectedUrl = "common/unselected.png`";
  32. const jyApi = require("../../static/etc/JYAPI/GenvictBleUtil.js");
  33. const jlQZApi = require("../../static/etc/JLQZAPI/JLObuSDK.js");
  34. const atApi = require("../../static/etc/ATAPI/ArtcBleUtil.js");
  35. const wqApi = require("../../static/etc/WQAPI/WCDObuSdk.js");
  36. const cgApi = require("../../static/etc/CGAPI/cguBle.js");
  37. const tools = require("../../static/etcUtil/tools.js");
  38. const datas = require("../../static/etcUtil/datas.js");
  39. const bluetoothUtil = require("../../static/etcUtil/index.js");
  40. let jlApi = require("../../static/etc/JLAPI/JLObuSDK.js"); //聚力SDK
  41. let wjApi = require("../../static/etc/WJAPI/wjBleAPI");
  42. let tdApi = require("../../static/etc/TDAPI/TDRObuSDK.js");
  43. let zzApi = require("../../static/etc/WJAPI/wjBleAPI.js");
  44. let jtApi = require("../../static/etc/JTAPI/BleUtil.js");
  45. // #ifdef MP-ALIPAY
  46. // 这部分代码将仅在支付宝小程序平台下执行
  47. jlApi = require("../../static/etc/JLObuSDK/JLObuSDK.js");
  48. wjApi = require("../../static/etc/wjBle/WJBleAPI");
  49. zzApi = require("../../static/etc/wjBle/WJBleAPI");
  50. tdApi = require("../../static/etc/TDRAPI/TDRObuSDK.js");
  51. jtApi = require("../../static/etc/JTAPIS/BleUtil.js");
  52. // 执行支付宝小程序的特定功能
  53. // #endif
  54. const state = reactive({
  55. fee: "",
  56. id: "",
  57. cardId: "",
  58. showPopup: false, //显示激活成功提示
  59. curDeviceId: "", //当前选中的设备ID
  60. deviceList: [
  61. //设备列表
  62. {
  63. id: "1",
  64. image: imgURL + "bluetooth/card1.png`",
  65. name: "聚力",
  66. number: "235564444558855",
  67. },
  68. ],
  69. connectSuccess: 1
  70. });
  71. const deviceList = ref([]);
  72. const connectPrefixName = ref(null);
  73. const routeType = ref(null); //来源 1激活 2圈存 3信息重写 4信息读取
  74. interface DeviceType {
  75. name : string; //设备名称
  76. deviceId : string; //uuid
  77. prefixName : string; //前缀名称
  78. selected : boolean; //判断点击次数
  79. _name : string; //中文名称
  80. }
  81. let device : DeviceType = reactive({
  82. name: "", //设备名称
  83. deviceId: "", //uuid
  84. prefixName: "", //前缀名称
  85. selected: false, //判断点击次数
  86. _name: "", //中文名称
  87. });
  88. onReady(() => {
  89. load();
  90. });
  91. onLoad((option) => {
  92. routeType.value = option.routeType ? option.routeType : "1";
  93. state.cardId = option.cardId;
  94. if (option.fee) {
  95. state.fee = option.fee;
  96. }
  97. if (option.id) {
  98. state.id = option.id;
  99. }
  100. console.log("传过来的参数", option)
  101. });
  102. /*
  103. * 蓝牙初始化
  104. */
  105. const load = () => {
  106. deviceList.value = [];
  107. console.log("****************蓝牙getsetting******************");
  108. uni.getSetting({
  109. success(res) {
  110. console.log("****************成功******************");
  111. console.log("scopebluetooth:" + res.authSetting["scope.bluetooth"]);
  112. console.log("成功结果:" + JSON.stringify(res));
  113. if (res.authSetting["scope.bluetooth"] == undefined) {
  114. //没有授权使用蓝牙功能,提示用户授权使用
  115. // #ifdef MP-WEIXIN
  116. uni.authorize({
  117. scope: "scope.bluetooth",
  118. success() {
  119. openBluetooth();
  120. },
  121. fail(err) {
  122. console.log('蓝牙授权失败', err)
  123. uni.showToast({
  124. title: "蓝牙授权失败",
  125. icon: "error",
  126. duration: 1500,
  127. });
  128. }
  129. });
  130. // #endif
  131. // #ifdef MP-ALIPAY
  132. openBluetooth();
  133. // #endif
  134. } else if (!res.authSetting["scope.bluetooth"]) {
  135. //false
  136. uni.showModal({
  137. title: "是否授权读取蓝牙",
  138. content: "需要获取你的蓝牙,请确认授权,否则无法获取蓝牙",
  139. success: function (mres) {
  140. if (mres.confirm) {
  141. uni.openSetting({
  142. success(authData) {
  143. if (authData.authSetting["scope.bluetooth"] == true) {
  144. tools.showLoadingAlert("扫描蓝牙中");
  145. openBluetooth();
  146. } else {
  147. uni.closeBluetoothAdapter();
  148. uni.showToast({
  149. title: "蓝牙授权失败",
  150. icon: "error",
  151. duration: 1500,
  152. });
  153. }
  154. },
  155. });
  156. } else if (mres.cancel) {
  157. uni.showToast({
  158. title: "蓝牙授权失败",
  159. icon: "error",
  160. duration: 1000,
  161. });
  162. }
  163. },
  164. });
  165. } else {
  166. tools.showLoadingAlert("扫描蓝牙中");
  167. openBluetooth();
  168. }
  169. },
  170. fail(res) {
  171. console.log("****************失败******************");
  172. console.log("失败结果:" + JSON.stringify(res));
  173. },
  174. });
  175. };
  176. /*
  177. * 打开蓝牙
  178. */
  179. const openBluetooth = () => {
  180. tools.hideLoadingAlert();
  181. let foundDevices = []; //扫描到的蓝牙列表
  182. uni.closeBluetoothAdapter(); //先关闭蓝牙
  183. console.log('执行断开蓝牙方法')
  184. //打开蓝牙
  185. uni.openBluetoothAdapter({
  186. success: function (item : any) {
  187. console.log('初始化蓝牙模块', item)
  188. tools.showLoadingAlert("扫描蓝牙中...");
  189. //开始搜索附近的蓝牙设备
  190. uni.startBluetoothDevicesDiscovery({
  191. success: function (res) {
  192. console.log('开始搜寻附近的蓝牙外围设备', res);
  193. //监听搜索到新设备的事件
  194. uni.onBluetoothDeviceFound(function (res) {
  195. console.log('监听寻找到新设备的事件', res, res.devices);
  196. for (let i = 0; i < res.devices.length; i++) {
  197. let name = res.devices[i]["name"];
  198. let prefixName = "";
  199. let deviceId = res.devices[i]["deviceId"];
  200. console.log("res.devices[i]", res.devices[i]["name"]);
  201. if (name != "" && name != undefined && name != "undefined") {
  202. if (name.indexOf("G-WJ") != -1 || name.indexOf("ETC") != -1 || name.indexOf("G-JL") != -1) {
  203. //前装设备
  204. prefixName = "ETC";
  205. } else {
  206. //聚力临时设备
  207. if (name.indexOf("5201121") != -1) {
  208. prefixName = "JL";
  209. } else {
  210. prefixName = name.substring(0, 2);
  211. }
  212. }
  213. prefixName = prefixName.toUpperCase();
  214. if (
  215. prefixName == "WJ" ||
  216. prefixName == "JL" ||
  217. prefixName == "JY" ||
  218. prefixName == "AT" ||
  219. prefixName == "JT" ||
  220. prefixName == "WQ" ||
  221. prefixName == "CG" ||
  222. prefixName == "TD" ||
  223. prefixName == "ZZ" ||
  224. prefixName == "ETC"
  225. ) {
  226. // //隐藏加载框
  227. tools.hideLoadingAlert();
  228. device.name = name;
  229. device.deviceId = deviceId;
  230. device.prefixName = prefixName;
  231. device.selected = false; //防止重复点击
  232. let _name = "";
  233. switch (prefixName) {
  234. case "WJ":
  235. _name = "万集";
  236. break;
  237. case "JL":
  238. _name = "聚利";
  239. break;
  240. case "JY":
  241. _name = "金溢";
  242. break;
  243. case "AT":
  244. _name = "埃特斯";
  245. break;
  246. case "JT":
  247. _name = "建投";
  248. break;
  249. case "WQ":
  250. _name = "握奇";
  251. break;
  252. case "CG":
  253. _name = "成谷";
  254. break;
  255. case "TD":
  256. _name = "天地融";
  257. break;
  258. case "ZZ":
  259. _name = "智载";
  260. break;
  261. case "ETC":
  262. _name = "前装";
  263. break;
  264. default:
  265. _name = "未知";
  266. break;
  267. }
  268. device._name = _name;
  269. if (deviceList.value.length == 0) {
  270. // #ifdef MP-ALIPAY
  271. if (device._name == '聚利' || device._name == '万集' || device._name == '天地融' || device._name == '建投') {
  272. foundDevices.push(device);
  273. }
  274. // #endif
  275. // #ifdef MP-WEIXIN
  276. foundDevices.push(device);
  277. // #endif
  278. } else {
  279. let isHave = false;
  280. for (let j = 0; j < foundDevices.length; j++) {
  281. if (name == foundDevices[j].deviceId) {
  282. isHave = true;
  283. break;
  284. }
  285. }
  286. if (!isHave) {
  287. foundDevices.push(device);
  288. }
  289. }
  290. deviceList.value = foundDevices;
  291. foundDevices = [];
  292. // console.log(foundDevices);
  293. }
  294. }
  295. }
  296. });
  297. },
  298. fail: function (res) {
  299. console.log(res);
  300. tools.hideLoadingAlert();
  301. },
  302. });
  303. },
  304. fail: function (res) {
  305. console.log(res);
  306. alertF("手机蓝牙未打开或不支持蓝牙");
  307. },
  308. });
  309. };
  310. /**
  311. * 连接蓝牙
  312. */
  313. const connectDevice = (e) => {
  314. console.log(e);
  315. let item = e;
  316. //停止扫描蓝牙
  317. console.info("停止搜寻附近的蓝牙外围设备");
  318. uni.stopBluetoothDevicesDiscovery({
  319. success: function (res) {
  320. console.log(device);
  321. if (item.selected == false) {
  322. console.log("第一次点击了");
  323. item.selected = true;
  324. setTimeout(function () {
  325. if (device.selected == undefined) {
  326. console.info("selected is undefined");
  327. linkFail(); //未找到设备, 请重新搜索
  328. return;
  329. }
  330. item.selected = false;
  331. }, 4000);
  332. } else {
  333. console.log("第二次点击了");
  334. return;
  335. }
  336. if (
  337. item.prefixName == undefined ||
  338. item.prefixName == "undefined" ||
  339. item.prefixName == ""
  340. ) {
  341. console.info("device.prefixName is undefined");
  342. linkFail(); //未找到设备, 请重新搜索
  343. return;
  344. }
  345. let prefixName = item.prefixName;
  346. connectPrefixName.value = item.prefixName;
  347. console.log(item);
  348. // datas.setData("connectPrefixName", connectPrefixName.value);
  349. // //断开蓝牙
  350. // bluetoothUtil.disconnectDevice();
  351. tools.showLoadingAlert("蓝牙连接中");
  352. console.info("连接的是" + prefixName, device);
  353. switch (prefixName) {
  354. case "WJ":
  355. wjApi.connectDevice(
  356. device,
  357. function (res) {
  358. connectSuccess(res);
  359. },
  360. function (res) {
  361. listenStatus(res);
  362. }
  363. );
  364. break;
  365. case "JL":
  366. jlApi.connectDevice(
  367. device,
  368. function (res) {
  369. console.log('设备链接1', res)
  370. connectSuccess(res);
  371. },
  372. function (res) {
  373. console.log('设备链接2', res)
  374. listenStatus(res);
  375. }
  376. );
  377. break;
  378. case "ETC":
  379. jlQZApi.connectDevice(
  380. device,
  381. function (res) {
  382. preDevice(res);
  383. },
  384. function (res) {
  385. listenStatus(res);
  386. }
  387. );
  388. break;
  389. case "JY":
  390. jyApi.connectDevice(
  391. device,
  392. function (res) {
  393. connectSuccess(res);
  394. },
  395. function (res) {
  396. listenStatus(res);
  397. }
  398. );
  399. break;
  400. case "AT":
  401. atApi.connectDevice(
  402. device,
  403. function (res) {
  404. connectSuccess(res);
  405. },
  406. function (res) {
  407. listenStatus(res);
  408. }
  409. );
  410. break;
  411. case "JT":
  412. console.info("连接的是=====123" + prefixName, device);
  413. jtApi.connectDevice(
  414. device,
  415. function (res) {
  416. console.log('设备链接1', res)
  417. connectSuccess(res);
  418. },
  419. function (res) {
  420. console.log('设备链接2', res)
  421. listenStatus(res);
  422. }
  423. );
  424. break;
  425. case "WQ":
  426. wqApi.connectDevice(
  427. device,
  428. function (res) {
  429. connectSuccess(res);
  430. },
  431. function (res) {
  432. listenStatus(res);
  433. }
  434. );
  435. break;
  436. case "CG":
  437. cgApi.connectDevice(
  438. device,
  439. function (res) {
  440. connectSuccess(res);
  441. },
  442. function (res) {
  443. listenStatus(res);
  444. }
  445. );
  446. break;
  447. case "TD":
  448. tdApi.connectDevice(device, function (res) {
  449. connectSuccess(res)
  450. }, function (res) {
  451. listenStatus(res);
  452. });
  453. break;
  454. case "ZZ":
  455. zzApi.connectDevice(
  456. device,
  457. function (res) {
  458. connectSuccess(res);
  459. },
  460. function (res) {
  461. listenStatus(res);
  462. }
  463. );
  464. break;
  465. default: //未找到设备, 请重新搜索
  466. linkFail();
  467. break;
  468. }
  469. },
  470. fail: function (res) {
  471. console.log(res);
  472. linkFail(); //未找到设备, 请重新搜索
  473. },
  474. });
  475. };
  476. /**
  477. * 未找到设备, 请重新搜索
  478. */
  479. const linkFail = () => {
  480. datas.setData("bluLinkStatus", false);
  481. datas.setData("connectPrefixName", "");
  482. tools.showModalAlert("未找到设备, 请重新搜索", function successFunc() {
  483. load();
  484. });
  485. };
  486. /**
  487. * 连接成功
  488. */
  489. const connectSuccess = (res) => {
  490. console.log("连接回调函数func1");
  491. console.log(res);
  492. if (res.code == 0) {
  493. console.log("连接成功");
  494. tools.hideLoadingAlert(); //关闭加载框
  495. datas.setData("bluLinkStatus", true);
  496. datas.setData("connectPrefixName", connectPrefixName.value);
  497. oks();
  498. } else {
  499. alertF(res.msg);
  500. }
  501. };
  502. function oks() {
  503. //routeType 1.激活(订单来) 2.圈存 (/pages/recharge/recharge来)3.ping码解锁 4.信息读取 5从哪里来回哪里去监听bluetoothLink
  504. if (routeType.value == "1") {
  505. uni.navigateTo({
  506. url: "/subpackage/after-sale/activation/activate",
  507. });
  508. } else if (routeType.value == "2") {
  509. // #ifdef MP-ALIPAY
  510. // uni.navigateTo({
  511. // url: `/pages/recharge/recharge?connectSuccess=1&&cardId=${state.cardId}&&fee=${state.fee}`,
  512. // });
  513. // #endif
  514. uni.navigateTo({
  515. url: `/pages/recharge/recharge-weixin?connectSuccess=${state.connectSuccess}`,
  516. });
  517. } else if (routeType.value == "3") {
  518. uni.navigateTo({
  519. url: "/subpackage/after-sale/pin-code-deblocking/pin-code-confirm?state=true&id=" + state.id,
  520. });
  521. } else if (routeType.value == "4") {
  522. uni.navigateTo({
  523. url: "/subpackage/after-sale/deviceInfo/deviceInfo",
  524. });
  525. } else if (routeType.value == "5") {
  526. // #ifdef MP-ALIPAY
  527. uni.navigateTo({
  528. url: "/subpackage/after-sale/activation-once-again/activation-once-again?state=true&id=" + state.id,
  529. });
  530. // #endif
  531. // #ifdef MP-WEIXIN
  532. uni.$emit('bluetoothLink', { status: true })
  533. uni.navigateBack({
  534. delta: 1
  535. })
  536. // #endif
  537. } else if (routeType.value == "6") {
  538. uni.navigateTo({
  539. url: "/subpackage/after-sale/activation-once-again/activation-once-again?state=true&id=" + state.id
  540. });
  541. } else {
  542. return;
  543. }
  544. }
  545. /**
  546. * 前装设备
  547. */
  548. const preDevice = (res) => {
  549. if (res.code == 0) {
  550. console.log("连接成功");
  551. datas.setData("bluLinkStatus", true);
  552. datas.setData("connectPrefixName", connectPrefixName.value);
  553. // tools.toUrl(route.preActivateInfo);//跳转 前装设备
  554. } else {
  555. alertF(res.msg);
  556. console.log(res.msg);
  557. }
  558. };
  559. /**
  560. * 监听蓝牙状态
  561. */
  562. const listenStatus = (res) => {
  563. console.log("时时监听蓝牙状态func2");
  564. console.log(res);
  565. if (res.code == 0) {
  566. datas.setData("bluLinkStatus", true);
  567. datas.setData("connectPrefixName", connectPrefixName.value);
  568. // oks();
  569. } else {
  570. //断开蓝牙
  571. bluetoothUtil.disconnectDevice();
  572. datas.setData("bluLinkStatus", false);
  573. datas.setData("connectPrefixName", "");
  574. tools.showToastAlert("蓝牙已断开");
  575. // #ifdef MP-ALIPAY
  576. my.closeBluetoothAdapter({
  577. success(res) {
  578. // 关闭蓝牙适配器成功
  579. },
  580. fail(err) {
  581. // 处理关闭蓝牙适配器失败的情况
  582. }
  583. })
  584. // #endif
  585. }
  586. };
  587. /**
  588. * 提示加关蓝牙
  589. */
  590. const alertF = (msg : string) => {
  591. //隐藏加载框
  592. tools.hideLoadingAlert();
  593. //断开蓝牙
  594. bluetoothUtil.disconnectDevice();
  595. //提示对话框
  596. tools.showModalAlert(msg);
  597. };
  598. </script>
  599. <style>
  600. page {
  601. background-color: #f3f3f3;
  602. }
  603. :deep(.u-mode-center-box) {
  604. border-radius: 20rpx;
  605. }
  606. </style>
  607. <style lang="scss" scoped>
  608. .devices {
  609. .device {
  610. width: calc(100% - 30rpx);
  611. margin-left: 30rpx;
  612. display: flex;
  613. flex-direction: row;
  614. align-items: center;
  615. margin-top: 30rpx;
  616. background: white;
  617. padding: 25rpx 20rpx;
  618. border-top-left-radius: 20rpx;
  619. border-bottom-left-radius: 20rpx;
  620. box-shadow: 0px 10px 10rpx 10rpx rgba(223, 223, 223, 0.3);
  621. .head {
  622. width: 120rpx;
  623. height: 120rpx;
  624. border-radius: 10rpx;
  625. background-color: #f3f3f3;
  626. }
  627. .center {
  628. flex: 1;
  629. margin-left: 30rpx;
  630. margin-right: 30rpx;
  631. .name {
  632. font-size: 30rpx;
  633. color: #333333;
  634. }
  635. .desc {
  636. font-size: 26rpx;
  637. color: #666666;
  638. margin-top: 25rpx;
  639. }
  640. }
  641. .icon {
  642. width: 43rpx;
  643. height: 43rpx;
  644. margin-right: 10rpx;
  645. }
  646. }
  647. .hint {
  648. margin: 60rpx 30rpx 0px;
  649. .orange-txt {
  650. font-size: 26rpx;
  651. color: #ff8000;
  652. image {
  653. width: 30rpx;
  654. height: 30rpx;
  655. margin-right: 10rpx;
  656. }
  657. }
  658. .grey-txt {
  659. font-size: 26rpx;
  660. color: #666666;
  661. line-height: 40rpx;
  662. margin-top: 16rpx;
  663. }
  664. }
  665. .btn {
  666. margin: 70rpx 40rpx;
  667. }
  668. }
  669. </style>