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
  * that provide pure *.vue files that need compiling
19
  * that provide pure *.vue files that need compiling
20
  * https://simulatedgreg.gitbooks.io/electron-vue/content/en/webpack-configurations.html#white-listing-externals
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
 let rendererConfig = {
24
 let rendererConfig = {
25
   devtool: '#cheap-module-eval-source-map',
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
     <el-button @click="search">搜索</el-button>
5
     <el-button @click="search">搜索</el-button>
6
     <el-button @click="connect">连接</el-button>
6
     <el-button @click="connect">连接</el-button>
7
     <el-button @click="send">发送</el-button>
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
     </div>
66
     </div>
15
 
67
 
16
 
68
 
69
+
70
+
17
   </div>
71
   </div>
18
 
72
 
19
 </template>
73
 </template>
20
 
74
 
21
 <script>
75
 <script>
22
-  import {mapGetters} from "vuex"
23
 
76
 
77
+  import {mapGetters} from "vuex"
24
   export default {
78
   export default {
25
     name: "rt_ble",
79
     name: "rt_ble",
26
     data() {
80
     data() {
27
       return {
81
       return {
28
-        choseBlueDeviceId: undefined
82
+        choseBlueDeviceId: undefined,
83
+        choseDeviceIndex:-1,
84
+        characteristicsList:[],
29
       }
85
       }
30
     },
86
     },
31
     created() {
87
     created() {
32
       this.search()
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
     methods: {
102
     methods: {
36
       toIndex() {
103
       toIndex() {
37
         this.$router.push('/')
104
         this.$router.push('/')
105
+        console.log(this.blueDeviceList)
38
       },
106
       },
39
       search() {
107
       search() {
40
         this.$socket.emit('ble', {cmd: 'scan'})
108
         this.$socket.emit('ble', {cmd: 'scan'})
@@ -44,14 +112,41 @@
44
       },
112
       },
45
       send() {
113
       send() {
46
         this.$socket.emit('ble', {cmd: 'send', args: {msg: '0123'}})
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
     computed: {
136
     computed: {
50
       ...mapGetters([
137
       ...mapGetters([
51
         'blueDeviceList'
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
 </script>
151
 </script>
57
 
152
 

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

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

+ 1 - 2
static/server/app.js

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

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

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