Parcourir la source

feature: 处理读写数据处理先后顺序

332777428@qq.com il y a 5 mois
Parent
commit
a73d2ac040
2 fichiers modifiés avec 50 ajouts et 35 suppressions
  1. 41 9
      utils/blue_ble/manager.js
  2. 9 26
      utils/blue_ble/util.js

+ 41 - 9
utils/blue_ble/manager.js

@@ -405,14 +405,14 @@ class Manager {
   }
 
   ///获取设备真正的属性特征值
-  getCharacteristics(device, failed, getMessageCall, successed) {
+  getCharacteristics(device, failed, readSucceed) {
     var that = this;
     var deviceId = device.deviceId;
     wx.getBLEDeviceCharacteristics({
       deviceId: deviceId,
       serviceId: that.writeServiceId,
       success: function (res) {
-        that._forProcessDeal(res, failed, getMessageCall, successed);
+        that._forProcessDeal(res, failed, readSucceed);
       },
       fail: (err) => {
         failed();
@@ -420,7 +420,7 @@ class Manager {
     });
   }
 
-  async _forProcessDeal(res, failed, getMessageCall, successed) {
+  async _forProcessDeal(res, failed, readSucceed) {
     var that = this;
     const time_util = require('./../../utils/time_util');
     var characteristics = res.characteristics;
@@ -430,19 +430,51 @@ class Manager {
       return;
     }
 
+    var isWrited = false;
     for (let i = 0; i < characteristics.length; i++) {
       var charc = characteristics[i];
-      ///可以开始读取数据
-      if (charc.properties.notify) {
-        getMessageCall(charc.uuid);
-      }
-
       ///可以开始写入数据
       if (charc.properties.write || charc.properties.writeWithoutResponse) {
+        isWrited = true;
         that.writeCharaterId = charc.uuid;
-        successed();
+        break;
+      }
+    }
+
+    if (!isWrited) {
+      failed();
+      return;
+    }
+
+    var isReaded = false;
+    for (let i = 0; i < characteristics.length; i++) {
+      var charc = characteristics[i];
+      ///可以开始读取数据
+      if (charc.properties.notify) {
+        isReaded = true;
+        readSucceed(charc.uuid);
+        break;
       }
     }
+
+    if (!isReaded) {
+      failed();
+      return;
+    }
+
+    // for (let i = 0; i < characteristics.length; i++) {
+    //   var charc = characteristics[i];
+    //   ///可以开始读取数据
+    //   if (charc.properties.notify) {
+    //     writeSucceed(charc.uuid);
+    //   }
+
+    //   ///可以开始写入数据
+    //   if (charc.properties.write || charc.properties.writeWithoutResponse) {
+    //     that.writeCharaterId = charc.uuid;
+    //     readSucceed();
+    //   }
+    // }
   }
 
   // 监听特征值变化

+ 9 - 26
utils/blue_ble/util.js

@@ -90,15 +90,13 @@ class BleUtil {
       }
     }
 
-    var isRead = false;
-    var isWrited = false;
     that.manager.setWriteServiceId(serviceId);
     that.manager.getCharacteristics(device, function () {
         that.disconnect(device);
         onChanged(false);
-      }, function (notifyCharaterId) {
-        ///监听特征值变化
-        var isTrue = that.manager.monitorCharacteristicValueChange(device, serviceId, notifyCharaterId);
+      },
+      function (notifyUuid) {
+        var isTrue = that.manager.monitorCharacteristicValueChange(device, serviceId, notifyUuid);
         if (!isTrue) {
           onChanged(false);
           return;
@@ -108,27 +106,12 @@ class BleUtil {
         that.manager.onBLECharacteristicValueChange(function (value) {
           BtParse.parseTLV(value);
         });
-        isRead = true;
-        if (isWrited) {
-          console.log("gadsfasdfadfaf===7777==");
-          setTimeout(() => {
-            onChanged(true);
-          }, 300);
-        }
-      },
-      async function () {
-          isWrited = true;
-          if (isRead) {
-            console.log("gadsfasdfadfaf===8888==");
-            setTimeout(() => {
-              onChanged(true);
-            }, 300);
-          }
-        },
-        function () {
-          onChanged(false);
-          return;
-        });
+
+        console.log("gadsfasdfadfaf===7777==");
+        setTimeout(() => {
+          onChanged(true);
+        }, 300);
+      });
   }
 
   ///发送数据