Bläddra i källkod

feature: 数据分析解析处理

332777428@qq.com 5 månader sedan
förälder
incheckning
57e5cdf1a3
4 ändrade filer med 96 tillägg och 29 borttagningar
  1. 1 4
      pages/deviceConnect4/deviceConnect4.js
  2. 3 8
      utils/blue_ble/manager.js
  3. 55 17
      utils/blue_ble/util.js
  4. 37 0
      utils/hex_util.js

+ 1 - 4
pages/deviceConnect4/deviceConnect4.js

@@ -43,16 +43,13 @@ Page({
       connectedDeviceId: deviceId,
     });
 
-    console.log("gadsfasdfadfaf==获取的数据==" + name + "===" + deviceId);
     var device = {
       "mac": name,
       "deviceId": deviceId
     };
-    console.log("gadsfasdfadfaf===000==" + JSON.stringify(device));
-
     BleUtil.getInstance().startConnect(device, function (changed) {
       console.log("gadsfasdfadfaf===111==" + changed);
-      BleUtil.getInstance().sendData(device, "哈哈哈哈哈哈");
+      BleUtil.getInstance().sendData(device, app.globalData.ssid,app.globalData.pwdData);
     });
 
   },

+ 3 - 8
utils/blue_ble/manager.js

@@ -308,17 +308,13 @@ class Manager {
   ///开始连接设备
   startConnect(device) {
     var deviceId = device.deviceId;
-    console.log("gadsfasdfadfaf===mmm==" + deviceId);
     return new Promise((resolve, reject) => {
-      console.log("gadsfasdfadfaf===oooo==" + deviceId);
       wx.createBLEConnection({
         deviceId: deviceId,
         success: function (res) {
-          console.log("gadsfasdfadfaf===qqqq==" + JSON.stringify(res));
           resolve(true);
         },
         fail: function (res) {
-          console.log("gadsfasdfadfaf===ppp==" + JSON.stringify(res));
           var errCode = res.errCode;
           var errMsg = res.errMsg;
           if (errCode == -1 && errMsg == "createBLEConnection:fail:already connect") {
@@ -334,9 +330,7 @@ class Manager {
   ///初始化 接收数据 获取特征值
   getNotifyServices(device) {
     var deviceId = device.deviceId;
-    console.log("gadsfasdfadfaf===nnn==" + deviceId);
     return new Promise((resolve, reject) => {
-      console.log("gadsfasdfadfaf===ppp==" + deviceId);
       wx.getBLEDeviceServices({
         deviceId: deviceId,
         success: function (res) {
@@ -451,8 +445,9 @@ class Manager {
       var that = this;
       var deviceId = device.deviceId;
       var buffer = new ArrayBuffer(text.length)
-      var dataView = new Uint8Array(buffer)
-      for (var i = 0; i < text.length; i++) {
+      var dataView = new Uint8Array(buffer);
+
+      for (var i = 1; i < text.length; i++) {
         dataView[i] = text.charCodeAt(i)
       }
       wx.writeBLECharacteristicValue({

+ 55 - 17
utils/blue_ble/util.js

@@ -63,7 +63,6 @@ class BleUtil {
     // await that.disconnect(device);
 
     var res = await that.manager.startConnect(device);
-    console.log("gadsfasdfadfaf===aaaa==" + JSON.stringify(res));
     if (!res) {
       onChanged(false, device);
       return;
@@ -81,30 +80,31 @@ class BleUtil {
       async function (notifyServiceId, notifyCharaterId) {
           var isTrue = that.manager.monitorCharacteristicValueChange(device, notifyServiceId, notifyCharaterId);
           if (!isTrue) {
-            console.log("gadsfasdfadfaf===ccc==");
             onChanged(false);
             return;
           }
 
+          console.log("gadsfasdfadfaf===ddd==" + JSON.stringify(isTrue));
           ///解决连接之后会有一段时间显示离线,先添加进去就不会了
-          var has = false;
-          var compareList = that.manager.getCompareList();
-          for (var j = 0; j < compareList.length; j++) {
-            if (compareList[j].deviceId == device.deviceId) {
-              has = true;
-              break;
-            }
-          }
-          if (!has) {
-            compareList.unshift(device);
-            that.manager.setCompareList(compareList);
-          }
+          // var has = false;
+          // var compareList = that.manager.getCompareList();
+          // for (var j = 0; j < compareList.length; j++) {
+          //   if (compareList[j].deviceId == device.deviceId) {
+          //     has = true;
+          //     break;
+          //   }
+          // }
+          // if (!has) {
+          //   compareList.unshift(device);
+          //   that.manager.setCompareList(compareList);
+          // }
           onChanged(true);
 
           const hex_util = require('./../../utils/hex_util');
           const bt_parse = require('./../../devices/bluetooth/bt_parse');
           that.manager.onBLECharacteristicValueChange(function (value) {
             var receiveText = hex_util.buf2string(value);
+            console.log("gadsfasdfadfaf===eeee==" + receiveText);
             console.log('接收到数据文字:' + receiveText);
 
             var buffer = new DataView(value);
@@ -116,16 +116,17 @@ class BleUtil {
           });
         },
         function () {
-          console.log("gadsfasdfadfaf===ddd==");
           onChanged(false);
           return;
         });
   }
 
   ///发送数据
-  async sendData(device, text) {
+  async sendData(device, wifiName, pwd) {
     var that = this;
-    console.log("gadsfasdfadfaf===222==" + text);
+    console.log("gadsfasdfadfaf===222==" + wifiName + "===" + pwd);
+    var text = that.sendWiFiInfo(wifiName, pwd);
+    console.log("gadsfasdfadfaf===444==" + text);
     var isTrue = await that.manager.sendData(device, text);
     console.log("gadsfasdfadfaf===333==" + isTrue);
     ///发送数据成功
@@ -134,6 +135,43 @@ class BleUtil {
     }
   }
 
+  sendWiFiInfo(wifiName, pwd) {
+    const hex_util = require('../hex_util');
+    let result = [];
+
+    // 字母*6 +
+    let wifiList = hex_util.string2ListInt(wifiName);
+
+    // 数字*3 +
+    let pwdList = hex_util.string2ListInt(pwd);
+
+    // 16进制
+    result.push(0x22);
+    result.push(parseInt(hex_util.int2Hex(wifiList.length + pwdList.length + 6), 16));
+
+    // 账号
+    result.push(0x33);
+    result.push(parseInt(hex_util.int2Hex(wifiList.length), 16));
+
+    let p = result[3] + 4;
+    let j = 0;
+    for (let i = 4; i < p; i++) {
+      result.splice(i, 0, wifiList[j++]);
+    }
+
+    // 密码
+    result.splice(p, 0, 0x44);
+    result.splice(++p, 0, parseInt(hex_util.int2Hex(pwdList.length), 16));
+    p++;
+    j = 0;
+    for (let i = p; i < p + pwdList.length; i++) {
+      result.splice(i, 0, pwdList[j++]);
+    }
+    ///[34, 22, 51, 5, 109, 117, 122, 101, 110, 68, 11, 109, 117, 122, 101, 110, 111, 102, 102, 105, 99, 101]
+    console.log("发送wifi账号密码:", result.toString());
+    return result.toString();
+  }
+
   ///断开设备连接
   async disconnect(device) {
     var that = this;

+ 37 - 0
utils/hex_util.js

@@ -1,10 +1,47 @@
 module.exports = {
+  string2ListInt: string2ListInt,
+  int2Hex: int2Hex,
   buf2hex: buf2hex,
   buf2string: buf2string,
   changeArrayByValue: changeArrayByValue,
   removeWrongArray: removeWrongArray,
 }
 
+///string转换List<int>
+function string2ListInt(str) {
+  let utf8 = [];
+  for (let i = 0; i < str.length; i++) {
+    let charcode = str.charCodeAt(i);
+    if (charcode < 0x80) {
+      utf8.push(charcode);
+    } else if (charcode < 0x800) {
+      utf8.push(0xc0 | (charcode >> 6),
+        0x80 | (charcode & 0x3f));
+    } else if (charcode < 0xd800 || charcode >= 0xe000) {
+      utf8.push(0xe0 | (charcode >> 12),
+        0x80 | ((charcode >> 6) & 0x3f),
+        0x80 | (charcode & 0x3f));
+    } else { // surrogate pair
+      i++;
+      charcode = 0x10000 + (((charcode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
+      utf8.push(0xf0 | (charcode >> 18),
+        0x80 | ((charcode >> 12) & 0x3f),
+        0x80 | ((charcode >> 6) & 0x3f),
+        0x80 | (charcode & 0x3f));
+    }
+  }
+  return utf8; // 在小程序中,这个数组可以替代 Uint8Array 使用
+}
+
+///10进制转16进制
+function int2Hex(num) {
+  // 将数字转换为16进制字符串
+  let hexStr = num.toString(16);
+  // 将16进制字符串转换回数字(这一步在微信小程序中通常不需要,除非有特殊需求)
+  let i = parseInt(hexStr, 16);
+  return i;
+}
+
 ///获取数据
 function buf2hex(buffer) {
   return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');