Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

bluetooth.vue 15KB

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