connectDevice.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. },
  129. });
  130. //返回上上一级页面
  131. uni.navigateBack({
  132. delta: 2,
  133. });
  134. } else {
  135. uni.showToast({
  136. title: "iccid获取失败",
  137. icon: "none",
  138. });
  139. }
  140. }
  141. },
  142. //初始化蓝牙
  143. init() {
  144. const that = this;
  145. uni.openBluetoothAdapter({
  146. success: (res) => {
  147. console.log(`初始化蓝牙模块成功 ${res}`);
  148. that.startBleScan();
  149. },
  150. fail: (error) => {
  151. if (error.errCode === 10001) {
  152. //在用户蓝牙开关未开启或者手机不支持蓝牙功能的情况下
  153. this.showAlert = true;
  154. //蓝牙状态监听
  155. that.lisenBtState();
  156. } else {
  157. //其他错误
  158. uni.showToast({
  159. title: `查看手机蓝牙是否打开`,
  160. icon: "none",
  161. });
  162. console.log(error);
  163. }
  164. },
  165. });
  166. },
  167. //蓝牙开关的监听
  168. lisenBtState() {
  169. //监听蓝牙是否打开
  170. let that = this;
  171. uni.onBluetoothAdapterStateChange(function (res) {
  172. console.log(
  173. `onBluetoothAdapterStateChange available = ${res.available}`
  174. );
  175. //console.log(res);
  176. if (res.available) {
  177. //蓝牙已打开
  178. //开始扫描设备
  179. that.startBleScan();
  180. }
  181. });
  182. },
  183. //扫描蓝牙设备
  184. startBleScan() {
  185. this.showAlert = false;
  186. this.btnName = "scaning";
  187. this.hintText = "正在扫描设备,请保持设备开机状态";
  188. this.showStateIcon = false;
  189. this.showLink = true;
  190. this.device = null;
  191. const that = this;
  192. clearTimeout(this.stopScanTimer);
  193. //开始扫描
  194. uni.startBluetoothDevicesDiscovery({
  195. success: (res) => {
  196. //设置扫描的超时时间
  197. that.setStopScanTimeout();
  198. //扫描到的设备的回调
  199. uni.onBluetoothDeviceFound(function (res) {
  200. let devices = res.devices;
  201. for (var i = 0; i < devices.length; i++) {
  202. console.log(devices[i]);
  203. //todo 到时候要更具名称/型号来判断
  204. if (
  205. devices[i].name &&
  206. devices[i].name.startsWith("AIrSMArT") &&
  207. that.device == null
  208. ) {
  209. //停止扫描
  210. that.stopScan();
  211. that.device = {
  212. name: devices[i].name,
  213. mac: devices[i].deviceId,
  214. };
  215. that.showStateIcon = false;
  216. that.hintText = "以搜索到MW-M4设备";
  217. that.showLink = true;
  218. that.btnName = "connect";
  219. break;
  220. }
  221. }
  222. });
  223. },
  224. fail: (err) => {
  225. console.warn(err);
  226. that.hintText = "搜索设备失败";
  227. that.btnName = "rescan";
  228. that.showStateIcon = false;
  229. that.showLink = true;
  230. },
  231. });
  232. },
  233. //设置超时扫描的定时器
  234. setStopScanTimeout() {
  235. const that = this;
  236. //N秒之后停止扫描
  237. that.stopScanTimer = setTimeout(() => {
  238. //停止扫描
  239. uni.stopBluetoothDevicesDiscovery({
  240. complete() {
  241. if (that.device == null) {
  242. that.hintText = "沒有搜索到设备";
  243. that.btnName = "rescan";
  244. that.showStateIcon = false;
  245. that.showLink = true;
  246. }
  247. },
  248. });
  249. }, 15 * 1000);
  250. },
  251. //停止蓝牙的扫描
  252. stopScan() {
  253. uni.stopBluetoothDevicesDiscovery({});
  254. clearTimeout(this.stopScanTimer);
  255. },
  256. //ble配网的回调
  257. bleConfigNetCallback(res) {
  258. console.log(res);
  259. if (res.errCode == 0) {
  260. if (this.device) {
  261. this.device.uuid = res.iccid;
  262. }
  263. //配网成功
  264. this.hintText = "连接成功";
  265. this.btnName = "finish";
  266. this.showStateIcon = true;
  267. this.showLink = false;
  268. this.stateIcon = "../../../static/common/ic_green_success.svg";
  269. } else {
  270. this.hintText = "连接失败,请确保设备开机状态";
  271. this.btnName = "reconnect";
  272. this.showStateIcon = true;
  273. this.showLink = true;
  274. this.stateIcon = "../../../static/common/ic_red_fail.svg";
  275. }
  276. },
  277. },
  278. watch: {},
  279. // 页面周期函数--监听页面加载
  280. onLoad(options) {
  281. this.model = options.model;
  282. this.productName = options.name;
  283. if (this.productName && this.productName.length > 0) {
  284. uni.setNavigationBarTitle({
  285. title: `连接${this.productName}`,
  286. });
  287. }
  288. this.init();
  289. },
  290. // 页面周期函数--监听页面初次渲染完成
  291. onReady() {},
  292. // 页面周期函数--监听页面显示(not-nvue)
  293. onShow() {},
  294. // 页面周期函数--监听页面隐藏
  295. onHide() {},
  296. // 页面周期函数--监听页面卸载
  297. onUnload() {
  298. this.stopScan();
  299. BleConfigNet.closeBLEConnection();
  300. },
  301. };
  302. </script>
  303. <style>
  304. .content {
  305. display: flex;
  306. flex-direction: column;
  307. justify-content: flex-start;
  308. align-items: center;
  309. }
  310. .img {
  311. margin-top: 19.79vh;
  312. width: 37.33vw;
  313. height: 34.4vw;
  314. }
  315. .img1 {
  316. width: 32rpx;
  317. height: 32rpx;
  318. margin-right: 8rpx;
  319. }
  320. .img2 {
  321. width: 24rpx;
  322. height: 22rpx;
  323. margin-right: 8rpx;
  324. }
  325. .hint {
  326. height: 40rpx;
  327. display: flex;
  328. flex-direction: row;
  329. justify-content: center;
  330. align-items: center;
  331. }
  332. .mt1 {
  333. margin-top: 30rpx;
  334. }
  335. .text1 {
  336. font-size: 24rpx;
  337. font-weight: 700;
  338. color: #353535;
  339. }
  340. .text2 {
  341. font-size: 24rpx;
  342. font-weight: 500;
  343. color: #684bbe;
  344. }
  345. .btn {
  346. background-color: #353535;
  347. width: 69.33vw;
  348. border-radius: 50rpx;
  349. color: white;
  350. padding-top: 24rpx;
  351. padding-bottom: 24rpx;
  352. font-size: 32rpx;
  353. line-height: 1;
  354. }
  355. .disabled {
  356. background-color: #999999;
  357. cursor: not-allowed;
  358. }
  359. .finish {
  360. background-color: #684bbe;
  361. }
  362. .bottom {
  363. position: fixed;
  364. top: 73.8vh;
  365. left: 0;
  366. right: 0;
  367. bottom: 0;
  368. }
  369. </style>