소스 검색

feature: 发送数据和接受数据处理

Damon 7 달 전
부모
커밋
7215c8a611
2개의 변경된 파일121개의 추가작업 그리고 73개의 파일을 삭제
  1. 67 71
      utils/blue_ble/manager.js
  2. 54 2
      utils/blue_ble/util.js

+ 67 - 71
utils/blue_ble/manager.js

@@ -19,16 +19,8 @@ class Manager {
     that.dissmissDevice = [];
 
     ///当前蓝牙ble连接设备的相关属性
-    that.services = {};
-    that.serviceIndex = 0;
-    that.notify = false;
-    that.write = false;
-    that.read = false;
-
     that.writeCharaterId = "";
     that.writeServiceId = "";
-    that.notifyCharaterId = "";
-    that.notifyServiceId = "";
     that.readCharaterId = "";
     that.readServiceId = "";
   }
@@ -329,99 +321,97 @@ class Manager {
   }
 
   ///初始化 接收数据 获取特征值
-  getNotifyCharaterId(device) {
-    var that = this;
+  getNotifyServices(device) {
     return new Promise((resolve, reject) => {
       var deviceId = device.deviceId;
       wx.getBLEDeviceServices({
         deviceId: deviceId,
         success: function (res) {
-          that.services = res.services;
-          that.getCharacteristics(device);
+          resolve(res.services);
+        },
+        fail: function (res) {
+          resolve(null);
         }
-      })
+      });
     });
   }
 
   ///获取设备真正的属性特征值
-  getCharacteristics(device) {
+  getCharacteristics(device, services, index, notify, write, read, succeed, fail) {
     var that = this;
     var deviceId = device.deviceId;
-    var index = that.serviceIndex;
-    var notify = that.notify;
-    var write = that.write;
-    var read = that.read;
-    var services = that.services;
     wx.getBLEDeviceCharacteristics({
       deviceId: deviceId,
       serviceId: services[index].uuid,
       success: function (res) {
-        console.log(res.characteristics)
+        var notifyCharaterId = "";
+        var notifyServiceId = "";
         for (var i = 0; i < res.characteristics.length; i++) {
           var properties = res.characteristics[i].properties;
           var charaterId = res.characteristics[i].uuid;
 
-          if (!that.notify) {
+          if (!notify) {
             if (properties.notify) {
-              that.notifyCharaterId = charaterId;
-              that.notifyServiceId = services[index].uuid;
-              that.notify = true;
+              notifyCharaterId = charaterId;
+              notifyServiceId = services[index].uuid;
+              notify = true;
             }
           }
-          if (!that.write) {
+          if (!write) {
             if (properties.write) {
               that.writeCharaterId = charaterId;
               that.writeServiceId = services[index].uuid;
-              that.write = true;
+              write = true;
             }
           }
-          if (!that.read) {
+
+          if (!read) {
             if (properties.read) {
               that.readCharaterId = charaterId;
               that.readServiceId = services[index].uuid;
-              that.read = true;
+              read = true;
             }
           }
         }
-        if (!that.notify) {
-          index++
-          that.serviceIndex = index;
-          that.notify = notify;
-          that.write = write;
-          that.read = read;
 
+        if (!notify) {
+          index++
           if (index == services.length) {
-            that.serviceIndex = 0;
-            that.notify = false;
-            that.write = false;
-            that.read = false;
+            fail();
           } else {
-            that.getCharacteristics(device);
+            that.getCharacteristics(device, services, index, notify, write, read, succeed, fail);
           }
         } else {
-          that.monitorCharacteristicValueChange(device);
+          succeed(notifyServiceId, notifyCharaterId);
         }
       }
     })
   }
 
   // 监听发送的数据
-  monitorCharacteristicValueChange(device) {
-    var that = this;
-    var deviceId = device.deviceId;
-    const hex_util = require('./../../utils/hex_util');
-    wx.notifyBLECharacteristicValueChange({
-      state: true,
-      deviceId: deviceId,
-      serviceId: that.notifyServiceId,
-      characteristicId: that.notifyCharaterId,
-      success: function (res) {
-        wx.onBLECharacteristicValueChange(function (r) {
-          var receiveText = hex_util.buf2string(r.value);
-          console.log('接收到数据文字:' + receiveText)
-        });
-      }
-    })
+  monitorCharacteristicValueChange(device, notifyServiceId, notifyCharaterId) {
+    return new Promise((resolve, reject) => {
+      var deviceId = device.deviceId;
+      wx.notifyBLECharacteristicValueChange({
+        state: true,
+        deviceId: deviceId,
+        serviceId: notifyServiceId,
+        characteristicId: notifyCharaterId,
+        success: function (res) {
+          resolve(true);
+        },
+        fail: function (res) {
+          resolve(false);
+        }
+      });
+    });
+  }
+
+  ///收取到的数据转换为文字
+  onBLECharacteristicValueChange(callback) {
+    wx.onBLECharacteristicValueChange(function (r) {
+      callback(r.value);
+    });
   }
 
   // 断开设备的连接
@@ -441,21 +431,27 @@ class Manager {
   }
 
   ///发送数据
-  sendData(text, device) {
-    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++) {
-      dataView[i] = text.charCodeAt(i)
-    }
-    wx.writeBLECharacteristicValue({
-      deviceId: deviceId,
-      serviceId: that.writeServiceId,
-      characteristicId: that.writeCharaterId,
-      value: buffer,
-      success: function (res) {},
-      fail(err) {}
+  sendData(device, text) {
+    return new Promise((resolve, reject) => {
+      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++) {
+        dataView[i] = text.charCodeAt(i)
+      }
+      wx.writeBLECharacteristicValue({
+        deviceId: deviceId,
+        serviceId: that.writeServiceId,
+        characteristicId: that.writeCharaterId,
+        value: buffer,
+        success: function (res) {
+          resolve(true);
+        },
+        fail(err) {
+          resolve(false);
+        }
+      });
     });
   }
 }

+ 54 - 2
utils/blue_ble/util.js

@@ -59,7 +59,59 @@ class BleUtil {
   }
 
   ///开始连接设备
-  async startConnect(device) {
-    
+  async startConnect(device, onChanged) {
+    var that = this;
+    await that.stopScan();
+    await that.disconnect(device);
+
+    var res = await that.manager.startConnect(device);
+    if (res === false) {
+      onChanged(false, device);
+      return;
+    }
+
+    const strings = require('./../../utils/strings');
+    var services = await that.manager.getNotifyServices(device);
+    if (strings.isEmpty(services)) {
+      onChanged(false, device);
+      return;
+    }
+
+    var index = 0;
+    var notify = false;
+    var write = false;
+    var read = false;
+    that.manager.getCharacteristics(device, services, index, notify, write, read,
+      async function (notifyServiceId, notifyCharaterId) {
+          var isTrue = that.manager.monitorCharacteristicValueChange(device, notifyServiceId, notifyCharaterId);
+          if (!isTrue) {
+            onChanged(false, device);
+            return;
+          }
+          const hex_util = require('./../../utils/hex_util');
+          that.manager.onBLECharacteristicValueChange(function (value) {
+            var receiveText = hex_util.buf2string(value);
+            console.log('接收到数据文字:' + receiveText)
+          });
+        },
+        function () {
+          onChanged(false, device);
+          return;
+        });
+  }
+
+  ///发送数据
+  async sendData(device, text) {
+    var isTrue = await that.manager.sendData(device, text);
+    ///发送数据成功
+    if (isTrue) {
+
+    }
+  }
+
+  ///断开设备连接
+  async disconnect(device) {
+    var that = this;
+    await that.manager.disconnect(device);
   }
 }