瀏覽代碼

feature: 添加权限类

332777428@qq.com 6 月之前
父節點
當前提交
4640b2df61
共有 3 個文件被更改,包括 77 次插入55 次删除
  1. 17 34
      devices/ble_manager.js
  2. 24 21
      utils/blue_ble/manager.js
  3. 36 0
      utils/permission_util.js

+ 17 - 34
devices/ble_manager.js

@@ -56,12 +56,13 @@ class bleManager {
   ///监听搜索设备列表
   getBluetoothDevices() {
     var that = this;
+    const hex_util = require('./../utils/hex_util');
     wx.onBluetoothDeviceFound(function (res) {
       ///第一种情况
       if (res.deviceId) {
         if (that.callBackConnect != null) {
           if (that.connectWillDevice != null && res.name == that.connectWillDevice.clientType) {
-            res.mac = res.advertisData ? that.buf2hex(res.advertisData) : '';
+            res.mac = res.advertisData ? hex_util.buf2hex(res.advertisData) : '';
             // if (that.callBackConnect != null) {
             console.log("1111", res.mac);
             if (that.callBackConnect != null) {
@@ -98,7 +99,7 @@ class bleManager {
             //   console.log("2222", temp.name);
             // }
             if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
-              temp.mac = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
+              temp.mac = temp.advertisData ? hex_util.buf2hex(temp.advertisData) : '';
               temp.mac2 = that.ab2hex(temp.advertisData ?? "")
               // if (that.callBackConnect != null) {
               console.log("2222:", temp.mac, ":", temp.mac2, ":", temp.deviceId, temp);
@@ -135,12 +136,10 @@ class bleManager {
       else if (res[0]) {
         if (that.callBackConnect != null) {
           if (that.connectWillDevice != null && res[0].name == that.connectWillDevice.clientType) {
-            res[0].mac = res[0].advertisData ? that.buf2hex(res[0].advertisData) : '';
+            res[0].mac = res[0].advertisData ? hex_util.buf2hex(res[0].advertisData) : '';
             // if (that.callBackConnect != null) {
             console.log("3333", res.mac);
-
             if (that.callBackConnect != null) {
-
               that.callBackConnect(res[0]);
             }
           }
@@ -211,13 +210,14 @@ class bleManager {
   ///获取 所有搜索到的蓝牙设备
   getConnectedDevices() {
     var that = this;
+    const hex_util = require('./../utils/hex_util');
     wx.getBluetoothDevices({
       success: (res) => {
         if (that.callBackConnect != null) {
           for (var i = 0; i < res.devices.length; i++) {
             var temp = res.devices[i];
             if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
-              temp.mac = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
+              temp.mac = temp.advertisData ? hex_util.buf2hex(temp.advertisData) : '';
               if (temp.mac) {
                 console.log("搜索到的" + JSON.stringify(temp));
               }
@@ -259,29 +259,12 @@ class bleManager {
     });
   }
 
-  ///获取数据
-  buf2hex(buffer) {
-    return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
-  }
-
-  ///获取毫秒
-  getCurrentMills() {
-    var currentDate = new Date();
-    var currentTimeMillis = currentDate.getTime();
-    // return Math.floor(currentTimeMillis / 1000);
-    return currentTimeMillis;
-  }
-
-  // 等待多少秒
-  delay(ms) {
-    return new Promise(resolve => setTimeout(resolve, ms));
-  }
-
   // 开始搜索蓝牙设备
   async startScan(connectWillDevice, boolean, callBackConnect) {
     var that = this;
 
     ///限制搜索没有打开蓝牙一直询问打开蓝牙
+    const time_util = require('./../utils/time_util');
     if (!that.isAvailable) {
       if (callBackConnect == null) {
         return;
@@ -293,7 +276,7 @@ class bleManager {
       that.isAvailable = available;
       if (!available) {
         that.doStartScaning = false;
-        that.requestBlueTime = that.getCurrentMills();
+        that.requestBlueTime = time_util.getCurrentMills();
         if (boolean != null) {
           boolean(false);
         }
@@ -320,16 +303,16 @@ class bleManager {
     }
 
     that.doStartScaning = true;
-    var currentMill = that.getCurrentMills();
+    var currentMill = time_util.getCurrentMills();
     var waitMills = 0;
     var reduce = currentMill - that.requestBlueTime;
-    const delayMiliis = 6 * 1000;
-    if (reduce > 0 && reduce < delayMiliis) {
-      waitMills = delayMiliis - reduce;
+    const dMiliis = 6 * 1000;
+    if (reduce > 0 && reduce < dMiliis) {
+      waitMills = dMiliis - reduce;
     }
 
     if (waitMills > 0) {
-      await that.delay(waitMills);
+      await time_util.delayMills(waitMills);
     }
 
     if (callBackConnect == null && lastPageRoute == connectBleRoot) {
@@ -343,7 +326,7 @@ class bleManager {
       },
       fail: (err) => {
         that.doStartScaning = false;
-        that.requestBlueTime = that.getCurrentMills();
+        that.requestBlueTime = time_util.getCurrentMills();
         if (boolean != null) {
           boolean(false);
         }
@@ -353,6 +336,7 @@ class bleManager {
 
   search(connectWillDevice, boolean, callBackConnect) {
     var that = this;
+    const time_util = require('./../utils/time_util');
     wx.startBluetoothDevicesDiscovery({
       allowDuplicatesKey: true,
       success: function (res) {
@@ -360,7 +344,7 @@ class bleManager {
         that.getConnectedDevices();
 
         that.doStartScaning = false;
-        that.requestBlueTime = that.getCurrentMills();
+        that.requestBlueTime = time_util.getCurrentMills();
         if (boolean != null) {
           boolean(true);
         }
@@ -370,9 +354,8 @@ class bleManager {
       },
       fail(err) {
         console.log("开始搜索设备失败", res);
-
         that.doStartScaning = false;
-        that.requestBlueTime = that.getCurrentMills();
+        that.requestBlueTime = time_util.getCurrentMills();
         if (boolean != null) {
           boolean(false);
         }

+ 24 - 21
utils/blue_ble/manager.js

@@ -195,9 +195,23 @@ class Manager {
   async startScan(connectWillDevice, boolean, callBackConnect) {
     var that = this;
     ///限制搜索没有打开蓝牙一直询问打开蓝牙
-    var isAvailable = that.isAvailable;
-    if (!isAvailable && callBackConnect == null) {
-      return;
+    if (!that.isAvailable) {
+      if (callBackConnect == null) {
+        return;
+      }
+
+      ///做搜索蓝牙适配器权限
+      const permission_util = require('../utils/permission_util');
+      var available = await permission_util.openBluetoothAdapter();
+      that.isAvailable = available;
+      if (!available) {
+        that.doStartScaning = false;
+        that.requestBlueTime = that.getCurrentMills();
+        if (boolean != null) {
+          boolean(false);
+        }
+        return;
+      }
     }
 
     const route_util = require('../utils/route_util');
@@ -221,9 +235,9 @@ class Manager {
     var currentMill = time_util.getCurrentMills();
     var waitMills = 0;
     var reduce = currentMill - that.requestBlueTime;
-    const delayMills = 6 * 1000;
-    if (reduce > 0 && reduce < delayMills) {
-      waitMills = delayMills - reduce;
+    const dMills = 6 * 1000;
+    if (reduce > 0 && reduce < dMills) {
+      waitMills = dMills - reduce;
     }
 
     if (waitMills > 0) {
@@ -235,22 +249,11 @@ class Manager {
       return;
     }
 
-    wx.openBluetoothAdapter({
-      success: function (res) {
-        wx.stopBluetoothDevicesDiscovery({
-          success: (res) => {
-            that.search(connectWillDevice, boolean, callBackConnect);
-          },
-          fail: (err) => {
-            that.doStartScaning = false;
-            that.requestBlueTime = time_util.getCurrentMills();
-            if (boolean != null) {
-              boolean(false);
-            }
-          }
-        });
+    wx.stopBluetoothDevicesDiscovery({
+      success: (res) => {
+        that.search(connectWillDevice, boolean, callBackConnect);
       },
-      fail: function (res) {
+      fail: (err) => {
         that.doStartScaning = false;
         that.requestBlueTime = time_util.getCurrentMills();
         if (boolean != null) {

+ 36 - 0
utils/permission_util.js

@@ -0,0 +1,36 @@
+module.exports = {
+  openBluetoothAdapter: openBluetoothAdapter,
+}
+
+// 打开蓝牙权限
+function openBluetoothAdapter() {
+  return new Promise((resolve, reject) => {
+    wx.openBluetoothAdapter({
+      success: function (res) {
+        resolve(true);
+      },
+      fail: function (res) {
+        if (res.errno == 103) {
+          wx.showModal({
+            title: '提示',
+            content: '需要您的蓝牙权限才能使用此功能,是否前往设置授权?',
+            showCancel: true,
+            success: (modalRes) => {
+              if (modalRes.confirm) {
+                wx.openSetting({
+                  success: (settingRes) => {
+                    resolve(settingRes.authSetting['scope.bluetooth'] ? true : false);
+                  }
+                });
+              } else if (modalRes.cancel) {
+                resolve(false);
+              }
+            }
+          });
+        } else {
+          resolve(false);
+        }
+      }
+    })
+  });
+}