connectDevice.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. } else if (
  234. "MW_BLE_M3" == devices[i].name ||
  235. "MW_BLE_M4" == devices[i].name
  236. ) {
  237. //devices[i].advertisData
  238. let iccid = Buffer.from(devices[i].advertisData).toString()
  239. let uuid = `AIrSMArT_${iccid.substring(8,iccid.length)}`
  240. console.warn(iccid);
  241. that.device = {
  242. name: devices[i].name,
  243. mac: iccid,
  244. };
  245. if (that.device) {
  246. that.device.uuid = uuid;
  247. }
  248. //配网成功
  249. that.hintText = "连接成功";
  250. that.btnName = "finish";
  251. that.showStateIcon = true;
  252. that.showLink = false;
  253. that.stateIcon = "../../../static/common/ic_green_success.svg";
  254. that.stopScan()
  255. break;
  256. }
  257. }
  258. });
  259. },
  260. fail: (err) => {
  261. console.warn(err);
  262. that.hintText = "搜索设备失败";
  263. that.btnName = "rescan";
  264. that.showStateIcon = false;
  265. that.showLink = true;
  266. },
  267. });
  268. },
  269. //设置超时扫描的定时器
  270. setStopScanTimeout() {
  271. const that = this;
  272. //N秒之后停止扫描
  273. that.stopScanTimer = setTimeout(() => {
  274. //停止扫描
  275. uni.stopBluetoothDevicesDiscovery({
  276. complete() {
  277. if (that.device == null) {
  278. that.hintText = "沒有搜索到设备";
  279. that.btnName = "rescan";
  280. that.showStateIcon = false;
  281. that.showLink = true;
  282. }
  283. },
  284. });
  285. }, 15 * 1000);
  286. },
  287. //停止蓝牙的扫描
  288. stopScan() {
  289. uni.stopBluetoothDevicesDiscovery({});
  290. clearTimeout(this.stopScanTimer);
  291. },
  292. //ble配网的回调
  293. bleConfigNetCallback(res) {
  294. console.log(res);
  295. if (res.errCode == 0) {
  296. if (this.device) {
  297. this.device.uuid = res.iccid;
  298. }
  299. //配网成功
  300. this.hintText = "连接成功";
  301. this.btnName = "finish";
  302. this.showStateIcon = true;
  303. this.showLink = false;
  304. this.stateIcon = "../../../static/common/ic_green_success.svg";
  305. } else {
  306. this.hintText = "连接失败,请确保设备开机状态";
  307. this.btnName = "reconnect";
  308. this.showStateIcon = true;
  309. this.showLink = true;
  310. this.stateIcon = "../../../static/common/ic_red_fail.svg";
  311. }
  312. },
  313. },
  314. watch: {},
  315. // 页面周期函数--监听页面加载
  316. onLoad(options) {
  317. this.model = options.model;
  318. this.productName = options.name;
  319. if (this.productName && this.productName.length > 0) {
  320. uni.setNavigationBarTitle({
  321. title: `连接${this.productName}`,
  322. });
  323. }
  324. this.init();
  325. },
  326. // 页面周期函数--监听页面初次渲染完成
  327. onReady() {},
  328. // 页面周期函数--监听页面显示(not-nvue)
  329. onShow() {},
  330. // 页面周期函数--监听页面隐藏
  331. onHide() {},
  332. // 页面周期函数--监听页面卸载
  333. onUnload() {
  334. this.stopScan();
  335. BleConfigNet.closeBLEConnection();
  336. },
  337. };
  338. </script>
  339. <style>
  340. .content {
  341. display: flex;
  342. flex-direction: column;
  343. justify-content: flex-start;
  344. align-items: center;
  345. }
  346. .img {
  347. margin-top: 19.79vh;
  348. width: 37.33vw;
  349. height: 34.4vw;
  350. }
  351. .img1 {
  352. width: 32rpx;
  353. height: 32rpx;
  354. margin-right: 8rpx;
  355. }
  356. .img2 {
  357. width: 24rpx;
  358. height: 22rpx;
  359. margin-right: 8rpx;
  360. }
  361. .hint {
  362. height: 40rpx;
  363. display: flex;
  364. flex-direction: row;
  365. justify-content: center;
  366. align-items: center;
  367. }
  368. .mt30 {
  369. margin-top: 30rpx;
  370. }
  371. .disabled {
  372. background-color: #999999;
  373. cursor: not-allowed;
  374. }
  375. .bottom {
  376. position: fixed;
  377. top: 73.8vh;
  378. left: 0;
  379. right: 0;
  380. bottom: 0;
  381. }
  382. </style>