activateDevice.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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/BleActivateDevice.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. }
  131. },
  132. //初始化蓝牙
  133. init() {
  134. const that = this;
  135. uni.openBluetoothAdapter({
  136. success: (res) => {
  137. console.log(`初始化蓝牙模块成功 ${res}`);
  138. that.startBleScan();
  139. },
  140. fail: (error) => {
  141. if (error.errCode === 10001) {
  142. //在用户蓝牙开关未开启或者手机不支持蓝牙功能的情况下
  143. this.showAlert = true;
  144. //蓝牙状态监听
  145. that.lisenBtState();
  146. } else {
  147. //其他错误
  148. uni.showToast({
  149. title: `查看手机蓝牙是否打开`,
  150. icon: "none",
  151. });
  152. console.log(error);
  153. }
  154. },
  155. });
  156. },
  157. //蓝牙开关的监听
  158. lisenBtState() {
  159. //监听蓝牙是否打开
  160. let that = this;
  161. uni.onBluetoothAdapterStateChange(function (res) {
  162. console.log(
  163. `onBluetoothAdapterStateChange available = ${res.available}`
  164. );
  165. //console.log(res);
  166. if (res.available) {
  167. //蓝牙已打开
  168. //开始扫描设备
  169. that.startBleScan();
  170. }
  171. });
  172. },
  173. //扫描蓝牙设备
  174. startBleScan() {
  175. this.showAlert = false;
  176. this.btnName = "scaning";
  177. this.hintText = "正在扫描设备,请保持设备开机状态";
  178. this.showStateIcon = false;
  179. this.showLink = true;
  180. this.device = null;
  181. const that = this;
  182. clearTimeout(this.stopScanTimer);
  183. //开始扫描
  184. uni.startBluetoothDevicesDiscovery({
  185. success: (res) => {
  186. //设置扫描的超时时间
  187. that.setStopScanTimeout();
  188. //扫描到的设备的回调
  189. uni.onBluetoothDeviceFound(function (res) {
  190. let devices = res.devices;
  191. for (var i = 0; i < devices.length; i++) {
  192. console.log(devices[i]);
  193. if (
  194. "MW_BLE_M3" == devices[i].name ||
  195. "MW_BLE_M4" == devices[i].name
  196. ) {
  197. that.stopScan();
  198. that.device = {
  199. name: devices[i].name,
  200. mac: devices[i].deviceId,
  201. };
  202. that.showStateIcon = false;
  203. that.hintText = `以搜索到${that.productName}设备`;
  204. that.showLink = true;
  205. that.btnName = "connect";
  206. break;
  207. }
  208. }
  209. });
  210. },
  211. fail: (err) => {
  212. console.warn(err);
  213. that.hintText = "搜索设备失败";
  214. that.btnName = "rescan";
  215. that.showStateIcon = false;
  216. that.showLink = true;
  217. },
  218. });
  219. },
  220. //设置超时扫描的定时器
  221. setStopScanTimeout() {
  222. const that = this;
  223. //N秒之后停止扫描
  224. that.stopScanTimer = setTimeout(() => {
  225. //停止扫描
  226. uni.stopBluetoothDevicesDiscovery({
  227. complete() {
  228. if (that.device == null) {
  229. that.hintText = "沒有搜索到设备";
  230. that.btnName = "rescan";
  231. that.showStateIcon = false;
  232. that.showLink = true;
  233. }
  234. },
  235. });
  236. }, 30 * 1000);
  237. },
  238. //停止蓝牙的扫描
  239. stopScan() {
  240. uni.stopBluetoothDevicesDiscovery({});
  241. clearTimeout(this.stopScanTimer);
  242. },
  243. //ble配网的回调
  244. bleConfigNetCallback(res) {
  245. console.log(res);
  246. if (res.errCode == 0) {
  247. if (this.device) {
  248. this.device.uuid = res.iccid;
  249. }
  250. //配网成功
  251. this.hintText = "连接成功";
  252. this.btnName = "finish";
  253. this.showStateIcon = true;
  254. this.showLink = false;
  255. this.stateIcon = "../../../static/common/ic_green_success.svg";
  256. } else {
  257. this.hintText = "连接失败,请确保设备开机状态";
  258. this.btnName = "reconnect";
  259. this.showStateIcon = true;
  260. this.showLink = true;
  261. this.stateIcon = "../../../static/common/ic_red_fail.svg";
  262. }
  263. },
  264. },
  265. // 页面周期函数--监听页面加载
  266. onLoad(options) {
  267. this.model = options.model;
  268. this.productName = options.name;
  269. if (this.productName && this.productName.length > 0) {
  270. uni.setNavigationBarTitle({
  271. title: `连接${this.productName}`,
  272. });
  273. }
  274. this.init();
  275. },
  276. // 页面周期函数--监听页面卸载
  277. onUnload() {
  278. this.stopScan();
  279. BleConfigNet.closeBLEConnection();
  280. },
  281. };
  282. </script>
  283. <style>
  284. .content {
  285. display: flex;
  286. flex-direction: column;
  287. justify-content: flex-start;
  288. align-items: center;
  289. }
  290. .img {
  291. margin-top: 19.79vh;
  292. width: 37.33vw;
  293. height: 34.4vw;
  294. }
  295. .img1 {
  296. width: 32rpx;
  297. height: 32rpx;
  298. margin-right: 8rpx;
  299. }
  300. .img2 {
  301. width: 24rpx;
  302. height: 22rpx;
  303. margin-right: 8rpx;
  304. }
  305. .hint {
  306. height: 40rpx;
  307. display: flex;
  308. flex-direction: row;
  309. justify-content: center;
  310. align-items: center;
  311. }
  312. .mt30 {
  313. margin-top: 30rpx;
  314. }
  315. .disabled {
  316. background-color: #999999;
  317. cursor: not-allowed;
  318. }
  319. .bottom {
  320. position: fixed;
  321. top: 73.8vh;
  322. left: 0;
  323. right: 0;
  324. bottom: 0;
  325. }
  326. </style>