connectDevice.vue 9.7 KB

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