Browse Source

noble search and connect

xiaoyuzhang 5 years ago
parent
commit
12176b9075

+ 1 - 1
.electron-vue/webpack.renderer.config.js

@@ -19,7 +19,7 @@ const { VueLoaderPlugin } = require('vue-loader')
19 19
  * that provide pure *.vue files that need compiling
20 20
  * https://simulatedgreg.gitbooks.io/electron-vue/content/en/webpack-configurations.html#white-listing-externals
21 21
  */
22
-let whiteListedModules = ['vue']
22
+let whiteListedModules = ['vue','element-ui']
23 23
 
24 24
 let rendererConfig = {
25 25
   devtool: '#cheap-module-eval-source-map',

File diff suppressed because it is too large
+ 4464 - 4415
package-lock.json


+ 105 - 10
src/renderer/components/ble/rt_ble.vue

@@ -5,36 +5,104 @@
5 5
     <el-button @click="search">搜索</el-button>
6 6
     <el-button @click="connect">连接</el-button>
7 7
     <el-button @click="send">发送</el-button>
8
+    <br/>
9
+    <el-select v-model="choseDeviceIndex" placeholder="请选择">
10
+        <el-option
11
+                v-for="(item,index) in blueDeviceList"
12
+                :key="'select'+ index"
13
+                :label="item.localName"
14
+                :value="index">
15
+        </el-option>
16
+      </el-select>
17
+    <div >
18
+      <el-table
19
+              :data="characteristicsList"
20
+              style="width: 100%;padding-top: 50px">
21
+        <el-table-column
22
+                prop="id"
23
+                label="id"
24
+                width="150">
25
+        </el-table-column>
26
+        <el-table-column
27
+                prop="uuid"
28
+                label="seviceUUid"
29
+                width="90">
30
+        </el-table-column>
31
+        <el-table-column
32
+                prop="name"
33
+                label="name"
34
+                width="130">
35
+          <template slot-scope="scope">
36
+            <span>{{scope.row.name == null?'未知name':scope.row.name}}</span>
37
+          </template>
38
+        </el-table-column>
39
+        <el-table-column
40
+                prop="name"
41
+                label="characteristicServiceUUid"
42
+                width="180">
43
+          <template slot-scope="scope">
44
+            <span>{{scope.row.characteristic.uuid}}</span>
45
+          </template>
46
+        </el-table-column>
47
+        <el-table-column
48
+                prop="name"
49
+                label="properties"
50
+                width="180">
51
+          <template slot-scope="scope">
52
+            <span v-for="item in scope.row.characteristic.properties">{{item + '、'}}</span>
53
+          </template>
54
+        </el-table-column>
55
+        <el-table-column
56
+                prop="name"
57
+                label="options"
58
+                width="180">
59
+          <template slot-scope="scope">
60
+            <el-button  :disabled ="!canConnect(scope.row.characteristic.properties)"  @click="connect(scope.row)">connect</el-button>
61
+          </template>
62
+        </el-table-column>
63
+
64
+      </el-table>
8 65
 
9
-    <div>
10
-      <el-radio v-for="(item,index) in blueDeviceList" v-model="choseBlueDeviceId" :key="'device'+index"
11
-                :label="item.id">
12
-        {{item.localName}}
13
-      </el-radio>
14 66
     </div>
15 67
 
16 68
 
69
+
70
+
17 71
   </div>
18 72
 
19 73
 </template>
20 74
 
21 75
 <script>
22
-  import {mapGetters} from "vuex"
23 76
 
77
+  import {mapGetters} from "vuex"
24 78
   export default {
25 79
     name: "rt_ble",
26 80
     data() {
27 81
       return {
28
-        choseBlueDeviceId: undefined
82
+        choseBlueDeviceId: undefined,
83
+        choseDeviceIndex:-1,
84
+        characteristicsList:[],
29 85
       }
30 86
     },
31 87
     created() {
32 88
       this.search()
33 89
     },
34
-
90
+    watch:{
91
+        blueDeviceList:function (val) {
92
+            if(val.length >0) {
93
+                console.log(val)
94
+                this.choseDeviceIndex = 0
95
+                this.characteristicsList = this.blueDeviceList[0].characteristics
96
+            }
97
+        },
98
+        choseDeviceIndex:function (val) {
99
+            this.characteristicsList = this.blueDeviceList[val].characteristics
100
+        }
101
+    },
35 102
     methods: {
36 103
       toIndex() {
37 104
         this.$router.push('/')
105
+        console.log(this.blueDeviceList)
38 106
       },
39 107
       search() {
40 108
         this.$socket.emit('ble', {cmd: 'scan'})
@@ -44,14 +112,41 @@
44 112
       },
45 113
       send() {
46 114
         this.$socket.emit('ble', {cmd: 'send', args: {msg: '0123'}})
115
+      },
116
+      canConnect(properties) {
117
+        let bRes = false
118
+        for(let j of properties) {
119
+          if (j=='write'||j=='read'||j=='notify') {
120
+              bRes = true
121
+          }
122
+          break;
123
+        }
124
+        return bRes
125
+
126
+      },
127
+      connect(row) {
128
+          let connectArgsObj = {
129
+              id:row.id,
130
+              server_uuid:row.uuid,
131
+              characteristic_uuid : row.characteristic.uuid
132
+          }
133
+          this.$socket.emit('ble', {cmd: 'connect', args: connectArgsObj})
47 134
       }
48 135
     },
49 136
     computed: {
50 137
       ...mapGetters([
51 138
         'blueDeviceList'
52
-      ])
139
+      ]),
140
+      computedDevice :{
141
+          get:function () {
142
+
143
+
144
+          },
145
+          set:function (val) {
146
+
147
+          }
148
+      }
53 149
     },
