瀏覽代碼

1. 界面完善,
2. 蓝牙扫描功能调整

kindring 1 年之前
父節點
當前提交
229fea85d1
共有 10 個文件被更改,包括 155 次插入38 次删除
  1. 9 1
      data/lampType.js
  2. 4 0
      data/sony_item.js
  3. 22 0
      pages/index/index.js
  4. 2 2
      pages/index/index.wxml
  5. 84 18
      pages/light/light.js
  6. 2 2
      pages/light/light.wxml
  7. 1 1
      pages/old/index.js
  8. 7 4
      utils/mjs_buffer.js
  9. 21 10
      utils/mjs_wxble.js
  10. 3 0
      utils/test.js

+ 9 - 1
data/lampType.js

@@ -3,4 +3,12 @@ export const MAX_COLOR_TEMPERATURE = 6500;
 // 最小色温
 export const MIN_COLOR_TEMPERATURE = 2700;
 // 色温步长
-export const COLOR_TEMPERATURE_STEP = 50;
+export const COLOR_TEMPERATURE_STEP = 50;
+
+// 蓝牙相关信息
+export const bleInfo = {
+    // 控制uuid
+    controlUuid: "0000ffe9-0000-1000-8000-00805f9b34fb",
+    // 监听数据uuid
+    notifyUuid: "0000ffe4-0000-1000-8000-00805f9b34fb",
+}

+ 4 - 0
data/sony_item.js

@@ -39,3 +39,7 @@ export const sonyItems = [
         state: 5
     },
 ]
+
+export const sonyUuid = {
+    a7c: "2d010300"
+}

+ 22 - 0
pages/index/index.js

