Browse Source

feature: 蓝牙设备和wifi设备选择连接处理

Damon 8 months ago
parent
commit
b4d4352865
1 changed files with 40 additions and 14 deletions
  1. 40 14
      pages/index/index.js

+ 40 - 14
pages/index/index.js

@@ -84,7 +84,7 @@ Page({
     var devicelist = wx.getStorageSync("devicelist") || "";
     if (!strings.isEmpty(devicelist)) {
       var list = JSON.parse(devicelist);
-      that.updateDeviceList(list, true);
+      that.updateDeviceList(list, false);
     }
   },
 
@@ -164,12 +164,16 @@ Page({
         }
         ///获取播放状态
         else if (payloads.type === "play" || payloads.type === "play_state") {
-          if (that.data.deviceListIndex === null) {
-            return;
-          }
+          var deviceList = that.data.deviceList;
+          var deviceListIndex = that.data.deviceListIndex;
+          if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
+            return
+          };
+          var deviceId = deviceList[deviceListIndex].deviceId;
+
           // 接收设备当前播放状态
           const obj = {
-            DstDeviceName: that.getThisDeviceID()
+            DstDeviceName: that.getThisDeviceID(deviceId)
           }
           app.PubMsg({
             type: "get_position",
@@ -333,15 +337,28 @@ Page({
       return;
     };
 
-    if (that.data.deviceList.length === 0 || that.data.deviceListIndex === null) {
+    var deviceList = that.data.deviceList;
+    var deviceListIndex = that.data.deviceListIndex;
+    if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
       return
     };
 
-    let topic = `/AIrSMArT_${that.data.deviceList[that.data.deviceListIndex].name.split("BLUFI_")[1]}/user/pub_response`;
+    var tempDevice = that.data.deviceList[deviceListIndex];
+    var tempDeviceId = tempDevice.deviceId;
+    ///删除当前选中这个
+    deviceList.splice(deviceListIndex, 1);
+    ///添加到第一个去
+    deviceList.unshift(tempDevice);
+    that.setData({
+      deviceListIndex: 0,
+      deviceList: deviceList,
+    });
 
+    let topic = `/AIrSMArT_${tempDeviceId.split("BLUFI_")[1]}/user/pub_response`;
     app.subscribe(topic);
+
     const obj = {
-      DstDeviceName: that.getThisDeviceID()
+      DstDeviceName: that.getThisDeviceID(tempDeviceId)
     };
     app.PubMsg({
       type: "get_dev_info",
@@ -364,6 +381,13 @@ Page({
     that.setData({
       actionIndex: e.currentTarget.dataset.index
     });
+
+    var deviceList = that.data.deviceList;
+    var deviceListIndex = that.data.deviceListIndex;
+    if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
+      return
+    };
+    var deviceId = deviceList[deviceListIndex].deviceId;
     const other = {
       "url": "",
       "media_data": "",
@@ -378,21 +402,23 @@ Page({
     };
     app.PubMsg({
       type: "play",
-      DstDeviceName: that.getThisDeviceID(),
+      DstDeviceName: that.getThisDeviceID(deviceId),
       other
     });
   },
 
-  getThisDeviceID() {
-    var that = this;
-    return `AIrSMArT_${that.data.deviceList[that.data.deviceListIndex].name.split("BLUFI_")[1]}`
+  getThisDeviceID(deviceId) {
+    return `AIrSMArT_${deviceId.split("BLUFI_")[1]}`
   },
 
   getchannelData(clientType) {
     var that = this;
-    if (that.data.deviceListIndex === null) {
+    var deviceList = that.data.deviceList;
+    var deviceListIndex = that.data.deviceListIndex;
+    if (deviceListIndex === null || deviceList.length <= deviceListIndex) {
       return;
     };
+    var deviceId = deviceList[deviceListIndex].deviceId;
 
     listByDevice({
       clientType
@@ -402,7 +428,7 @@ Page({
       });
       // 接收设备当前播放状态
       const obj = {
-        DstDeviceName: that.getThisDeviceID()
+        DstDeviceName: that.getThisDeviceID(deviceId)
       }
       app.PubMsg({
         type: "get_position",