瀏覽代碼

临时存储

搜索a7c设备信息
kindring 2 年之前
父節點
當前提交
2f6167d36b
共有 7 個文件被更改,包括 157 次插入31 次删除
  1. 7 0
      app.wxss
  2. 13 3
      pages/index/index.js
  3. 20 21
      pages/index/index.wxml
  4. 47 0
      pages/index/index.wxss
  5. 1 1
      project.config.json
  6. 62 6
      utils/ble.js
  7. 7 0
      utils/buffer.js

+ 7 - 0
app.wxss

@@ -22,4 +22,11 @@
 .custom-info-btn:active{
   background-color: #c2b8b8;
   box-shadow: 1px 1px 5px black;
+}
+.mx-10{
+  margin-left: 10px;
+  margin-right: 10px;
+}
+.text-red{
+  color: red;
 }

+ 13 - 3
pages/index/index.js

@@ -18,6 +18,7 @@ const bleStateEnum = {
 Page({
   data: {
     connectStateMap,
+    bleStateEnum,
     ble:{
       state: 0,
       init: false,
@@ -39,6 +40,7 @@ Page({
     try {
       console.log("init ble");
       await ble.initBle();
+      ble.onSearch = this.onSearchHandle;
       this.setData({
         ble: {...this.data.ble,init: true},
       })
@@ -73,12 +75,20 @@ Page({
         title: '蓝牙错误',
         content: "无法启用蓝牙!!!",
         success (res) { }
-      })
+      }) 
     }
-    
     this.setData({
       ble: {...this.data.ble,state: 1},
-    })
+    });
+    ble.search(this.onSearchHandle);
+  },
+  onSearchHandle(device){
+    console.log('搜索到设备');
+    if(this.data.bleDevs.find(dev=>dev.deviceId === device.deviceId )){
+      return console.log('设备再次被搜索到');
+    }
+    console.log(device);
+    this.data.bleDevs.push(device);
   },
   tapState(){
     switch(this.data.ble.state){

+ 20 - 21
pages/index/index.wxml

@@ -5,36 +5,35 @@
     <view class="state-info" bindtap="tapState">
      <led color="{{connectStateMap[ble.state].color}}" width="15"/> {{connectStateMap[ble.state].text}}
     </view>
-    <view class="state-msg" wx:if="{{ble.state == 3}}">
+    <view class="state-msg" wx:if="{{ble.state == bleStateEnum.connected}}">
       {{remoteDev.devName}}
     </view>
-    <view class="state-msg" wx:if="{{ble.state == 0}}">
+    <view class="state-msg" wx:if="{{ble.state == bleStateEnum.notConnect}}">
       <view class="custom-info-btn" bindtap="searchDevice" >扫描</view>
     </view>
-    <view class="state-msg" wx:if="{{ble.state == 2}}">
+    <view class="state-msg" wx:if="{{ble.state == bleStateEnum.connecting}}">
       <view class="custom-info-btn" bindtap="searchDevice" >重新扫描</view>
     </view>
   </view>
-  <view class="control">
-    
-  </view>
-  <view class="userinfo">
-    <block wx:if="{{canIUseOpenData}}">
-      <view class="userinfo-avatar" bindtap="bindViewTap">
-        <open-data type="userAvatarUrl"></open-data>
+
+  <view class="bleDevices" wx:if="{{ble.state == bleStateEnum.searching || ble.state == bleStateEnum.connecting }}">
+    <view class="title">
+      设备列表
+      <view class="mx-10 text-red">{{bleDevs.length}}</view>
+    </view>
+    <view class="devList">
+      <view class="devItem" wx:for="{{bleDevs}}" wx:key="deviceId" >
+        <view class="devInfo">
+          <view class="name">{{item.Name}}</view>
+          <view class="devId">{{item.deviceId}}</view>
+        </view>
+        <view class="option">
+          <view class="btn">连接</view>
+        </view>
       </view>
-      <open-data type="userNickName"></open-data>
-    </block>
-    <block wx:elif="{{!hasUserInfo}}">
-      <button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </button>
-      <button wx:elif="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
-      <view wx:else> 请使用1.4.4及以上版本基础库 </view>
-    </block>
-    <block wx:else>
-      <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
-      <text class="userinfo-nickname">{{userInfo.nickName}}</text>
-    </block>
+    </view>
   </view>
+  
   <view class="usermotto">
     <text class="user-motto">{{motto}}</text>
   </view>

+ 47 - 0
pages/index/index.wxss

@@ -47,4 +47,51 @@
   display: flex;
   justify-content: center;
   align-items: center;
+}
+
+.bleDevices{
+  width:100%;
+  height: 360px;
+  display: flex;
+  direction: column;
+  overflow: hidden;
+  margin-top: 10px;
+}
+.bleDevices .title{
+  width: 100%;
+  height: 24px;
+  display: flex;
+  align-items: center;
+  padding: 0 10px;
+}
+.bleDevices .devList{
+  padding: 0 10px;
+  height: 100%;
+  overflow: auto;
+}
+.bleDevices .devList .devItem{
+  width: 100%;
+  height: 36px;
+  border: 1px solid gray;
+  margin-bottom: 10px;
+  border-radius: 3px;
+  display: flex;
+  padding: 0 8px;
+}
+
+.bleDevices .devList .devItem .devInfo{
+  width: 70%;
+}
+
+.bleDevices .devList .devItem .devInfo .name{
+  font-size: 18px;
+}
+
+.bleDevices .devList .devItem .devInfo .devId{
+  font-size: 14px;
+}
+
+
+.bleDevices .devList .devItem .option{
+  width: 30%;
 }

+ 1 - 1
project.config.json

@@ -45,7 +45,7 @@
   },
   "compileType": "miniprogram",
   "libVersion": "2.19.4",
-  "appid": "wx0264b6ecd0573b81",
+  "appid": "wx3baaab5c72bef249",
   "projectname": "miniprogram-92",
   "condition": {},
   "editorSetting": {

+ 62 - 6
utils/ble.js

@@ -1,10 +1,7 @@
 const handle = require("./handle");
-
-const sonyBleCode = {
-
-}
-
-
+const {buf2hex} = require("./buffer");
+// 需要匹配的相机特征值列表
+const CAMERA_MANUFACTURER_LOOKUP = ["2d010300"];
 class BLE{
   constructor(){
     // this代表实例对象
@@ -18,6 +15,65 @@ class BLE{
     }
     return 0
   }
+  search(onSearchCallback){
+    // wx.onBluetoothDeviceFound((res) => {
+    //   res.devices.forEach((device) => {
+    //     // 这里可以做一些过滤
+    //     console.log('Device Found', device)
+    //   })
+    //   // 找到要搜索的设备后,及时停止扫描
+    //   wx.stopBluetoothDevicesDiscovery()
+    // })
+    console.log("搜索蓝牙中:");
+     setTimeout(() => {
+      console.log("test");
+    }, 2500);
+    wx.onBluetoothDeviceFound(function (res) {
+        var bleArray = res.devices;
+        //这里会收到周边搜索到的蓝牙
+        console.log("\n\nfind devices ----");
+        // console.log(res);
+        // console.log(res.devices);
+        // 对
+        for (let index = 0; index < bleArray.length; index++) {
+          const ble = bleArray[index];
+          if(ble.advertisData){
+            // 解析到特征值设备
+            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
+                })
+            }
+          }
+        }
+    });
+   
+    wx.startBluetoothDevicesDiscovery({
+      success(res) {
+        console.log("开始搜索蓝牙:", res)
+      },
+      fail(res) {
+        console.log(res)
+      }
+    })
+  }
+  onSearch(){
+    console.log('on serarch');
+  }
+  async stopSearch(){
+    let [err,res] = await handle(wx.stopBluetoothDevicesDiscovery());
+    if(err){
+      throw err;
+    }
+    return res;
+  }
+
 }
 
 

+ 7 - 0
utils/buffer.js

@@ -0,0 +1,7 @@
+ function buf2hex(buffer) { // buffer is an ArrayBuffer
+                return [...new Uint8Array(buffer)]
+                    .map(x => x.toString(16).padStart(2, '0'))
+                    .join('');
+              }
+
+module.exports = {buf2hex}