Forráskód Böngészése

feature: 处理蓝牙未打开,一直请求蓝牙权限的问题

Damon 7 hónapja
szülő
commit
2f081c7b6d
4 módosított fájl, 45 hozzáadás és 50 törlés
  1. 14 5
      devices/ble_manager.js
  2. 26 26
      pages/index/index.js
  3. 4 18
      pages/index/index.wxml
  4. 1 1
      utils/lexin/util.js

+ 14 - 5
devices/ble_manager.js

@@ -4,14 +4,14 @@ class bleManager {
   constructor() {
     var that = this;
     that.hasPermission = false;
-    that.scanDevices = [];
     that.publicDevice = null;
-    that.compareList = [];
     that.connectWillDevice = null;
     that.callBackConnect = null;
     that.requestBlueTime = 0;
     ///正在执行扫描中
+    that.isAvailable = false;
     that.doStartScaning = false;
+    that.compareList = [];
     that.dissmissDevice = [];
   }
 
@@ -42,9 +42,12 @@ class bleManager {
 
   /// 监控蓝牙打开状态
   initBluetoothAdapter() {
+    var that = this;
     wx.onBluetoothAdapterStateChange(function (res) {
-      var isAvailable = res.available;
-      if (!isAvailable) {
+      that.isAvailable = res.available;
+      if (!that.isAvailable) {
+        that.compareList = [];
+        that.dissmissDevice = [];
         getCurrentPages()[0].closeBlueResetOffline(false, true);
       }
     })
@@ -88,7 +91,6 @@ class bleManager {
           for (var i = 0; i < res.devices.length; i++) {
             var temp = res.devices[i];
             //            if (temp.name == "MW-SR1(4G_WIFI)") {
-            //              console.log("gadsfqewrqewrqwerqrqr==111==" + JSON.stringify(res.devices[i]));
             //            }
             if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
               temp.mac = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
@@ -275,6 +277,13 @@ class bleManager {
   // 开始搜索蓝牙设备
   async startScan(connectWillDevice, boolean, callBackConnect) {
     var that = this;
+
+    ///限制搜索没有打开蓝牙一直询问打开蓝牙
+    var isAvailable = that.isAvailable;
+    if (!isAvailable && callBackConnect == null) {
+      return;
+    }
+
     const route_util = require('../utils/route_util');
     const route_constant = require('../utils/route_constant');
     const indexRoot = route_constant.indexRoot;

+ 26 - 26
pages/index/index.js

@@ -190,12 +190,6 @@ Page({
       var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
       app.unsubscribe(pubResponse);
     };
-
-    var deviceId = device.deviceId;
-    deviceMacId = lexin_util.getDeviceMacId(deviceId);
-    that.setData({
-      deviceMacId: deviceMacId,
-    });
     that.subscribeCurrDevice(device);
   },
 
@@ -213,13 +207,18 @@ Page({
 
     var deviceId = device.deviceId;
     var deviceMacId = lexin_util.getDeviceMacId(deviceId);
-    const obj = {
-      DstDeviceName: deviceMacId
-    };
+    that.setData({
+      deviceMacId: deviceMacId,
+    });
 
-    ///  /AIrSMArT_7cdfa1fd3af0/user/pub_response
+    /// 订阅设备
     var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
     app.subscribe(pubResponse);
+
+    /// 获取设备信息
+    const obj = {
+      DstDeviceName: deviceMacId
+    };
     app.PubMsg({
       type: "get_dev_info",
       ...obj
@@ -239,22 +238,23 @@ Page({
 
   getchannelData(clientType) {
     var that = this;
-    var deviceList = that.getDeviceList();
-    var deviceListSelect = that.getDeviceListSelect();
-    if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
-      return;
-    };
-
-    var deviceId = deviceList[deviceListSelect].deviceId;
     listByDevice({
       clientType: clientType,
       unShowLoad: true,
     }).then((res) => {
+      store.setStore("channelData", res);
       that.setData({
-        channelData: res
+        channelData: res,
       });
 
       // 接收设备当前播放状态
+      var deviceList = that.getDeviceList();
+      var deviceListSelect = that.getDeviceListSelect();
+      if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
+        return;
+      };
+
+      var deviceId = deviceList[deviceListSelect].deviceId;
       var deviceMacId = lexin_util.getDeviceMacId(deviceId);
       const obj = {
         DstDeviceName: deviceMacId
@@ -263,7 +263,6 @@ Page({
         type: "get_position",
         ...obj
       });
-      wx.setStorageSync("channelData", res);
     })
   },
 
@@ -276,7 +275,7 @@ Page({
     }
     var channelData = that.getChannelData();
     if (channelData.length > actionIndex) {
-      wx.setStorageSync("channelDeta", channelData[actionIndex]);
+      store.setStore("channelDeta", channelData[actionIndex]);
       wx.navigateTo({
         url: './../channelDetails/channelDetails'
       });
@@ -332,6 +331,7 @@ Page({
         var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
         app.unsubscribe(pubResponse);
         that.setData({
+          deviceMacId: null,
           actionIndex: null,
           deviceListSelect: null,
         });
@@ -434,7 +434,7 @@ Page({
   },
 
   ///断开当前的
-  async cancelCurrent() {
+  cancelCurrent() {
     var that = this;
     var deviceList = that.getDeviceList();
     var deviceListSelect = that.getDeviceListSelect();
@@ -443,14 +443,14 @@ Page({
     };
 
     if (deviceList[deviceListSelect].connectType == 3) {
-      await that.cancelWifi(deviceListSelect, true);
+      that.cancelWifi(deviceListSelect, true);
     } else {
-      await that.cancelBlue(deviceListSelect, true);
+      that.cancelBlue(deviceListSelect, true);
     }
   },
 
   /// 断开蓝牙连接
-  async cancelBlue(index, onlyCancel) {
+  cancelBlue(index, onlyCancel) {
     var that = this;
     if (onlyCancel) {
       return;
@@ -471,7 +471,7 @@ Page({
   },
 
   /// 断开连接wifi
-  async cancelWifi(index, onlyCancel) {
+  cancelWifi(index, onlyCancel) {
     var that = this;
     var deviceList = that.getDeviceList();
     if (index === null || deviceList.length <= index) {
@@ -729,7 +729,7 @@ Page({
     //   that.setData({
     //     bannerList: res
     //   });
-    //   wx.setStorageSync("homeBanner", res);
+    // store.setStore("homeBanner", res);
     // })
   },
 

+ 4 - 18
pages/index/index.wxml

@@ -37,7 +37,7 @@
               <view style="height: 190rpx; margin-top: 37rpx; margin-left: 32rpx; flex-direction: column; display: flex;">
                 <view style="flex: 1;">
                   <view style=" flex-direction: row; display: flex; width: 100%;">
-                    <view style="flex: 1; background-color: #6D00FF;">
+                    <view style="flex: 1;">
                       <view style="color: #333333; font-size: 28rpx;padding-right: 5rpx;">
                         {{item.devName}}
                       </view>
@@ -50,9 +50,9 @@
                   <view style="color: #666666; font-size: 24rpx;display: flex; flex-direction: row; align-items: center;">
                     <image mode="heightFix" src="{{item.connectType==3?'./../../images/device/wifi_black.png':'./../../images/device/blue_black.png'}}" style="width: 28rxp;height: 28rpx; padding-right: 10rpx; opacity: {{item.state && item.state === 'online' ? '1' : '0.7'}};"></image>
                     <view style="white-space: nowrap;"> {{item.state && item.state === "online" ? (item.connectType==3?"Wifi在线":"蓝牙在线") : "离线"}} </view>
-                    <view wx:if="{{item.ProdModel}}" style="color: #666666; font-size: 24rpx; flex:1;white-space: nowrap;text-overflow: ellipsis;">
+                    <!-- <view wx:if="{{item.ProdModel}}" style="color: #666666; font-size: 24rpx; flex:1;white-space: nowrap;text-overflow: ellipsis;">
                       ({{item.ProdModel}})
-                    </view>
+                    </view> -->
                   </view>
                 </view>
                 <view style="align-items: center; justify-content: center; vertical-align: middle;width: 280rpx;height: 64rpx;background-color: #F2F5F7;  border-radius: 52rpx; text-align: center; justify-content: center;  display: flex;" data-index="{{index}}" catchtap="deleteDevice">
@@ -192,18 +192,4 @@
         <view style="font-size: 20rpx;color: {{indexPage==2?'#A4D099':'black'}};">我的</view>
       </view>
     </view>
-  </view>
-  <!-- <view wx:if="{{newVersion}}" class="updataminiwx">
-    <view class="mode">
-      <image class="bg" src="./../../img/BG@2x.png"></image>
-      <view class="updata-view">
-        <image src="./../../img/sj.png"></image>
-        <text class="title">更新检测</text>
-        <text class="tip">检测到新版本,是否重启小程序</text>
-        <view>
-          <text bindtap="updataClone" class="onBut">取消</text>
-          <text bindtap="updata" class="twoBut">确定</text>
-        </view>
-      </view>
-    </view>
-  </view> -->
+  </view>

+ 1 - 1
utils/lexin/util.js

@@ -40,7 +40,7 @@ function getResponseByDeviceId(deviceId) {
   return getResponseByDeviceMacId(deviceMacId);
 };
 
-///通过mac地址过去请求
+///通过mac地址过去请求 /AIrSMArT_7cdfa1fd3af0/user/pub_response
 function getResponseByDeviceMacId(deviceMacId) {
   return `/${deviceMacId}/user/pub_response`;
 };