Browse Source

feature:增加首页的蓝牙自动连接

zeng.chen 8 months ago
parent
commit
866f473538
2 changed files with 164 additions and 121 deletions
  1. 58 10
      pages/index/index.js
  2. 106 111
      pages/index/index.wxml

+ 58 - 10
pages/index/index.js

@@ -8,6 +8,7 @@ import timeUtil from '../../utils/time_util.js';
 import strings from '../../utils/strings.js';
 import route_constant from '../../utils/route_constant.js';
 import route_util from '../../utils/route_util.js';
+import { BtHelper } from '../../devices/bt_helper.js';
 
 Page({
   data: {
@@ -80,12 +81,58 @@ Page({
     }
   },
 
+  tryConnectBle() {
+    var hasBle = false;
+    var hasConnectBle = false;
+    var hasWifi = false;
+    var bleDevice;
+    let that = this
+    this.data.deviceList.forEach(device => {
+      if (device.connectType == 1) {
+        hasBle = true
+        if (device.state == "online") {
+          hasConnectBle = true
+        }
+        bleDevice = device
+      } else if (device.connectType == 3 && device.state == "online") {
+        // wifi
+        hasWifi = true;
+      }
+    });
+    if (!hasWifi && !hasConnectBle && hasBle) {
+      // 没有wifi 没有连接的ble 有未连接的ble
+      console.log("去连接蓝牙")
+      let bt_helper = BtHelper.getInstance()
+      bt_helper.initBluetooth(function (adapterState, hasPermission) {
+        console.log("蓝牙状态", adapterState, hasPermission)
+        if (adapterState && hasPermission) {
+          bt_helper.connect(bleDevice, function (data) {
+            console.log("连接成功:", data)
+            if (data == true) {
+              // 蓝牙模式
+              bleDevice.connectType = 1
+              // 在线
+              bleDevice.state = 'online'
+              bleDevice.ProdModel = bleDevice.clientType
+              that.addConnectBlueDevice(connectDevice);
+            }
+          })
+        } else {
+        }
+      })
+    }
+  },
   onDeviceLoad() {
     var that = this;
     var devicelist = wx.getStorageSync("devicelist") || "";
     if (!strings.isEmpty(devicelist)) {
       var list = JSON.parse(devicelist);
+<<<<<<< Updated upstream
       that.updateDeviceList(list, true);
+=======
+      that.updateDeviceList(list, false);
+      that.tryConnectBle()
+>>>>>>> Stashed changes
     }
   },
 
@@ -547,8 +594,11 @@ Page({
       return;
     }
     var item = e.currentTarget.dataset.item;
+    console.log("点击item:", item)
+
     var connectType = item.connectType;
-    if (connectType == 3) {
+    // wifi只支持在线点击
+    if (connectType == 3 && item.state === "online") {
       if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
         that.goWake();
       } else {
@@ -557,19 +607,20 @@ Page({
       return;
     }
 
-    console.log(item)
-    route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
-    return
     ///去蓝牙连接处理
     if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
       console.log(item)
       route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(item))
+    } else if (item.state === "offline") {
+      console.log("去连接蓝牙")
+
     } else {
+      console.log("去添加蓝牙")
       that.addConnectBlueDevice({
         deviceId: item.deviceId,
         name: item.devName,
         state: item.state,
-        clientType: item.ProdModel,
+        clientType: item.ProdModel ?? item.clientType,
         mac: item.mac,
       });
     }
@@ -775,6 +826,7 @@ Page({
     var that = this;
     var finalList = [];
     finalList.push(deviceList[0]);
+    console.log("online", deviceList[0].deviceId, deviceList[0].state);
 
     var onLineList = [];
     var onNoLineList = [];
@@ -822,14 +874,10 @@ Page({
     // 更新缓存
     wx.setStorageSync("devicelist", JSON.stringify(finalList));
 
-    finalList.forEach(element => {
-      if (element.connectType == 1) {
-        element.state = "online"
-      }
-    });
     that.setData({
       deviceList: finalList
     });
+    getApp().globalData.deviceList = finalList;
   },
 
   ///******************************* 可折叠 ********************************************///

+ 106 - 111
pages/index/index.wxml

@@ -1,14 +1,9 @@
-<!--index.wxml-->
-
-<nav-bar bind:goBack="_goBack" nav-bgc-class="ex-nav-bgc-class" nav-title-class="ex-nav-title-class" ex-back-pre="ex-back-pre" navbar-data='{{nvabarData}}'>
-</nav-bar>
-
+<!-- index.wxml -->
+<nav-bar bind:goBack="_goBack" nav-bgc-class="ex-nav-bgc-class" nav-title-class="ex-nav-title-class" ex-back-pre="ex-back-pre" navbar-data='{{nvabarData}}'></nav-bar>
 <view class="container">
   <view wx:if="{{indexPage==0}}" class="mair" style="height: calc(100vh - 120rpx - {{navBarHeight}}px);">
-
     <view style="font-size: 32rpx; color: #333333; margin-top: 14rpx;">{{greeting}}</view>
-
-    <!-- banner-->
+    <!-- banner -->
     <view wx:if="{{bannerList.length>0}}" class="banner_container">
       <swiper class="swiper" indicator-dots="{{bannerList.length>1}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}" circular="{{circular}}" indicator-active-color="#FFFFFF" indicator-color="#999999">
         <block wx:for="{{bannerList}}" wx:key="index">
@@ -18,26 +13,27 @@
         </block>
       </swiper>
     </view>
-
     <view class="add_device_public" bind:tap="addDevice">
       <image mode="heightFix" src="./../../img/add_device.png" style="width: 48rxp;height: 48rpx;"></image>
       <view style="color: white; margin-left: 8rpx; font-size: 32rpx;">添加设备</view>
     </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}}">
         <block wx:key="key" wx:for-item="item" wx:for-index="index" wx:for="{{deviceList}}">
-          <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="{{item.state && item.state === 'online' ? 'onTapItem' : ''}}">
+          <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'}};">
               <view style="height: 190rpx; margin-top: 37rpx; margin-left: 32rpx; flex-direction: column; display: flex;">
                 <view style="flex: 1;">
                   <view style="display: flex;">
-                    <view style="color: #333333; font-size: 28rpx; flex: 1;padding-right: 5rpx;">{{item.devName}}</view>
+                    <view style="color: #333333; font-size: 28rpx; flex: 1;padding-right: 5rpx;">
+                      {{item.devName}}
+                    </view>
                     <image wx:if="{{item.state && item.state === 'online'&&deviceListIndex === index }}" mode="heightFix" src="./../../img/add_device_arrow.png" style="width: 48rxp;height: 48rpx;"></image>
                   </view>
-                  <view style="color: #666666; font-size: 24rpx;">{{item.state && item.state === "online" ? (item.connectType==3?"Wifi在线":"蓝牙在线") : "离线"}}</view>
+                  <view style="color: #666666; font-size: 24rpx;">
+                    {{item.state && item.state === "online" ? (item.connectType==3?"Wifi在线":"蓝牙在线") : "离线"}}
+                  </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">
                   <view style="color: #666666; font-size: 28rpx; ">删除</view>
@@ -49,7 +45,6 @@
       </view>
     </scroll-view>
   </view>
-
   <view wx:else>
     <!-- 连接设备启用以下版本 -->
     <view wx:if="{{indexPage==1&&deviceListIndex!=null&&deviceList.length>deviceListIndex&&deviceList[deviceListIndex].connectType===3}}" class="mair" style="height: calc(100vh - 118rpx - {{navBarHeight}}px);">
@@ -58,127 +53,127 @@
           <image src="./../../img/blak.png" class="black" mode="scaleToFill"></image>
           <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}}</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 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}}
           </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 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>
-
-        <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 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>
-            <!-- <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>
+          <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>
               </view>
             </view>
           </view>
+          <!-- <image src="./../../svg/audio.svg"></image> -->
         </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>
+      </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 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 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>
           </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 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>
-        <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 class="li">
+          <view class="icon">
+            <image mode="widthFix" src="./../../img/q.png"></image>
           </view>
-          <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 class="button" bindtap="goMeAbout">
+            <text>关于我们</text>
+            <image class="j" mode="heightFix" src="./../../img/b.png"></image>
           </view>
         </view>
-        <view wx:if="{{isLogin}}" class="login">
-          <view bindtap="onTapLogin" class="{{!isLogin ? 'action' : ''}}">{{isLogin ? '退出登录' : "登录"}}</view>
+      </view>
+      <view wx:if="{{isLogin}}" class="login">
+        <view bindtap="onTapLogin" class="{{!isLogin ? 'action' : ''}}">
+          {{isLogin ? '退出登录' : "登录"}}
         </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>
-      </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 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="{{newVersion}}" class="updataminiwx">
+</view>
+<!-- <view wx:if="{{newVersion}}" class="updataminiwx">
     <view class="mode">
       <image class="bg" src="./../../img/BG@2x.png"></image>
       <view class="updata-view">