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 17KB

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