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

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