@@ -10,6 +10,28 @@ Page({
     sonyItems: []
   },
 
+  openPageHandle(e){
+    let cmd = e.currentTarget.dataset.dev;
+    console.log(e);
+    console.log(cmd);
+    switch(cmd){
+      case 'light':
+        wx.switchTab({
+          url: '/pages/light/light',
+        })
+        break;
+      case 'sony':
+          wx.switchTab({
+            url: '/pages/old/index',
+          })
+          break;
+      default:
+        wx.showToast({
+          title: '该页面正在开发中',
+        })
+        break;
+    }
+  },
   /**
    * 生命周期函数--监听页面加载
    */

+ 2 - 2
pages/index/index.wxml

@@ -8,7 +8,7 @@
             <text class="sm-text">本工具用于控制专属led</text>
         </view>
         <view class="c-btn">
-            <view class="btn">立即使用</view>
+            <view class="btn" data-dev="light" bindtap="openPageHandle">立即使用</view>
         </view>
     </view>
 
@@ -35,7 +35,7 @@
 
 <!-- 居中路由按钮. 实现立即跳转至对应页面-->
         <view class="c-btn" >
-            <view class="btn">立即使用</view>
+            <view class="btn" data-dev="{{'sony'}}" bindtap="openPageHandle" >立即使用</view>
         </view>
         </view>
     </view>

+ 84 - 18
pages/light/light.js

@@ -24,11 +24,16 @@ Page({
      * 页面的初始数据
      */
     data: {
+        // unConnect scaning connecting connected
         connectStateTypes: connectStateTypes,
         ble: {
             state: connectStateTypes.scaning,
-            devName: "123",
-            init: false
+            devName: "",
+            init: false,
+            // 控制uuid
+            controlUuid: "",
+            // 监听数据uuid
+            notifyUuid: "",
         },
         lamp: {
             // 信息相关
@@ -93,43 +98,104 @@ Page({
         this.setData({
             lamp: {...this.data.lamp, switch: !this.data.lamp.switch}
         });
+        this.setData({
+            lamp: {...this.data.lamp, fullOpen: false}
+        })
         this.reComputeBgColor();
     },
     
+    bleFail(title,msg){
+      this.setData({
+        ble: {...this.data.ble, 
+          state: connectStateTypes.unConnect, 
+          init: false
+        },
+        bleDevs: []
+      });
+      wx.showModal({
+        title: title,
+        content: msg,
+        success (res) { }
+      }) 
+    },
     // 蓝牙控制板块
     searchDeviceHandle() {
         // 开始搜索设备
         this.excuteSearchDevice();
     },
-    connectDevHandle(e) {
-        // 连接设备
-        console.log("click connectDev")
-        console.log(e);
-        let dev = e.detail;
-        console.log(dev);
-    },
+    
     async excuteSearchDevice(){
         console.log("搜索 蓝牙设备");
         let err,res;
         if(!this.data.ble.init){
           [err,res] = await handle(ble.initBle());
           if(err){
-            return wx.showModal({
-                title: '蓝牙错误',
-                content: "无法启用蓝牙!!!",
-                success (res) { }
-              }) 
+            return this.bleFail('蓝牙初始化失败', err.errMsg);
           }
           ble.onSearch = this.onSearchHandle;
-          this.setData({
-                ble: {...this.data.ble, init: true},
-          })
         }
         this.setData({
-          ble: {...this.data.ble,state: connectStateTypes.searching},
+          ble: {...this.data.ble, 
+            state: connectStateTypes.scaning, 
+            init: true
+          },
+          bleDevs: []
         });
         ble.search(this.onSearchHandle);
     },
+    // 搜索到设备基础函数
+    onSearchHandle(device){
+      console.log('搜索到设备');
+      if(this.data.bleDevs.find(dev=>dev.deviceId === device.deviceId )){
+        return console.log('设备再次被搜索到');
+      }
+      console.log(device);
+      device.id = device.deviceId;
+      let arr = this.data.bleDevs;
+      arr.push(device);
+      this.setData({
+        bleDevs: arr
+      })
+    },
+    // 连接设备
+    async connectDevHandle(e) {
+      console.log("click connectDev")
+      let err,res;
+      let dev = e.detail;
+      console.log(dev);
+      this.setData({
+        ble: {...this.data.ble,state: connectStateTypes.connecting, 
+          devName: dev.name, 
+          deviceId: dev.deviceId
+        },
+      });
+      // 停止搜索
+      [err,res] = await handle(ble.stopSearch());
+      if(err){
+        return this.bleFail('停止搜索失败', err.errMsg);
+      }
+      wx.showToast({title: `连接${dev.name}中...`, })
+      // 连接至设备
+      [err,res] = await handle(ble.connectDev(dev.deviceId));
+      if(err){  return this.bleFail('连接失败', err.errMsg);}
+      // 连接成功
+      this.setData({
+        ble:{...this.data.ble, state:connectStateTypes.connected }
+      });
+      // 设备服务管理
+      this.connectServices();
+  },
+
+  async connectServices(){
+      // uuid FF01
+      let [err,res] = await handle(ble.getServices());
+      if(err){ return this.bleFail('获取服务失败', err.errMsg);}
+      res.forEach(val=>{
+        // 不同的服务有不同的功能
+        console.log(val);
+      })
+  },
+
 
 
       

+ 2 - 2
pages/light/light.wxml

@@ -6,7 +6,7 @@
     bindsearchDevice="searchDeviceHandle"
     />
 
-    <view class="lamp__content">
+    <view class="lamp__content" style="background-color:{{lamp.bgc}}">
         <tabList
         wx:if="{{ble.state === connectStateTypes.scaning || ble.state === connectStateTypes.connecting}}"
         ble-devs="{{ bleDevs }}"
@@ -16,7 +16,7 @@
 
         <view class="lampControl" 
         wx:if="{{ble.state === connectStateTypes.connected}}"
-        style="background-color:{{lamp.bgc}}"
+        
         >
             <!-- 占位块 -->
             <view class="lampControl__placeholder"></view>

+ 1 - 1
pages/old/index.js

@@ -450,7 +450,7 @@ Page({
     let arr = this.data.bleDevs;
     arr.push(device);
     this.setData({
-      bleDevs:arr
+      bleDevs: arr
     })
   },
   tapState(){

+ 7 - 4
utils/mjs_buffer.js

@@ -1,10 +1,10 @@
- function buf2hex(buffer) { // buffer is an ArrayBuffer
+ export function buf2hex(buffer) { // buffer is an ArrayBuffer
                 return [...new Uint8Array(buffer)]
                     .map(x => x.toString(16).padStart(2, '0'))
                     .join('');
               }
 
-function hex2ab(hex){
+export function hex2ab(hex){
   let typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
     return parseInt(h, 16)
   }))
@@ -16,8 +16,11 @@ function hexStrToBuffer(str){
   if(str.length % 2){str=`0${str}`}
   
   for(let i=0;i<str.length;i+=2){
-    hex2ab
+    hex2ab(str.substr(i,2))
   }
 }
 
-export default {buf2hex,hex2ab}
+export default {
+  buf2hex,
+  hex2ab
+}

+ 21 - 10
utils/mjs_wxble.js

@@ -1,14 +1,18 @@
 
 import {handle} from "./mjs_handle";
 import {buf2hex, hex2ab} from "./mjs_buffer";
-// 需要匹配的相机特征值列表
-const CAMERA_MANUFACTURER_LOOKUP = ["2d010300"];
 class BLE{
-  constructor(){
+  // 白名单设备id列表
+  whiteList = [];
+  useWhiteList = false;
+  constructor(whiteList = []){
     // this代表实例对象
      this.isInit= false;
+    this.whiteList = whiteList;
+    this.useWhiteList = whiteList.length > 0;
  }
   
+  
   async initBle(option){
     let [err,ok] = await handle(wx.openBluetoothAdapter(option));
     if(err){
@@ -43,13 +47,20 @@ class BLE{
             let advertisData = buf2hex(ble.advertisData);
             console.log(advertisData);
             console.log(ble);
-            if(CAMERA_MANUFACTURER_LOOKUP.find(codeStr=>advertisData.startsWith(codeStr))){
-              console.log('找到匹配到的设备');
-                // 找到相机
-                onSearchCallback({
-                  ...ble,
-                  hexAdvertisData: advertisData
-                })
+            if (this.useWhiteList ){
+              if(whiteList.find(codeStr=>advertisData.startsWith(codeStr))){
+                console.log('找到匹配到的设备');
+                  // 找到相机
+                  onSearchCallback({
+                    ...ble,
+                    hexAdvertisData: advertisData
+                  })
+              }
+            }else{
+              onSearchCallback({
+                ...ble,
+                hexAdvertisData: advertisData
+              })
             }
           }
         }

+ 3 - 0
utils/test.js

@@ -0,0 +1,3 @@
+function test(){
+    console.log('test');
+}