Jelajahi Sumber

feafeature:修改蓝牙的报错问题

zeng.chen 8 bulan lalu
induk
melakukan
fcd669edec
5 mengubah file dengan 112 tambahan dan 86 penghapusan
  1. 22 14
      devices/ble_manager.js
  2. TEMPAT SAMPAH
      images/common/icon_question.png
  3. 45 33
      pages/connectBle/connectBle.js
  4. 5 16
      pages/deviceConMode/index.js
  5. 40 23
      utils/event_bus.js

+ 22 - 14
devices/ble_manager.js

@@ -150,7 +150,6 @@ class bleManager {
                     //     deviceId: device.deviceId,
                     //     name: device.name || device.localName
                     // }));
-                    console.log('已扫描过的蓝牙设备:', res);
                     let newDevices = this.fiterDevice(res)
 
                     resolve(newDevices);
@@ -187,9 +186,9 @@ class bleManager {
             const buffer = new ArrayBuffer(data.length);
             const view = new Uint8Array(buffer);
             for (let i = 0; i < data.length; i++) {
-                view[i] = data[i];
+                view[i] = data[i].toString(16);
             }
-            console.log('开始发送数据:', data, view);
+            console.log('开始发送数据:', view);
             wx.writeBLECharacteristicValue({
                 deviceId: this.device.deviceId,
                 serviceId: this.device.serviceId,
@@ -307,13 +306,16 @@ class bleManager {
             let newDevices = this.fiterDevice(res)
             // this.devices.push(...devices);
             if (newDevices.length > 0) {
-                console.log('发现设备1:', res);
+                // console.log('发现设备1:', res);
             }
             if (callback) {
                 callback(newDevices);
             }
         });
     }
+    getMac() {
+
+    }
     ab2hex(buffer) {
         var hexArr = Array.prototype.map.call(
             new Uint8Array(buffer),
@@ -321,7 +323,7 @@ class bleManager {
                 return ('00' + bit.toString(16)).slice(-2)
             }
         )
-        return hexArr.join('');
+        return hexArr.join(':');
     }
     fiterDevice(res) {
         var devices = res.devices.filter(device => {
@@ -329,6 +331,7 @@ class bleManager {
             const localName = device.localName || '';
             let isNot = this.isNotEmpty(name) || this.isNotEmpty(localName);
             if (isNot) {
+                // console.log('是猫王设备名称:', device.advertisData, device.serviceData)
                 let mac = this.ab2hex(device.advertisData)
                 // console.log(mac)
                 device.mac = mac
@@ -351,10 +354,11 @@ class bleManager {
     }
     isNotEmpty(name) {
         let isNot = (name !== ''
-            && (name.includes("猫王") ||
-                name.includes("MW_") ||
-                name.includes("妙播") ||
-                name.includes("AirSmart") ||
+            && (name.startsWith("MW_") ||
+                name.startsWith("MW-") ||
+                name.startsWith("猫王") ||
+                name.startsWith("妙播") ||
+                name.startsWith("AirSmart") ||
                 name === "le")
         )
         // if (!isNot && name !== '') {
@@ -363,7 +367,7 @@ class bleManager {
         return isNot;
     }
     // 连接到指定设备
-    connectToDevice(device) {
+    async connectToDevice(device) {
 
         return new Promise((resolve, reject) => {
             console.log("开始连接蓝牙:", device.deviceId)
@@ -417,18 +421,22 @@ class bleManager {
             wx.getBLEDeviceServices({
                 deviceId: deviceId,
                 success: (res) => {
-                    this.device.services = res.services;
+                    // this.device.services = res.services;
                     let service_id = "";
                     for (let i = 0; i < res.services.length; i++) {
                         if (res.services[i].uuid.toUpperCase().indexOf("AB00") != -1
                             || res.services[i].uuid.toUpperCase().indexOf("FFC0") != -1
+                            || res.services[i].uuid.toUpperCase().indexOf("ae800") != -1
                         ) {
                             service_id = res.services[i].uuid;
                             break;
                         }
+                        console.log('发现服务1:', service_id);
+                        service_id = res.services[i].uuid;
+
                     }
                     this.device.serviceId = service_id;
-                    console.log('发现服务:', service_id);
+                    console.log('发现服务2:', service_id);
 
                     resolve(service_id);
                     // resolve(res.services);
@@ -458,7 +466,7 @@ class bleManager {
                 success: (res) => {
                     // this.characteristics[serviceId] = res.characteristics;
                     console.log('发现特征值2:', res);
-                    this.device.characteristics = res.characteristics;
+                    // this.device.characteristics = res.characteristics;
 
                     resolve(res.characteristics);
                 },
@@ -534,7 +542,7 @@ class bleManager {
 
     setWrite(wirte, characteristicId) {
         console.log('写入特征值:', characteristicId)
-        this.device.wirte = wirte
+        // this.device.wirte = wirte
         this.device.characteristicId = characteristicId;
     }
 

TEMPAT SAMPAH
images/common/icon_question.png


File diff ditekan karena terlalu besar
+ 45 - 33
pages/connectBle/connectBle.js


+ 5 - 16
pages/deviceConMode/index.js

@@ -18,21 +18,7 @@ Page({
     isNotruter: false,
     model: 0,
     connectDevice: {},
-    typeList: [{
-        "icon": "../../images/device/mode_wifi.png",
-        "text": "WiFi模式",
-        "type": 2,
-      },
-      {
-        "icon": "../../images/device/mode_ble.png",
-        "text": "蓝牙模式",
-        "type": 1,
-      },
-      {
-        "icon": "../../images/device/mode_g4.png",
-        "text": "4G模式",
-        "type": 4,
-      },
+    typeList: [
     ],
   },
   getModeName(curItem) {
@@ -55,6 +41,9 @@ Page({
       // let device = e.currentTarget.dataset.data
       let json = JSON.stringify(this.data.connectDevice)
       routeUtil.jumpParam(route_constant.connectBle, json)
+      // wx.navigateTo({
+      //   url: './../deviceDetail/detail',
+      // });
     } else if (type == 2) {
       wx.navigateTo({
         url: './../deviceConnect0/deviceConnect0',
@@ -101,7 +90,7 @@ Page({
 
     if (getApp().globalData.isIOS) {
 
-    } else {}
+    } else { }
   },
 
 })

+ 40 - 23
utils/event_bus.js

@@ -22,7 +22,7 @@ var isDebug = true;
  */
 function addNotification(name, selector, observer) {
     if (name && selector) {
-        if(!observer){
+        if (!observer) {
             console.log("addNotification Warning: no observer will can't remove notice");
         }
         console.log("addNotification:" + name);
@@ -58,7 +58,7 @@ function addOnceNotification(name, selector, observer) {
             }
         }
     }
-	this.addNotification(name, selector, observer)
+    this.addNotification(name, selector, observer)
 }
 
 function addNotices(newNotice) {
@@ -77,7 +77,7 @@ function addNotices(newNotice) {
 
     //     }
     // } else {
-        
+
     // }
 
     __notices.push(newNotice);
@@ -92,16 +92,16 @@ function addNotices(newNotice) {
  * observer: 移除的通知所在的Page对象
  */
 
-function removeNotification(name,observer) {
+function removeNotification(name, observer) {
     console.log("removeNotification:" + name);
-    for (var i = 0; i < __notices.length; i++){
-      var notice = __notices[i];
-      if(notice.name === name){
-        if(notice.observer === observer){
-            __notices.splice(i,1);
-            return;
+    for (var i = 0; i < __notices.length; i++) {
+        var notice = __notices[i];
+        if (notice.name === name) {
+            if (notice.observer === observer) {
+                __notices.splice(i, 1);
+                return;
+            }
         }
-      }
     }
 
 
@@ -115,21 +115,37 @@ function removeNotification(name,observer) {
  * name: 已经注册了的通知
  * info: 携带的参数
  */
+function fire(cmdEvent) {
+    let name = "cmdEventNotification";
+    let info = cmdEvent;
+    console.log("fire:" + name);
+    if (__notices.length == 0) {
+        console.log("fire error: u hadn't add any notice.");
+        return;
+    }
 
-function fire(name, info) {
+    for (var i = 0; i < __notices.length; i++) {
+        var notice = __notices[i];
+        if (notice.name === name) {
+            notice.selector(info);
+        }
+    }
+}
+
+function fireName(name, cmdEvent) {
+    let info = cmdEvent;
     console.log("fire:" + name);
-    if(__notices.length == 0){
-      console.log("fire error: u hadn't add any notice.");
-      return;
+    if (__notices.length == 0) {
+        console.log("fire error: u hadn't add any notice.");
+        return;
     }
 
-    for (var i = 0; i < __notices.length; i++){
-      var notice = __notices[i];
-      if(notice.name === name){
-        notice.selector(info);
-      }
+    for (var i = 0; i < __notices.length; i++) {
+        var notice = __notices[i];
+        if (notice.name === name) {
+            notice.selector(info);
+        }
     }
-    
 }
 
 // 用于对比两个对象是否相等
@@ -140,7 +156,7 @@ function cmp(x, y) {
     }
 
     // If they are not strictly equal, they both need to be Objects  
-    if (! (x instanceof Object) || !(y instanceof Object)) {
+    if (!(x instanceof Object) || !(y instanceof Object)) {
         return false;
     }
 
@@ -164,7 +180,7 @@ function cmp(x, y) {
             }
 
             // Numbers, Strings, Functions, Booleans must be strictly equal  
-            if (typeof(x[p]) !== "object") {
+            if (typeof (x[p]) !== "object") {
                 return false;
             }
 
@@ -188,5 +204,6 @@ module.exports = {
     addNotification: addNotification,
     removeNotification: removeNotification,
     fire: fire,
+    fireName: fireName,
     addOnceNotification: addOnceNotification
 }