54
-    watch: {}
55 150
   }
56 151
 </script>
57 152
 

+ 0 - 2
src/renderer/store/modules/socket.js

@@ -42,10 +42,8 @@ const mutations = {
42 42
         state.blueSendData = func.buf2hex(data)
43 43
     },
44 44
     SOCKET_blueDeviceList(state, resp) {
45
-            console.log(resp)
46 45
         if (resp.ok) {
47 46
             state.blueDeviceList = resp.data;
48
-
49 47
         } else {
50 48
             console.log(resp)
51 49
         }

+ 1 - 2
static/server/app.js

@@ -178,7 +178,7 @@ io.sockets.on('connection', function (socket) {
178 178
             let resp_cb =  function(resp) {
179 179
                 io.sockets.emit('blueConnect', resp)
180 180
             }
181
-            nobleWrapper.connect(request.args.id, on_data_cb, resp_cb);
181
+            nobleWrapper.connect(request.args, on_data_cb, resp_cb);
182 182
         } else if (request.cmd === 'send') {
183 183
             nobleWrapper.write(request.args.msg, function(resp) {
184 184
                 io.sockets.emit('blueSend', resp)
@@ -200,7 +200,6 @@ socketServer.on('connection',(socket)=>{
200 200
             connectCount = count
201 201
             io.sockets.emit('connectCount', count);
202 202
 
203
-
204 203
         }
205 204
     });
206 205
     socket.on('data', (data)=>{

+ 14 - 24
static/server/ble/nobleWrapper.js

@@ -5,17 +5,17 @@ let inRange = {};
5 5
 let blueDeviceList = []
6 6
 let blueCharacteristics = []
7 7
 
8
-const ECHO_SERVICE_UUID = 'ec00';
9
-const ECHO_CHARACTERISTIC_UUID = 'ec0e';
10 8
 
11 9
 let connectedCharacteristic = null;
12 10
 let on_data_callback = null;
13 11
 
14 12
 noble.on('discover', function (peripheral) {
13
+  if(peripheral.addressType != 'public') {
14
+     return;
15
+  }
15 16
   inRange[peripheral.id] = {
16 17
     peripheral: peripheral
17 18
   };
18
-
19 19
   var blueDevice = {
20 20
     'id': peripheral.id,
21 21
     'localName': peripheral.advertisement.localName,
@@ -25,9 +25,7 @@ noble.on('discover', function (peripheral) {
25 25
     'connectable': peripheral.connectable,
26 26
     'rssi': peripheral.rssi,
27 27
   }
28
-
29 28
   blueDeviceList.push(blueDevice)
30
-
31 29
   explore(peripheral)
32 30
 });
33 31
 
@@ -40,7 +38,6 @@ function explore(peripheral) {
40 38
           service.discoverCharacteristics([], function (error, characteristics) {
41 39
             for (let charIndex = 0; charIndex < characteristics.length; charIndex++) {
42 40
               let c = characteristics[charIndex];
43
-
44 41
               let info = {
45 42
                 id: peripheral.id,
46 43
                 uuid: service.uuid,
@@ -50,14 +47,12 @@ function explore(peripheral) {
50 47
                   'properties': c.properties
51 48
                 }
52 49
               }
53
-
54 50
               blueCharacteristics.push(info)
55 51
             }
56 52
           })
57 53
         })(services[serviceIndex])
58 54
 
59 55
       }
60
-
61 56
       setTimeout(function () {
62 57
         peripheral.disconnect()
63 58
       }, 1000)
@@ -69,26 +64,24 @@ function merge_scan_result() {
69 64
   for (let i = 0; i < blueDeviceList.length; i++) {
70 65
     let device = blueDeviceList[i]
71 66
     device.characteristics = []
72
-
73 67
     for (let j = 0; j < blueCharacteristics.length; j++) {
74 68
       let character = blueCharacteristics[j]
75
-      if (device.id === character.id) {
76
-        device.characteristics.push(character)
69
+      if (device.id == character.id) {
70
+          console.log(character)
71
+          device.characteristics.push(character)
77 72
       }
78 73
     }
79 74
   }
80
-
75
+ // console.log(blueDeviceList)
81 76
   return blueDeviceList
82 77
 }
83 78
 
84
-function connectAndSetUp(peripheral) {
85
-  peripheral.connect(error => {
79
+function connectAndSetUp(peripheral,server_uuid,characteristic_uuid) {
80
+    peripheral.connect(error => {
86 81
     console.log('Connected to', peripheral.id);
87
-
88 82
     // specify the services and characteristics to discover
89
-    const serviceUUIDs = [ECHO_SERVICE_UUID];
90
-    const characteristicUUIDs = [ECHO_CHARACTERISTIC_UUID];
91
-
83
+    const serviceUUIDs = [server_uuid];
84
+    const characteristicUUIDs = [characteristic_uuid];
92 85
     peripheral.discoverSomeServicesAndCharacteristics(
93 86
       serviceUUIDs,
94 87
       characteristicUUIDs,
@@ -118,29 +111,26 @@ function onServicesAndCharacteristicsDiscovered(error, services, characteristics
118 111
       console.log('Subscribed for echoCharacteristic notifications');
119 112
     }
120 113
   });
121
-
122 114
   connectedCharacteristic = echoCharacteristic
123 115
 }
124 116
 
125 117
 module.exports = {
126 118
   scan: function (resp_callback) {
127
-
128 119
     try {
129 120
       blueDeviceList = []
130 121
       blueCharacteristics = []
131
-
132 122
       setTimeout(() => {
133 123
         noble.stopScanning()
134 124
         resp_callback(resp.ok_resp(merge_scan_result()))
135
-      }, 2000);
125
+      }, 3000);
136 126
       noble.startScanning()
137 127
     } catch (e) {
138 128
       resp_callback(resp.fail_resp(e.message))
139 129
     }
140 130
   },
141
-  connect: function (id, on_data_cb, resp_callback) {
131
+  connect: function (args, on_data_cb, resp_callback) {
142 132
     on_data_callback = on_data_cb;
143
-    connectAndSetUp(inRange[id].peripheral)
133
+    connectAndSetUp(inRange[args.id].peripheral,args.server_uuid,args.characteristic_uuid)
144 134
   },
145 135
   write: function (msg, resp_callback) {
146 136
     try {