Browse Source

feature: 解决报错的问题

Damon 8 months ago
parent
commit
7d393d889c
5 changed files with 175 additions and 144 deletions
  1. 49 30
      devices/ble_manager.js
  2. 6 5
      devices/bt_helper.js
  3. 20 13
      pages/connectBle/connectBle.js
  4. 0 1
      pages/index/index.js
  5. 100 95
      pages/index/index.wxml

+ 49 - 30
devices/ble_manager.js

@@ -31,11 +31,11 @@ class bleManager {
         }
       });
       wx.onBluetoothAdapterStateChange(function (res) {
-        // console.log('adapterState changed, now is', res)
+        console.log('adapterState changed, now is', res)
         // if (callback) {
         //   callback(res.available ?? false);
         // }
-        that.isAvailable = res.available ?? false
+        that.isAvailable = res.available;
       })
     });
   }
@@ -71,14 +71,16 @@ class bleManager {
 
     // services: ["ffc0", "ab00", "ffe5"],
     wx.startBluetoothDevicesDiscovery({
-      allowDuplicatesKey: false,
+      allowDuplicatesKey: true,
       success: (res) => {
         if (res) {
           wx.onBluetoothDeviceFound((devicesRes) => {
             for (var i = 0; i < devicesRes.devices.length; i++) {
-              if (devicesRes.devices[i].name === getApp().globalData.connectWillDevice.clientType) {
-                callBack(devicesRes.devices[i]);
-                break;
+              if (devicesRes.devices[i].name != "") {
+                if (devicesRes.devices[i].name === getApp().globalData.connectWillDevice.clientType) {
+                  callBack(devicesRes.devices[i]);
+                  break;
+                }
               }
             }
           });
@@ -99,6 +101,47 @@ class bleManager {
     });
   }
 
+  ///获取已经连接过手机的设备
+  getAllOnlineDevices(callBack) {
+    var that = this;
+    wx.getBluetoothDevices({
+      success: (res) => {
+        if (res && res.devices.length > 0) {
+          callBack(res);
+        }
+        setTimeout(() => {
+          that.getAllOnlineDevices();
+        }, 3 * 1000);
+      },
+      fail: (err) => {
+        setTimeout(() => {
+          that.getAllOnlineDevices();
+        }, 3 * 1000);
+      }
+    });
+  }
+
+  // 获取获取在蓝牙模块生效期间所有搜索到的蓝牙设备。包括已经和本机处于连接状态的设备。
+  getAllConnectedDevices() {
+    var that = this;
+    if (!that.isAvailable && !that.hasPermission) {
+      return [];
+    }
+
+    return new Promise((resolve, reject) => {
+      wx.getBluetoothDevices({
+        success: (res) => {
+          let newDevices = that.fiterDevice(res)
+          resolve(newDevices);
+        },
+        fail: (err) => {
+          console.error('已扫描过的蓝牙设备失败:', err);
+          reject([]);
+        }
+      });
+    });
+  }
+
   // 停止搜索
   stopScan() {
     return new Promise((resolve, reject) => {
@@ -201,30 +244,6 @@ class bleManager {
     });
   }
 
-  // 获取获取在蓝牙模块生效期间所有搜索到的蓝牙设备。包括已经和本机处于连接状态的设备。
-  getAllConnectedDevices() {
-    var that = this;
-    if (!that.isAvailable && !that.hasPermission) {
-      return [];
-    }
-    return new Promise((resolve, reject) => {
-      wx.getBluetoothDevices({
-        success: (res) => {
-          // const connectedDevices = res.devices.map(device => ({
-          //     deviceId: device.deviceId,
-          //     name: device.name || device.localName
-          // }));
-          let newDevices = that.fiterDevice(res)
-          resolve(newDevices);
-        },
-        fail: (err) => {
-          console.error('已扫描过的蓝牙设备失败:', err);
-          reject([]);
-        }
-      });
-    });
-  }
-
   // 断开与指定设备的连接
   disconnect() {
     var that = this;

+ 6 - 5
devices/bt_helper.js

@@ -29,9 +29,6 @@ const QueueManager = require('./QueueManager');
 // const EnumLowDelayMode = require('./EnumLowDelayMode');
 // const EnumPlayStatus = require('./EnumPlayStatus');
 
-
-
-
 class BtHelper {
 
   static getInstance() {
@@ -415,8 +412,6 @@ class BtHelper {
         }
       }
 
-
-
       // if (charc.properties.read) {
       //     var chara = await this.bleManager.readCharacteristicValue(charc.uuid,)
       // }
@@ -484,6 +479,12 @@ class BtHelper {
     });
   }
 
+  ///获取所有在线连接过的设备
+  getAllOnlineDevices(callBack) {
+    var that = this;
+    that.bleManager.getAllOnlineDevices(callBack);
+  }
+
   async getConnectedDevices() {
     var that = this;
     try {

+ 20 - 13
pages/connectBle/connectBle.js

@@ -9,7 +9,6 @@ import route_util from '../../utils/route_util.js';
 Page({
   data: {
     connectDevice: {},
-    connectingDevice: {},
     nvabarData: {
       showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
       title: '连接设备', //导航栏 中间的标题
@@ -23,7 +22,6 @@ Page({
     searchTips: "正在搜索设备,请保持开机状态…",
     subTips: "确认手机蓝牙已打开",
     buttonTips: "正在搜索设备",
-    btHelper: null,
     deviceImg: null,
   },
 
@@ -36,9 +34,7 @@ Page({
     });
     ///准备连接的设备
     app.globalData.connectWillDevice = that.data.connectDevice;
-    var b = BtHelper.getInstance();
-    that.data.btHelper = b;
-    b.initBluetooth(function (adapterState, hasPermission) {
+    BtHelper.getInstance().initBluetooth(function (adapterState, hasPermission) {
       console.log("蓝牙状态:", adapterState, hasPermission)
       if (adapterState && hasPermission) {
         that.startSearch();
@@ -47,7 +43,17 @@ Page({
         that.setStatus(1);
       }
     })
-    that.findAllDevice()
+
+    // wx.getBluetoothDevices({
+    //   success: function (res) {
+    // res.devices
+    //     console.log('All discovered devices', JSON.stringify(res));
+    //   },
+    //   fail(err) {
+    //     console.log(err)
+    //   }
+    // })
+    // that.findAllDevice()
   },
 
   async startSearch() {
@@ -55,13 +61,14 @@ Page({
     that.setStatus(0)
     that.data.btHelper.search(
       async function (c) {
-          console.log("gadfadfqwerqewqr====" + JSON.stringify(c));
+          console.log("gadfadfqwerqewqr==111==" + JSON.stringify(c));
           that.setStatus(2);
+          that.data.btHelper.stopSearch();
           that.data.connectDevice.deviceId = c.deviceId;
           that.data.connectDevice.connectable = c.connectable;
         },
         async function (c) {
-          connected(c);
+
         },
     );
     // const connectedDevices = await that.getConnectedDevices()
@@ -171,7 +178,7 @@ Page({
 
   getConnectedDevices: async function () {
     var that = this;
-    const connectedDevices = await that.data.btHelper.getConnectedDevices()
+    const connectedDevices = await BtHelper.getInstance().getConnectedDevices()
     console.log("全部设备", connectedDevices)
     if (connectedDevices.length > 0) {
       connectedDevices.forEach(element => {
@@ -184,7 +191,7 @@ Page({
 
   findAllDevice() {
     var that = this;
-    that.data.btHelper.findDevices(function (devices) {
+    BtHelper.getInstance().findDevices(function (devices) {
       let hasFind = false;
       devices.forEach(element => {
         console.log('发现设备2:', element);
@@ -221,7 +228,7 @@ Page({
         console.log("找到设备ble", element);
         that.data.connectDevice.mac = element.mac
         that.data.connectDevice.deviceId = element.deviceId
-        that.data.btHelper.stopSearch()
+        BtHelper.getInstance().stopSearch()
         that.setStatus(2)
         return true
       }
@@ -231,8 +238,8 @@ Page({
 
   ///关闭界面时候触发
   onUnload: function () {
-    // var that = this;
-    // that.data.btHelper.closeBle();
+    var that = this;
+    BtHelper.getInstance().stopSearch();
   }
 })
 

+ 0 - 1
pages/index/index.js

@@ -611,7 +611,6 @@ Page({
       route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
     } else if (item.state === "offline") {
       console.log("去连接蓝牙")
-
     } else {
       console.log("去添加蓝牙")
       that.addConnectBlueDevice({

+ 100 - 95
pages/index/index.wxml

@@ -19,7 +19,13 @@
     </view>
     <scroll-view scroll-y="true" style="height: calc(100vh - 118rpx - 14rpx - 20rpx  - 88rpx - 64rpx - 58rpx - {{bannerList.length>0?296:0}}rpx -  {{navBarHeight}}px);margin-top: 22rpx;padding-top: 10rpx; ">
       <view wx:if="{{deviceList}}">
+        {{item.state && item.state === 'online'&&deviceListIndex === index }}
         <block wx:key="key" wx:for-item="item" wx:for-index="index" wx:for="{{deviceList}}">
+          {{item.state }}
+          {{item.state && item.state === 'online'}}
+          {{deviceListIndex }}
+          {{index }}
+          {{item.state && item.state === 'online'&&deviceListIndex === index}}
           <view class="item_public" style="width: calc(100vw - 72rpx);border: {{item.state && item.state === 'online'&&deviceListIndex === index  ? '4rpx solid #6546A3' : item.state && item.state === 'online'?'2rpx solid #6546A3':'2rpx solid #F2F5F7'}}; margin-left: {{item.state && item.state === 'online'&&deviceListIndex === index  ?0:4}}rpx;" data-item="{{item}}" data-index="{{index}}" bind:tap="onTapItem">
             <image class="item_public_image" src="./../../img/min.png" style="opacity: {{item.state && item.state === 'online' ? '1' : '0.5'}};"></image>
             <view class="item_public_right" style="margin-right: 26rpx; opacity: {{item.state && item.state === 'online' ? '1' : '0.5'}};">
@@ -54,126 +60,125 @@
           <image src="./../../img/mm.png" class="mode" mode="scaleToFill"></image>
           <image class="logo" mode="heightFix" src="./../../img/logo.png"></image>
           <view wx:if="{{actionIndex !== null}}" style="opacity: 0.8; text-shadow:0px 0px 4px #6D00FF;" class="fm">
-            FM.{{actionIndex !== null ? (actionIndex + 1 < 10 ? "0" + (actionIndex + 1) : actionIndex + 1) : 00}}
+            FM.{{actionIndex !== null ? (actionIndex + 1 < 10 ? "0" + (actionIndex + 1) : actionIndex + 1) : 00}} </view>
+              <view wx:if="{{actionIndex !== null}}" style="opacity: 1;" class="fm">
+                FM.{{actionIndex !== null ? (actionIndex + 1 < 10 ? "0" + (actionIndex + 1) : actionIndex + 1) : 00}} <image class="gochnnalList" mode="heightFix" src="./../../img/a6.png">
+                  </image>
+              </view>
+              <view class="fmMod" bindtap="onTapToChannel"></view>
+              <view wx:if="{{actionIndex !== null}}" class="thisMusic">
+                <text>当前播放:</text>
+                <view class="scroll-box">
+                  <view class="scroll-wrap">
+                    <view class="scroll-item">{{channelData[actionIndex].channelName}}</view>
+                  </view>
+                </view>
+              </view>
+              <image src="{{'./../../img/dl' + battery + '.png'}}" class="battery" mode="scaleToFill"></image>
           </view>
-          <view wx:if="{{actionIndex !== null}}" style="opacity: 1;" class="fm">
-            FM.{{actionIndex !== null ? (actionIndex + 1 < 10 ? "0" + (actionIndex + 1) : actionIndex + 1) : 00}}
-            <image class="gochnnalList" mode="heightFix" src="./../../img/a6.png"></image>
+          <view class="wakeMode" bindtap="goWake">
+            <image src="./../../img/woak.png" class="mode" mode="scaleToFill"></image>
+            <image class="wake" mode="heightFix" src="{{isSetWake ? './../../img/n24.png' : './../../img/n23.png' }}"></image>
+            <text>定时</text>
           </view>
-          <view class="fmMod" bindtap="onTapToChannel"></view>
-          <view wx:if="{{actionIndex !== null}}" class="thisMusic">
-            <text>当前播放:</text>
-            <view class="scroll-box">
-              <view class="scroll-wrap">
-                <view class="scroll-item">{{channelData[actionIndex].channelName}}</view>
+        </view>
+        <scroll-view class="pd" scroll-y="true">
+          <view class="conm" wx:if="{{channelData && channelData.length > 0}}">
+            <image mode="scaleToFill" class="cz" src="./../../img/cz.png"></image>
+            <view class="musicList">
+              <view wx:for="{{channelData}}" class="li {{index === actionIndex ? 'action' : ' '}}">
+                <image mode="scaleToFill" wx:if="{{index === actionIndex}}" class="zz" src="./../../img/zz.png"></image>
+                <text class="lm">{{luoma[index]}}</text>
+                <view class="musicInfo" data-index="{{index}}" bindtap="actionMusic">
+                  <image mode="scaleToFill" class="pic" src="{{item.channelPic ? item.channelPic : './../../img/head_pic.png'}}"></image>
+                  <text>{{item.channelName}}</text>
+                  <image mode="scaleToFill" wx:if="{{index === actionIndex}}" class="svg" src="./../../svg/audio.svg"></image>
+                </view>
               </view>
             </view>
+            <!-- <image src="./../../svg/audio.svg"></image> -->
           </view>
-          <image src="{{'./../../img/dl' + battery + '.png'}}" class="battery" mode="scaleToFill"></image>
-        </view>
-        <view class="wakeMode" bindtap="goWake">
-          <image src="./../../img/woak.png" class="mode" mode="scaleToFill"></image>
-          <image class="wake" mode="heightFix" src="{{isSetWake ? './../../img/n24.png' : './../../img/n23.png' }}"></image>
-          <text>定时</text>
-        </view>
-      </view>
-      <scroll-view class="pd" scroll-y="true">
-        <view class="conm" wx:if="{{channelData && channelData.length > 0}}">
-          <image mode="scaleToFill" class="cz" src="./../../img/cz.png"></image>
-          <view class="musicList">
-            <view wx:for="{{channelData}}" class="li {{index === actionIndex ? 'action' : ' '}}">
-              <image mode="scaleToFill" wx:if="{{index === actionIndex}}" class="zz" src="./../../img/zz.png"></image>
-              <text class="lm">{{luoma[index]}}</text>
-              <view class="musicInfo" data-index="{{index}}" bindtap="actionMusic">
-                <image mode="scaleToFill" class="pic" src="{{item.channelPic ? item.channelPic : './../../img/head_pic.png'}}"></image>
-                <text>{{item.channelName}}</text>
-                <image mode="scaleToFill" wx:if="{{index === actionIndex}}" class="svg" src="./../../svg/audio.svg"></image>
+        </scroll-view>
+        <!-- 登录启用以下版本 -->
+        <view class="gologin" wx:if="{{showLogin}}">
+          <view class="model">
+            <view bindtap="goLogin" style="position: none; height: auto;" class="loginPic">
+              <image style="margin: 20rpx 0 20rpx 0; width: 100vw;" mode="widthFix" src="./../../img/BG_long.png"></image>
+            </view>
+            <view style="position: none; height: calc(100% - 224rpx); overflow: auto;" class="BG_PD">
+              <view>
+                <image style="margin: 0; width: 100vw;" mode="widthFix" src="./../../img/BG_PD.png"></image>
               </view>
             </view>
           </view>
-          <!-- <image src="./../../svg/audio.svg"></image> -->
         </view>
-      </scroll-view>
-      <!-- 登录启用以下版本 -->
-      <view class="gologin" wx:if="{{showLogin}}">
-        <view class="model">
-          <view bindtap="goLogin" style="position: none; height: auto;" class="loginPic">
-            <image style="margin: 20rpx 0 20rpx 0; width: 100vw;" mode="widthFix" src="./../../img/BG_long.png"></image>
-          </view>
-          <view style="position: none; height: calc(100% - 224rpx); overflow: auto;" class="BG_PD">
-            <view>
-              <image style="margin: 0; width: 100vw;" mode="widthFix" src="./../../img/BG_PD.png"></image>
+        <view class="gologin goruter" wx:if="{{!showLogin && deviceList && deviceList.length === 0}}">
+          <view class="model">
+            <view bindtap="goDeviceConnect" style="position: none; height: auto;" class="loginPic">
+              <image style="margin: 20rpx 0 20rpx 0; width: 100vw;" mode="widthFix" src="./../../img/BG_LJ.png"></image>
             </view>
-          </view>
-        </view>
-      </view>
-      <view class="gologin goruter" wx:if="{{!showLogin && deviceList && deviceList.length === 0}}">
-        <view class="model">
-          <view bindtap="goDeviceConnect" style="position: none; height: auto;" class="loginPic">
-            <image style="margin: 20rpx 0 20rpx 0; width: 100vw;" mode="widthFix" src="./../../img/BG_LJ.png"></image>
-          </view>
-          <view style="position: none; height: calc(100% - 224rpx); overflow: auto;" class="BG_PD">
-            <view>
-              <image style="margin: 0; width: 100vw;" mode="widthFix" src="./../../img/BG_PD.png"></image>
+            <view style="position: none; height: calc(100% - 224rpx); overflow: auto;" class="BG_PD">
+              <view>
+                <image style="margin: 0; width: 100vw;" mode="widthFix" src="./../../img/BG_PD.png"></image>
+              </view>
             </view>
           </view>
         </view>
       </view>
-    </view>
-    <view wx:elif="{{indexPage==2}}" class="me" style="height: calc(100vh - 100rpx - {{navBarHeight}}px);">
-      <view class="head-logo">
-        <image mode="scaleToFill" src="{{userPic}}"></image>
-        <text class="nick_name" wx:if="{{nickName}}">{{nickName}}</text>
-      </view>
-      <view class="br"></view>
-      <view class="list">
-        <view class="li">
-          <view class="icon">
-            <image mode="widthFix" src="./../../img/o.png"></image>
-          </view>
-          <view class="button">
-            <button open-type="feedback">建议与反馈</button>
-            <image class="j" mode="heightFix" src="./../../img/b.png"></image>
-          </view>
+      <view wx:elif="{{indexPage==2}}" class="me" style="height: calc(100vh - 100rpx - {{navBarHeight}}px);">
+        <view class="head-logo">
+          <image mode="scaleToFill" src="{{userPic}}"></image>
+          <text class="nick_name" wx:if="{{nickName}}">{{nickName}}</text>
         </view>
-        <view class="li">
-          <view class="icon">
-            <image mode="widthFix" src="./../../img/q.png"></image>
+        <view class="br"></view>
+        <view class="list">
+          <view class="li">
+            <view class="icon">
+              <image mode="widthFix" src="./../../img/o.png"></image>
+            </view>
+            <view class="button">
+              <button open-type="feedback">建议与反馈</button>
+              <image class="j" mode="heightFix" src="./../../img/b.png"></image>
+            </view>
           </view>
-          <view class="button" bindtap="goMeAbout">
-            <text>关于我们</text>
-            <image class="j" mode="heightFix" src="./../../img/b.png"></image>
+          <view class="li">
+            <view class="icon">
+              <image mode="widthFix" src="./../../img/q.png"></image>
+            </view>
+            <view class="button" bindtap="goMeAbout">
+              <text>关于我们</text>
+              <image class="j" mode="heightFix" src="./../../img/b.png"></image>
+            </view>
           </view>
         </view>
-      </view>
-      <view wx:if="{{isLogin}}" class="login">
-        <view bindtap="onTapLogin" class="{{!isLogin ? 'action' : ''}}">
-          {{isLogin ? '退出登录' : "登录"}}
+        <view wx:if="{{isLogin}}" class="login">
+          <view bindtap="onTapLogin" class="{{!isLogin ? 'action' : ''}}">
+            {{isLogin ? '退出登录' : "登录"}}
+          </view>
         </view>
       </view>
     </view>
   </view>
-</view>
-<view style="height: 98rpx;">
-  <view class="line"></view>
-  <view style="height: 97rpx; text-align: center; display: flex; flex: 1; align-items: center;">
-    <view class="bottom_view" data-index="0" bind:tap="onTapIndex">
-      <image class="bottom_image" mode="widthFix" src="{{indexPage==0?'./../../img/index_action.png':'./../../img/index_unaction.png'}}"></image>
-      <view style="font-size: 20rpx;color: {{indexPage==0?'#A4D099':'black'}}; padding-top: 0rpx;margin-top: 0rpx;">
-        首页
+  <view style="height: 98rpx;">
+    <view class="line"></view>
+    <view style="height: 97rpx; text-align: center; display: flex; flex: 1; align-items: center;">
+      <view class="bottom_view" data-index="0" bind:tap="onTapIndex">
+        <image class="bottom_image" mode="widthFix" src="{{indexPage==0?'./../../img/index_action.png':'./../../img/index_unaction.png'}}"></image>
+        <view style="font-size: 20rpx;color: {{indexPage==0?'#A4D099':'black'}}; padding-top: 0rpx;margin-top: 0rpx;">
+          首页
+        </view>
+      </view>
+      <view wx:if="{{deviceListIndex!=null&&deviceList.length>deviceListIndex&&deviceList[deviceListIndex].connectType===3}}" class="bottom_view" data-index="1" bind:tap="onTapIndex">
+        <image class="bottom_image" mode="widthFix" src="{{indexPage==1?'./../../img/channel_action.png':'./../../img/channel_unaction.png'}}"></image>
+        <view style="font-size: 20rpx;color: {{indexPage==1?'#A4D099':'black'}};">频道</view>
+      </view>
+      <view class="bottom_view" data-index="2" bind:tap="onTapIndex">
+        <image class="bottom_image" mode="widthFix" src="{{indexPage==2?'./../../img/me_action.png':'./../../img/me_unaction.png'}}"></image>
+        <view style="font-size: 20rpx;color: {{indexPage==2?'#A4D099':'black'}};">我的</view>
       </view>
-    </view>
-    <view wx:if="{{deviceListIndex!=null&&deviceList.length>deviceListIndex&&deviceList[deviceListIndex].connectType===3}}" class="bottom_view" data-index="1" bind:tap="onTapIndex">
-      <image class="bottom_image" mode="widthFix" src="{{indexPage==1?'./../../img/channel_action.png':'./../../img/channel_unaction.png'}}"></image>
-      <view style="font-size: 20rpx;color: {{indexPage==1?'#A4D099':'black'}};">频道</view>
-    </view>
-    <view class="bottom_view" data-index="2" bind:tap="onTapIndex">
-      <image class="bottom_image" mode="widthFix" src="{{indexPage==2?'./../../img/me_action.png':'./../../img/me_unaction.png'}}"></image>
-      <view style="font-size: 20rpx;color: {{indexPage==2?'#A4D099':'black'}};">我的</view>
     </view>
   </view>
-</view>
-<!-- <view wx:if="{{newVersion}}" class="updataminiwx">
+  <!-- <view wx:if="{{newVersion}}" class="updataminiwx">
     <view class="mode">
       <image class="bg" src="./../../img/BG@2x.png"></image>
       <view class="updata-view">