lanya2.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <view class="page">
  3. <view class="page">
  4. <view class="weui-form">
  5. <view class="weui-form__text-area">
  6. <view class="weui-form__title">xBlufi蓝牙配网{{version}}</view>
  7. <view class="weui-form__desc">设计理念:简单/快速上手</view>
  8. </view>
  9. <view class="weui-form__control-area">
  10. <view class="weui-cells__group weui-cells__group_form">
  11. <view class="weui-cells__title">需配置入网当前路由器SSID:{{ssid}}</view>
  12. <view class="weui-cells weui-cells_form">
  13. <view class="weui-cell">
  14. <picker @change="bindPickerChange" :value="index" :range="array">
  15. <view class="picker">
  16. Wi-Fi列表:{{array[index]}}
  17. </view>
  18. </picker>
  19. </view>
  20. <view class="weui-cell">
  21. <view class="weui-cell__hd"><label class="weui-label">密码:</label></view>
  22. <view class="weui-cell__bd">
  23. <input class="weui-input" :value="password" @input="bindPasswordInput" type="text"
  24. placeholder="在此输入路由器密码" />
  25. </view>
  26. </view>
  27. <view class="weui-cell">
  28. <view class="weui-cell__hd"><label class="weui-label">数据:</label></view>
  29. <view class="weui-cell__bd">
  30. <input class="weui-input" @input="bindCustomDataInput" type="text"
  31. placeholder="在此输入自定义数据" />
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. <view class="weui-form__tips-area">
  38. <label for="weuiAgree" class="weui-agree">
  39. <input id="weuiAgree" type="checkbox" class="weui-agree__checkbox" /><span
  40. class="weui-agree__text">{{connected?"蓝牙已连接":"蓝牙已断开"}}
  41. </span>
  42. </label>
  43. </view>
  44. <view class="weui-form__opr-area">
  45. <button @tap="OnClickStart" :disabled="!isInitOK" type="primary">{{isInitOK?"开始配网并发送自定义数据":"设备初始化失败或非2.4G网络"}}</button>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. var xBlufi = require("@/utils/blufi/xBlufi.js");
  53. export default {
  54. data() {
  55. return {
  56. version: '2.2',
  57. name: '',
  58. index: 0,
  59. array: [],
  60. connectedDeviceId: '',
  61. connected: true,
  62. deviceInfo: null,
  63. isInitOK: false,
  64. ssid:'',
  65. password: '',
  66. customData: ''
  67. }
  68. },
  69. onShow: function(options) {
  70. this.initWifi()
  71. },
  72. onLoad: function(options) {
  73. var that = this
  74. this.initWifi()
  75. this.name = options.name;
  76. this.connectedDeviceId = options.deviceId;
  77. xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
  78. xBlufi.notifyInitBleEsp32({
  79. deviceId: options.deviceId,
  80. })
  81. wx.showLoading({
  82. title: '设备初始化中',
  83. })
  84. },
  85. onUnload: function() {
  86. let that = this
  87. xBlufi.notifyConnectBle({
  88. isStart: false,
  89. deviceId: that.connectedDeviceId,
  90. name: that.name,
  91. });
  92. xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
  93. },
  94. methods: {
  95. funListenDeviceMsgEvent: function(options) {
  96. let that = this
  97. let ssid_arry = this.array;
  98. switch (options.type) {
  99. case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
  100. that.connected = options.result
  101. if (!options.result) {
  102. wx.showModal({
  103. title: '很抱歉提醒你!',
  104. content: '小程序与设备异常断开',
  105. showCancel: false, //是否显示取消按钮
  106. // success: function(res) {
  107. // wx.navigateBack({
  108. // url: '../search/search'
  109. // })
  110. // },
  111. })
  112. }
  113. break;
  114. case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT:
  115. wx.hideLoading();
  116. if (!options.result)
  117. wx.showModal({
  118. title: '温馨提示',
  119. content: '配网失败,请重试',
  120. showCancel: false, //是否显示取消按钮
  121. })
  122. else {
  123. if (options.data.progress == 100) {
  124. let ssid = options.data.ssid;
  125. wx.showModal({
  126. title: '温馨提示',
  127. // content: `成功连接路由器【${options.data.ssid}】`,
  128. content: `成功连接路由器【${that.ssid}】`,
  129. showCancel: false, //是否显示取消按钮
  130. success: function(res) {
  131. wx.setStorage({
  132. key: ssid,
  133. data: that.password
  134. })
  135. //判断是否为空
  136. if (that.customData)
  137. //开始发送自定义数据,此方法可以在蓝牙连接esp设备之后,随时随地调用!
  138. xBlufi.notifySendCustomData({
  139. customData: that.customData,
  140. })
  141. },
  142. })
  143. }
  144. }
  145. break;
  146. case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
  147. wx.showModal({
  148. title: '收到自定义设备数据',
  149. content: `【${options.data}】`,
  150. showCancel: false, //是否显示取消按钮
  151. })
  152. break;
  153. case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_NEAR_ROUTER_LISTS:
  154. wx.hideLoading();
  155. if ('' === options.data.SSID) break;
  156. if(!ssid_arry.includes(options.data.SSID)) ssid_arry.push(options.data.SSID)
  157. that.array = ssid_arry;
  158. that.ssid = that.array[0];
  159. that.isInitOK = true
  160. break;
  161. case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
  162. wx.hideLoading();
  163. if (options.result) {
  164. xBlufi.notifySendGetNearRouterSsid()
  165. wx.showLoading({
  166. title: '模组获取周围WiFi列表...',
  167. })
  168. } else {
  169. that.connected = false
  170. wx.showModal({
  171. title: '温馨提示',
  172. content: `设备初始化失败`,
  173. showCancel: false, //是否显示取消按钮
  174. })
  175. }
  176. break;
  177. }
  178. },
  179. OnClickStart: function() {
  180. if (!this.ssid) {
  181. wx.showToast({
  182. title: 'SSID不能为空',
  183. icon: 'none'
  184. })
  185. return
  186. }
  187. if (!this.password) {
  188. wx.showToast({
  189. title: '密码不能为空',
  190. icon: 'none'
  191. })
  192. return;
  193. }
  194. wx.showLoading({
  195. title: '正在配网',
  196. mask: true
  197. })
  198. xBlufi.notifySendRouterSsidAndPassword({
  199. ssid: this.ssid,
  200. password: this.password,
  201. success: res => {
  202. console.log(res, 'res');
  203. },
  204. fail:err=>{
  205. console.log(err, 'err');
  206. }
  207. })
  208. },
  209. stringToUtf8Bytes: function(str) {
  210. // 编码为百分比转义字符串(如 "%E6%88%91" => "我")
  211. const encoded = encodeURIComponent(str);
  212. const bytes = [];
  213. let i = 0;
  214. while (i < encoded.length) {
  215. if (encoded[i] === '%') {
  216. // 提取 % 后的两个字符作为十六进制字节(如 "%E6" => 0xE6)
  217. bytes.push(parseInt(encoded.substr(i + 1, 2), 16));
  218. i += 3; // 跳过 %XX 三个字符
  219. } else {
  220. // 普通 ASCII 字符直接转为字节(如 "A" => 0x41)
  221. bytes.push(encoded.charCodeAt(i));
  222. i++;
  223. }
  224. }
  225. return bytes;
  226. },
  227. bytesToBase64: function(bytes) {
  228. return wx.arrayBufferToBase64(new Uint8Array(bytes).buffer);
  229. },
  230. bindPasswordInput: function(e) {
  231. this.password = e.detail.value
  232. },
  233. bindCustomDataInput: function(e) {
  234. this.customData = e.detail.value
  235. },
  236. initWifi() {
  237. },
  238. bindPickerChange: function(e) {
  239. this.index = e.detail.value
  240. let password = wx.getStorageSync(this.array[e.detail.value])
  241. this.ssid = this.array[e.detail.value]
  242. this.isInitOK = true
  243. this.password = password == undefined ? "" : password
  244. },
  245. }
  246. }
  247. </script>
  248. <style scoped lang="scss">
  249. @import url(@/utils/blufi/weui.scss);
  250. page,
  251. .page {
  252. height: 100%;
  253. }
  254. button {
  255. width: 75%;
  256. }
  257. </style>