connectDevice.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <view class="content">
  3. <image class="img" :src="productUrl" mode="aspectFit" />
  4. <view class="hint mt1">
  5. <image
  6. class="img1"
  7. :src="stateIcon"
  8. mode="aspectFit"
  9. v-if="showStateIcon"
  10. />
  11. <text class="text1">{{ hintText }}</text>
  12. </view>
  13. <view class="hint mt1" v-if="showLink">
  14. <image
  15. class="img2"
  16. src="../../../static/common/ic_smile.svg"
  17. mode="aspectFit"
  18. />
  19. <text class="text2">{{ productHint }}</text>
  20. </view>
  21. <view class="bottom content">
  22. <button :loading="isloading" :class="buttonClass" @click="btnClick">
  23. {{ buttonText }}
  24. </button>
  25. </view>
  26. <bt-alert v-if="showAlert" @close="btAlertClose" />
  27. </view>
  28. </template>
  29. <script>
  30. import BleConfigNet from "../../../Lib/BleConfigNet.js";
  31. import BtAlert from "../../../components/btAlert/btAlert.vue";
  32. export default {
  33. components: { BtAlert },
  34. data: () => ({
  35. /** 产品型号*/
  36. model: "MW-M3",
  37. /** 产品名称 */
  38. productName: "",
  39. /** 是否显示界面上提示的icon*/
  40. showStateIcon: true,
  41. /** 界面上提示icon的图片*/
  42. stateIcon: "../../../static/common/ic_green_success.svg",
  43. /** 界面上提示文字内容*/
  44. hintText: "正在扫描设备,请保持设备开机状态",
  45. /** 是否显示如何连接设备的提示*/
  46. showLink: true,
  47. /** 按钮的name*/
  48. btnName: "scan",
  49. /** 是否显示蓝牙开关弹窗 */
  50. showAlert: false,
  51. /** 超时扫描的定时器*/
  52. stopScanTimer: null,
  53. /** 要连接的设备*/
  54. device: null,
  55. }),
  56. computed: {
  57. buttonText() {
  58. if (this.btnName == "scaning") {
  59. return "正在搜索设备...";
  60. } else if (this.btnName == "rescan") {
  61. return "重新搜索";
  62. } else if (this.btnName == "connect") {
  63. return "连接";
  64. } else if (this.btnName == "reconnect") {
  65. return "重新连接";
  66. } else if (this.btnName == "finish") {
  67. return "完成";
  68. } else if (this.btnName == "connecting") {
  69. return "正在连接...";
  70. } else if (this.btnName == "scan") {
  71. return "搜索";
  72. } else {
  73. return this.btnName;
  74. }
  75. },
  76. buttonClass() {
  77. if (this.btnName == "scaning" || this.btnName == "connecting") {
  78. return "btn disabled";
  79. } else if (this.btnName == "finish") {
  80. return "btn finish";
  81. } else {
  82. return "btn";
  83. }
  84. },
  85. productUrl() {
  86. return `https://airsmart-photo1.oss-cn-shanghai.aliyuncs.com/wx/productModel/3X/${this.model}.png`;
  87. },
  88. productHint() {
  89. return `如何连接${this.productName}设备?`;
  90. },
  91. isloading() {
  92. return this.btnName == "scaning" || this.btnName == "connecting";
  93. },
  94. },
  95. methods: {
  96. //关闭弹窗
  97. btAlertClose() {
  98. this.showAlert = false;
  99. },
  100. /**
  101. * 按钮点击事件
  102. */
  103. btnClick() {
  104. if (this.btnName == "rescan" || this.btnName == "scan") {
  105. //搜索
  106. this.init();
  107. } else if (this.btnName == "connect" || this.btnName == "reconnect") {
  108. //连接
  109. this.btnName = "connecting";
  110. if (this.device) {
  111. BleConfigNet.connectBle(this.device.mac, this.bleConfigNetCallback);
  112. } else {
  113. that.hintText = "沒有搜索到设备";
  114. that.btnName = "rescan";
  115. that.showStateIcon = false;
  116. that.showLink = true;
  117. }
  118. } else if (this.btnName == "finish") {
  119. //完成
  120. if (this.device && this.device.uuid) {
  121. //添加设备
  122. this.$store.dispatch({
  123. type: "moduleMqtt/addDevice",
  124. clientId: `wx_${getApp().globalData.uid}`,
  125. device: {
  126. devName: this.productName,
  127. uuid: this.device.uuid,
  128. ProdModel:this.model
  129. },
  130. });
  131. //返回上上一级页面
  132. uni.navigateBack({
  133. delta: 2,
  134. });
  135. } else {
  136. uni.showToast({
  137. title: "iccid获取失败",
  138. icon: "none",
  139. });
  140. }
  141. }
  142. },
  143. //初始化蓝牙
  144. init() {
  145. const that = this;
  146. uni.openBluetoothAdapter({
  147. success: (res) => {
  148. console.log(`初始化蓝牙模块成功 ${res}`);
  149. that.startBleScan();
  150. },
  151. fail: (error) => {
  152. if (error.errCode === 10001) {
  153. //在用户蓝牙开关未开启或者手机不支持蓝牙功能的情况下
  154. this.showAlert = true;
  155. //蓝牙状态监听
  156. that.lisenBtState();
  157. } else {
  158. //其他错误
  159. uni.showToast({
  160. title: `查看手机蓝牙是否打开`,
  161. icon: "none",
  162. });
  163. console.log(error);
  164. }
  165. },
  166. });
  167. },
  168. //蓝牙开关的监听
  169. lisenBtState() {
  170. //监听蓝牙是否打开
  171. let that = this;
  172. uni.onBluetoothAdapterStateChange(function (res) {
  173. console.log(
  174. `onBluetoothAdapterStateChange available = ${res.available}`
  175. );
  176. //console.log(res);
  177. if (res.available) {
  178. //蓝牙已打开
  179. //开始扫描设备
  180. that.startBleScan();
  181. }
  182. });
  183. },
  184. //扫描蓝牙设备
  185. startBleScan() {
  186. this.showAlert = false;
  187. this.btnName = "scaning";
  188. this.hintText = "正在扫描设备,请保持设备开机状态";
  189. this.showStateIcon = false;
  190. this.showLink = true;
  191. this.device = null;
  192. const that = this;
  193. clearTimeout(this.stopScanTimer);
  194. //开始扫描
  195. uni.startBluetoothDevicesDiscovery({
  196. success: (res) => {
  197. //设置扫描的超时时间
  198. that.setStopScanTimeout();
  199. //扫描到的设备的回调
  200. uni.onBluetoothDeviceFound(function (res) {
  201. let devices = res.devices;
  202. for (var i = 0; i < devices.length; i++) {
  203. console.log(devices[i]);
  204. //todo 到时候要更具名称/型号来判断
  205. if (
  206. devices[i].name &&
  207. devices[i].name.startsWith("AIrSMArT") &&
  208. that.device == null
  209. ) {
  210. //停止扫描
  211. that.stopScan();
  212. that.device = {
  213. name: devices[i].name,
  214. mac: devices[i].deviceId,
  215. };
  216. that.showStateIcon = false;
  217. that.hintText = "以搜索到MW-M4设备";
  218. that.showLink = true;
  219. that.btnName = "connect";
  220. break;
  221. }
  222. }
  223. });
  224. },
  225. fail: (err) => {
  226. console.warn(err);
  227. that.hintText = "搜索设备失败";
  228. that.btnName = "rescan";
  229. that.showStateIcon = false;
  230. that.showLink = true;
  231. },
  232. });
  233. },
  234. //设置超时扫描的定时器
  235. setStopScanTimeout() {
  236. const that = this;
  237. //N秒之后停止扫描
  238. that.stopScanTimer = setTimeout(() => {
  239. //停止扫描
  240. uni.stopBluetoothDevicesDiscovery({
  241. complete() {
  242. if (that.device == null) {
  243. that.hintText = "沒有搜索到设备";
  244. that.btnName = "rescan";
  245. that.showStateIcon = false;
  246. that.showLink = true;
  247. }
  248. },
  249. });
  250. }, 15 * 1000);
  251. },
  252. //停止蓝牙的扫描
  253. stopScan() {
  254. uni.stopBluetoothDevicesDiscovery({});
  255. clearTimeout(this.stopScanTimer);
  256. },
  257. //ble配网的回调
  258. bleConfigNetCallback(res) {
  259. console.log(res);
  260. if (res.errCode == 0) {
  261. if (this.device) {
  262. this.device.uuid = res.iccid;
  263. }
  264. //配网成功
  265. this.hintText = "连接成功";
  266. this.btnName = "finish";
  267. this.showStateIcon = true;
  268. this.showLink = false;
  269. this.stateIcon = "../../../static/common/ic_green_success.svg";
  270. } else {
  271. this.hintText = "连接失败,请确保设备开机状态";
  272. this.btnName = "reconnect";
  273. this.showStateIcon = true;
  274. this.showLink = true;
  275. this.stateIcon = "../../../static/common/ic_red_fail.svg";
  276. }
  277. },
  278. },
  279. watch: {},
  280. // 页面周期函数--监听页面加载
  281. onLoad(options) {
  282. this.model = options.model;
  283. this.productName = options.name;
  284. if (this.productName && this.productName.length > 0) {
  285. uni.setNavigationBarTitle({
  286. title: `连接${this.productName}`,
  287. });
  288. }
  289. this.init();
  290. },
  291. // 页面周期函数--监听页面初次渲染完成
  292. onReady() {},
  293. // 页面周期函数--监听页面显示(not-nvue)
  294. onShow() {},
  295. // 页面周期函数--监听页面隐藏
  296. onHide() {},
  297. // 页面周期函数--监听页面卸载
  298. onUnload() {
  299. this.stopScan();
  300. BleConfigNet.closeBLEConnection();
  301. },
  302. };
  303. </script>
  304. <style>
  305. .content {
  306. display: flex;
  307. flex-direction: column;
  308. justify-content: flex-start;
  309. align-items: center;
  310. }
  311. .img {
  312. margin-top: 19.79vh;
  313. width: 37.33vw;
  314. height: 34.4vw;
  315. }
  316. .img1 {
  317. width: 32rpx;
  318. height: 32rpx;
  319. margin-right: 8rpx;
  320. }
  321. .img2 {
  322. width: 24rpx;
  323. height: 22rpx;
  324. margin-right: 8rpx;
  325. }
  326. .hint {
  327. height: 40rpx;
  328. display: flex;
  329. flex-direction: row;
  330. justify-content: center;
  331. align-items: center;
  332. }
  333. .mt1 {
  334. margin-top: 30rpx;
  335. }
  336. .text1 {
  337. font-size: 24rpx;
  338. font-weight: 700;
  339. color: #353535;
  340. }
  341. .text2 {
  342. font-size: 24rpx;
  343. font-weight: 500;
  344. color: #684bbe;
  345. }
  346. .btn {
  347. background-color: #353535;
  348. width: 69.33vw;
  349. border-radius: 50rpx;
  350. color: white;
  351. padding-top: 24rpx;
  352. padding-bottom: 24rpx;
  353. font-size: 32rpx;
  354. line-height: 1;
  355. }
  356. .disabled {
  357. background-color: #999999;
  358. cursor: not-allowed;
  359. }
  360. .finish {
  361. background-color: #684bbe;
  362. }
  363. .bottom {
  364. position: fixed;
  365. top: 73.8vh;
  366. left: 0;
  367. right: 0;
  368. bottom: 0;
  369. }
  370. </style>