浏览代码

ver:0.0.5
desc:noble version
poster:张晓宇

xiaoyuzhang 5 年之前
父节点
当前提交
4c90087205

+ 0 - 1
package.json

@@ -55,7 +55,6 @@
55
   },
55
   },
56
   "dependencies": {
56
   "dependencies": {
57
     "axios": "^0.18.0",
57
     "axios": "^0.18.0",
58
-    "bleno": "^0.5.0",
59
     "bufferutil": "^4.0.1",
58
     "bufferutil": "^4.0.1",
60
     "echarts": "^4.7.0",
59
     "echarts": "^4.7.0",
61
     "element-ui": "^2.13.1",
60
     "element-ui": "^2.13.1",

+ 12 - 0
src/renderer/components/LandingPage.vue

@@ -45,6 +45,14 @@
45
 </template>
45
 </template>
46
 
46
 
47
 <script>
47
 <script>
48
+  function toArrayBuffer(buf) {
49
+      var ab = new ArrayBuffer(buf.length);
50
+      var view = new Uint8Array(ab);
51
+      for (var i = 0; i < buf.length; ++i) {
52
+          view[i] = buf[i];
53
+      }
54
+      return ab;
55
+  }
48
   import h337 from "heatmap.js";
56
   import h337 from "heatmap.js";
49
   import {mapState, mapGetters} from "vuex";
57
   import {mapState, mapGetters} from "vuex";
50
   export default {
58
   export default {
@@ -106,6 +114,10 @@
106
             });
114
             });
107
             this.h337 = heatmap;
115
             this.h337 = heatmap;
108
         },1000)
116
         },1000)
117
+//        var buf = Buffer.from('ab1c')
118
+//        console.log(buf.toString('hex'))
119
+//        console.log(toArrayBuffer(buf))
120
+
109
     },
121
     },
110
     computed:{
122
     computed:{
111
         ...mapGetters([
123
         ...mapGetters([

+ 187 - 0
src/renderer/components/ble/bleIndex.vue

@@ -0,0 +1,187 @@
1
+<template>
2
+  <div class="bluetoothContent">
3
+
4
+    <el-button @click="toIndex">返回首页</el-button>
5
+    <el-button @click="search">搜索</el-button>
6
+    <el-button @click="connect">连接</el-button>
7
+    <br/>
8
+    <div  v-loading="scanLoading" element-loading-text="searching...">
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
+      <el-table
18
+              :data="blueDeviceList"
19
+              style="width: 100%">
20
+        <el-table-column
21
+                prop="localName"
22
+                label="localName"
23
+                width="280">
24
+        </el-table-column>
25
+        <el-table-column
26
+                prop="address"
27
+                label="address"
28
+                width="280">
29
+        </el-table-column>
30
+        <el-table-column
31
+                prop="option"
32
+                label="操作">
33
+          <template slot-scope="scope">
34
+            <!--<el-button size='small' type="primary" v-on:click="goCompanyPayDetail(scope.row.company_importantinfo_id)">查看</el-button>
35
+            <el-button size='small' type="primary" v-on:click="deleteCompanyPay(scope.row.company_importantinfo_id)">删除</el-button>-->
36
+            <el-button type="default" size="mini"  @click="connect(scope.row)">connect</el-button>
37
+          </template>
38
+        </el-table-column>
39
+      </el-table>
40
+
41
+
42
+      <!--<el-table-->
43
+              <!--:data="characteristicsList"-->
44
+              <!--style="width: 100%;padding-top: 50px">-->
45
+        <!--<el-table-column-->
46
+                <!--prop="id"-->
47
+                <!--label="id"-->
48
+                <!--width="150">-->
49
+        <!--</el-table-column>-->
50
+        <!--<el-table-column-->
51
+                <!--prop="uuid"-->
52
+                <!--label="seviceUUid"-->
53
+                <!--width="90">-->
54
+        <!--</el-table-column>-->
55
+        <!--<el-table-column-->
56
+                <!--prop="name"-->
57
+                <!--label="name"-->
58
+                <!--width="130">-->
59
+          <!--<template slot-scope="scope">-->
60
+            <!--<span>{{scope.row.name == null?'Unknown Service':scope.row.name}}</span>-->
61
+          <!--</template>-->
62
+        <!--</el-table-column>-->
63
+        <!--<el-table-column-->
64
+                <!--prop="name"-->
65
+                <!--label="characteristicServiceUUid"-->
66
+                <!--width="180">-->
67
+          <!--<template slot-scope="scope">-->
68
+            <!--<span>{{scope.row.characteristic.uuid}}</span>-->
69
+          <!--</template>-->
70
+        <!--</el-table-column>-->
71
+        <!--<el-table-column-->
72
+                <!--prop="name"-->
73
+                <!--label="properties"-->
74
+                <!--width="180">-->
75
+          <!--<template slot-scope="scope">-->
76
+            <!--<span v-for="item in scope.row.characteristic.properties">{{item + '、'}}</span>-->
77
+          <!--</template>-->
78
+        <!--</el-table-column>-->
79
+        <!--<el-table-column-->
80
+                <!--prop="name"-->
81
+                <!--label="options"-->
82
+                <!--width="180">-->
83
+          <!--<template slot-scope="scope">-->
84
+            <!--<el-button  :disabled ="!canConnect(scope.row.characteristic.properties)"  @click="connect(scope.row)">connect</el-button>-->
85
+          <!--</template>-->
86
+        <!--</el-table-column>-->
87
+
88
+      <!--</el-table>-->
89
+
90
+    </div>
91
+
92
+
93
+
94
+
95
+  </div>
96
+
97
+</template>
98
+
99
+<script>
100
+
101
+  import {mapGetters} from "vuex"
102
+  export default {
103
+    name: "rt_ble",
104
+    data() {
105
+      return {
106
+        choseBlueDeviceId: undefined,
107
+        choseDeviceIndex:-1,
108
+        characteristicsList:[],
109
+        row:{}
110
+      }
111
+    },
112
+    created() {
113
+        this.search()
114
+        this.$store.dispatch('resetDisConnectBlueStatus')
115
+    },
116
+    watch:{
117
+        blueDeviceList:function (val) {
118
+            if(val.length >0) {
119
+                console.log(val)
120
+                this.choseDeviceIndex = 0
121
+                this.characteristicsList = this.blueDeviceList[0].characteristics
122
+            }
123
+        },
124
+        choseDeviceIndex:function (val) {
125
+            this.characteristicsList = this.blueDeviceList[val].characteristics
126
+        },
127
+        blueDeviceConnectStatus:function (val) {
128
+            if(val == 1) {
129
+                this.successNotify('成功连接蓝牙设备!')
130
+                this.$router.push({ path: '/char_server', query: {localName:this.row.localName}})
131
+            } else {
132
+                this.errorNotify('连接蓝牙设备失败!')
133
+            }
134
+        }
135
+    },
136
+    methods: {
137
+      toIndex() {
138
+          this.$router.push('/')
139
+      },
140
+      search() {
141
+          this.$socket.emit('ble', {cmd: 'scan'})
142
+          this.$store.dispatch('resetBlueDeviceList')
143
+      },
144
+      connect(row) {
145
+         //  console.log(row)
146
+          let  args = {
147
+              id: row.id,
148
+          }
149
+          this.row = row
150
+          this.$socket.emit('ble', {cmd: 'connectDevice', args: args})
151
+      },
152
+      errorNotify(msg) {
153
+          this.$notify({
154
+              title: '提示',
155
+              message: msg,
156
+              duration: 4500
157
+          });
158
+      },
159
+      successNotify(successMessage) {
160
+          this.$notify({
161
+              title: '提示',
162
+              message: successMessage,
163
+              duration: 3500
164
+          });
165
+      },
166
+//      connect(row) {
167
+//          let connectArgsObj = {
168
+//              id:row.id,
169
+//              server_uuid:row.uuid,
170
+//              characteristic_uuid : row.characteristic.uuid
171
+//          }
172
+//          this.$socket.emit('ble', {cmd: 'connect', args: connectArgsObj})
173
+//      }
174
+    },
175
+    computed: {
176
+      ...mapGetters([
177
+        'blueDeviceList',
178
+        'scanLoading',
179
+        'blueDeviceConnectStatus'
180
+      ]),
181
+    },
182
+  }
183
+</script>
184
+
185
+<style scoped>
186
+
187
+</style>

+ 236 - 0
src/renderer/components/ble/characteristicsServers.vue

@@ -0,0 +1,236 @@
1
+<template>
2
+  <div class="bluetoothContent">
3
+    <el-popover
4
+            placement="top"
5
+            trigger="click"
6
+            :ref="`popover`"
7
+            width="160"
8
+           >
9
+      <p >返回会断开蓝牙连接请确认</p>
10
+      <div style="text-align: right; margin: 0">
11
+        <el-button size="mini" type="text" @click="cancel">取消</el-button>
12
+        <el-button type="primary" size="mini" @click="confirmDisConnect" >确定</el-button>
13
+      </div>
14
+      <el-button   slot="reference"  >返回蓝牙首页</el-button>
15
+    </el-popover>
16
+    {{localName}}
17
+    <br/>
18
+      <el-table
19
+              :data="characteristicServiceList"
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?'Unknown Service':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="writeAndRead(scope.row)">write/read</el-button>
61
+          </template>
62
+        </el-table-column>
63
+      </el-table>
64
+
65
+    <el-dialog title="添加小程序模板" width="80%"
66
+               :close-on-click-modal="false"
67
+               :visible.sync="validate"
68
+               :modal-append-to-body="false"
69
+               :append-to-body="true"
70
+               :show-close="false"
71
+    >
72
+      <div style="text-align: center">
73
+        <div class="head_tip">
74
+        </div>
75
+        <div style="padding-bottom: 20px">
76
+            <!--发送数据:-->
77
+          <!--<el-input v-model="sendData"></el-input> <br/>-->
78
+          <el-button @click="startSetZero">开始取零</el-button>
79
+          <el-button @click="stopSetZero">停止取零</el-button>
80
+          <el-button @click="startCollection">开始采集</el-button>
81
+          <el-button @click="stopCollection">停止采集</el-button>
82
+          <el-button @click="resetDevice">复位</el-button>
83
+        </div>
84
+
85
+        <div style="padding-bottom: 20px">
86
+          <div style="width:140px;height:140px;position:relative;margin-top:30px" >
87
+            <div ref="heatsample" style="width:540px;height:540px;position:absolute;top:0;left:0" />
88
+            <chart id="heatmap" style="width:540px;height:540px;position:absolute;top:0"  ref="heatmap"></chart>
89
+          </div>
90
+        </div>
91
+      </div>
92
+      <div slot="footer" class="dialog-footer">
93
+        <el-button @click="closeDialog">关闭dialog</el-button>
94
+      </div>
95
+    </el-dialog>
96
+
97
+
98
+  </div>
99
+
100
+</template>
101
+
102
+<script>
103
+  import h337 from "heatmap.js";
104
+  import {mapGetters} from "vuex"
105
+  export default {
106
+    name: "char_server",
107
+    data() {
108
+      return {
109
+          localName:typeof (this.$route.query.localName) =='undefined' ?'N/A':this.$route.query.localName,
110
+          validate:false,
111
+          sendData:'',
112
+      }
113
+    },
114
+    created() {
115
+        // 获取characteristicServer List
116
+        this.getCharacteristicServerList()
117
+        this.$store.dispatch('resetCharacteristicServerList')
118
+
119
+    },
120
+    watch:{
121
+        blueDeviceDisConnectStatus:function (val) {
122
+            if(val == 1) {
123
+                this.$router.push('/rt_ble')
124
+            } else {
125
+                this.errorNotify('关闭蓝牙设备连接失败!')
126
+            }
127
+        },
128
+        pressureData:function (val) {
129
+            if(this.h337 != null) {
130
+                this.h337.setData({
131
+                    max: 255,
132
+                    data: val
133
+                });
134
+            }
135
+        },
136
+    },
137
+    methods: {
138
+        getCharacteristicServerList() {
139
+            this.$socket.emit('ble', {cmd: 'getCharServiceList'})
140
+        },
141
+        cancel() {
142
+            this.$refs[`popover`].doClose()
143
+        },
144
+        confirmDisConnect() {
145
+            this.$socket.emit('ble', {cmd: 'disConnectDevice'})
146
+            this.$router.push('/rt_ble')
147
+            this.$store.dispatch('resetConnectBlueStatus')
148
+          //  this.$refs[`popover`].doClose()
149
+        },
150
+
151
+        errorNotify(msg) {
152
+            this.$notify({
153
+                title: '提示',
154
+                message: msg,
155
+                duration: 4500
156
+            });
157
+        },
158
+        successNotify(successMessage) {
159
+            this.$notify({
160
+                title: '提示',
161
+                message: successMessage,
162
+                duration: 3500
163
+            });
164
+        },
165
+        canConnect(properties) {
166
+            let bRes = false
167
+            let flag = -1
168
+            for(let j of properties) {
169
+                if (j=='write'||j=='read') {
170
+                    flag ++
171
+                }
172
+            }
173
+            if(flag == 1) {
174
+                bRes = true
175
+            }
176
+            return bRes
177
+        },
178
+        writeAndRead(row) {
179
+            this.$socket.emit('ble', {cmd: 'setup',data:row})
180
+            this.validate = true
181
+            this.$store.dispatch('resetBlueToothData')
182
+            if(this.h337 !=null) {
183
+                this.h337.setData({
184
+                    max: 255,
185
+                    data: []
186
+                });
187
+            }
188
+            setTimeout(()=>{
189
+                var gradient = require('../../assets/heatmap_gradient.json')
190
+                var dst = this.$refs.heatsample;
191
+                var heatmap = h337.create({
192
+                    container: dst,
193
+                    radius:6,
194
+                    blur:0.7,
195
+                    gradient: gradient
196
+                });
197
+                this.h337 = heatmap;
198
+            },1000)
199
+        },
200
+        closeDialog() {
201
+            this.$socket.emit('ble', {cmd: 'closeSetUp'})
202
+            this.validate = false
203
+        },
204
+        startSetZero(){
205
+            this.$socket.emit('sendBleData', {type: 'setZero'})
206
+        },
207
+        stopSetZero() {
208
+            this.$socket.emit('sendBleData', {type: 'stopSetZero'})
209
+        },
210
+        startCollection() {
211
+            this.$socket.emit('sendBleData', {type: 'startCollection'})
212
+        },
213
+        stopCollection() {
214
+            this.$socket.emit('sendBleData', {type: 'stopCollection'})
215
+        },
216
+        resetDevice() {
217
+            this.$socket.emit('sendBleData', {type: 'resetDevice'})
218
+        }
219
+
220
+
221
+    },
222
+    computed: {
223
+      ...mapGetters([
224
+        'blueDeviceList',
225
+        'scanLoading',
226
+        'blueDeviceDisConnectStatus',
227
+        'characteristicServiceList',
228
+        'pressureData'
229
+      ]),
230
+    },
231
+  }
232
+</script>
233
+
234
+<style scoped>
235
+
236
+</style>

+ 0 - 155
src/renderer/components/ble/rt_ble.vue

@@ -1,155 +0,0 @@
1
-<template>
2
-  <div class="bluetoothContent">
3
-
4
-    <el-button @click="toIndex">返回首页</el-button>
5
-    <el-button @click="search">搜索</el-button>
6
-    <el-button @click="connect">连接</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>
65
-
66
-    </div>
67
-
68
-
69
-
70
-
71
-  </div>
72
-
73
-</template>
74
-
75
-<script>
76
-
77
-  import {mapGetters} from "vuex"
78
-  export default {
79
-    name: "rt_ble",
80
-    data() {
81
-      return {
82
-        choseBlueDeviceId: undefined,
83
-        choseDeviceIndex:-1,
84
-        characteristicsList:[],
85
-      }
86
-    },
87
-    created() {
88
-      this.search()
89
-    },
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
-    },
102
-    methods: {
103
-      toIndex() {
104
-        this.$router.push('/')
105
-        console.log(this.blueDeviceList)
106
-      },
107
-      search() {
108
-        this.$socket.emit('ble', {cmd: 'scan'})
109
-      },
110
-      connect() {
111
-        this.$socket.emit('ble', {cmd: 'connect', args: {id: this.choseBlueDeviceId}})
112
-      },
113
-      send() {
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})
134
-      }
135
-    },
136
-    computed: {
137
-      ...mapGetters([
138
-        'blueDeviceList'
139
-      ]),
140
-      computedDevice :{
141
-          get:function () {
142
-
143
-
144
-          },
145
-          set:function (val) {
146
-
147
-          }
148
-      }
149
-    },
150
-  }
151
-</script>
152
-
153
-<style scoped>
154
-
155
-</style>

+ 6 - 1
src/renderer/router/index.js

@@ -27,7 +27,12 @@ export default new Router({
27
     {
27
     {
28
         path: '/rt_ble',
28
         path: '/rt_ble',
29
         name: 'rt_ble',
29
         name: 'rt_ble',
30
-        component: require('@/components/ble/rt_ble').default
30
+        component: require('@/components/ble/bleIndex').default
31
+    },
32
+    {
33
+        path: '/char_server',
34
+        name: 'char_server',
35
+        component: require('@/components/ble/characteristicsServers').default
31
     },
36
     },
32
 
37
 
33
   ]
38
   ]

+ 68 - 5
src/renderer/store/modules/socket.js

@@ -6,12 +6,33 @@ const state = {
6
     blueToothDevice:'',
6
     blueToothDevice:'',
7
     deviceStatus:'',
7
     deviceStatus:'',
8
     blueSendData:'',
8
     blueSendData:'',
9
+    blueDeviceList: [],
10
+    scanLoading:false,
11
+    blueDeviceConnectStatus:-1,  //蓝牙连接状态  1 成功 0失败 -1初始值
12
+    blueDeviceDisConnectStatus:-1,  //蓝牙断开状态  1 成功 0失败 -1初始值
13
+    characteristicServiceList:[],
9
 
14
 
10
-    blueDeviceList: []
11
 }
15
 }
12
 
16
 
13
 const actions = {
17
 const actions = {
14
-
18
+    resetBlueDeviceList({commit}) {
19
+        commit('resetBlueDeviceList')
20
+    },
21
+    resetConnectBlueStatus({commit}) {
22
+        commit('resetConnectBlueStatus')
23
+    },
24
+    resetDisConnectBlueStatus({commit}){
25
+        commit('resetDisConnectBlueStatus')
26
+    },
27
+    resetCharacteristicServiceList({commit}){
28
+        commit('resetDisConnectBlueStatus')
29
+    },
30
+    resetCharacteristicServerList({commit}) {
31
+        commit('resetCharacteristicServerList')
32
+    },
33
+    resetBlueToothData({commit}) {
34
+        commit('resetBlueToothData')
35
+    }
15
 
36
 
16
 }
37
 }
17
 const mutations = {
38
 const mutations = {
@@ -42,28 +63,70 @@ const mutations = {
42
         state.blueSendData = func.buf2hex(data)
63
         state.blueSendData = func.buf2hex(data)
43
     },
64
     },
44
     SOCKET_blueDeviceList(state, resp) {
65
     SOCKET_blueDeviceList(state, resp) {
66
+        state.scanLoading = false
45
         if (resp.ok) {
67
         if (resp.ok) {
46
             state.blueDeviceList = resp.data;
68
             state.blueDeviceList = resp.data;
47
         } else {
69
         } else {
48
             console.log(resp)
70
             console.log(resp)
49
         }
71
         }
72
+    },
73
+    // 连接蓝牙设备返回
74
+    SOCKET_connectDeviceBack(state, resp) {
75
+        if (resp.ok) {
76
+            state.blueDeviceConnectStatus = 1
77
+        } else {
78
+            state.blueDeviceConnectStatus = 0
79
+        }
80
+    },
81
+    // 断开蓝牙设备返回
82
+    SOCKET_disConnectDeviceBack(state, resp) {
83
+        if (resp.ok) {
84
+            state.blueDeviceDisConnectStatus = 1
85
+        } else {
86
+            state.blueDeviceDisConnectStatus = 0
87
+        }
88
+    },
89
+    SOCKET_pushCharacteristicServerList(state,info) {
90
+        state.characteristicServiceList.push(info)
91
+    },
92
+    resetBlueDeviceList(state) {
93
+        state.blueDeviceList = []
94
+        state.scanLoading = true
95
+    },
96
+    // 重置蓝牙连接转态 (蓝牙服务页面返回蓝牙index 页面调用)
97
+    resetConnectBlueStatus(state) {
98
+        state.blueDeviceConnectStatus = -1
99
+    },
100
+    // 重置断开蓝牙连接转态 (蓝牙index页面加载时调用
101
+    resetDisConnectBlueStatus(state) {
102
+        state.blueDeviceDisConnectStatus = -1
103
+    },
104
+    // 重置characteristicServerList
105
+    resetCharacteristicServerList(state) {
106
+        state.characteristicServiceList = []
107
+    },
108
+    resetBlueToothData(state) {
109
+        state.blueToothData = ''
50
     }
110
     }
51
 }
111
 }
52
 
112
 
53
 const getters = {
113
 const getters = {
114
+    blueDeviceConnectStatus:state=>state.blueDeviceConnectStatus,
115
+    blueDeviceDisConnectStatus:state=>state.blueDeviceDisConnectStatus,
54
     hasConnectCount:state=>state.hasConnectCount,
116
     hasConnectCount:state=>state.hasConnectCount,
55
     pressureData:state=>state.pressureData,
117
     pressureData:state=>state.pressureData,
56
     blueToothDevice:state=>state.blueToothDevice,
118
     blueToothDevice:state=>state.blueToothDevice,
57
     deviceStatus:state=>state.deviceStatus,
119
     deviceStatus:state=>state.deviceStatus,
58
     blueSendData:state=>state.blueSendData,
120
     blueSendData:state=>state.blueSendData,
59
-
121
+    scanLoading:state=>state.scanLoading,
60
     blueDeviceList:state=>state.blueDeviceList,
122
     blueDeviceList:state=>state.blueDeviceList,
123
+    characteristicServiceList:state=>state.characteristicServiceList,
61
 }
124
 }
62
 
125
 
63
 
126
 
64
-export default {
127
+    export default {
65
     state,
128
     state,
66
     actions,
129
     actions,
67
     mutations,
130
     mutations,
68
     getters,
131
     getters,
69
-}
132
+    }

+ 10 - 1
src/renderer/utils/FunctionSet.js

@@ -56,7 +56,6 @@ module.exports = {
56
             // push to array
56
             // push to array
57
             hexParts.push(paddedHex);
57
             hexParts.push(paddedHex);
58
         }
58
         }
59
-
60
         // join all the hex values of the elements into a single string
59
         // join all the hex values of the elements into a single string
61
         return hexParts.join('');
60
         return hexParts.join('');
62
     },
61
     },
@@ -69,6 +68,16 @@ module.exports = {
69
             hexCharCode.push((str.charCodeAt(i)).toString(16));
68
             hexCharCode.push((str.charCodeAt(i)).toString(16));
70
         }
69
         }
71
         return hexCharCode;
70
         return hexCharCode;
71
+    },
72
+    // array 转string
73
+    ab2Ascii(buffer) {
74
+        var str = Array.prototype.map.call(
75
+            new Uint8Array(buffer),
76
+            function (bit) {
77
+                return String.fromCharCode(bit);
78
+            }
79
+        )
80
+        return str.join('');
72
     }
81
     }
73
 
82
 
74
 }
83
 }

+ 97 - 15
static/server/app.js

@@ -19,6 +19,9 @@ let socketEvent = null;
19
 var connectCount = 0;
19
 var connectCount = 0;
20
 var lastBuffer = null;
20
 var lastBuffer = null;
21
 
21
 
22
+var nowConnectedBlueDeviceId = ''
23
+
24
+
22
 io.sockets.on('connection', function (socket) {
25
 io.sockets.on('connection', function (socket) {
23
     // 取零
26
     // 取零
24
     socket.on('startSetZero', function () {
27
     socket.on('startSetZero', function () {
@@ -161,31 +164,112 @@ io.sockets.on('connection', function (socket) {
161
     })
164
     })
162
     // 蓝牙获取wifi账号密码
165
     // 蓝牙获取wifi账号密码
163
     socket.on('getServerIp',function (sendData) {
166
     socket.on('getServerIp',function (sendData) {
164
-        let data = sendProtocal.blueToothSendCommand(
165
-            sendData,'serverIp','get')
167
+        let data = sendProtocal.blueToothSendCommand(sendData,'serverIp','get')
166
         blueTouth.sendData(data)
168
         blueTouth.sendData(data)
167
     })
169
     })
168
-
169
     socket.on('ble', function(request) {
170
     socket.on('ble', function(request) {
170
         if (request.cmd === 'scan') {
171
         if (request.cmd === 'scan') {
171
             nobleWrapper.scan(function(resp) {
172
             nobleWrapper.scan(function(resp) {
172
                 io.sockets.emit('blueDeviceList', resp)
173
                 io.sockets.emit('blueDeviceList', resp)
173
             })
174
             })
174
-        } else if (request.cmd === 'connect') {
175
-            let on_data_cb  = function (buffer) {
176
-                console.log('Received: "' + buffer.length + '"');
175
+        }  else if(request.cmd === 'connectDevice') {
176
+            // 连接蓝牙设备
177
+            request.args.on_data_cb =  function (data) {
178
+                // 接收数据!
179
+                if (lastBuffer !== null) {
180
+                    data = Buffer.concat([lastBuffer, data]);
181
+                }
182
+                let flag = 1;
183
+                while(flag){
184
+                    if (data.length < frame_header_and_size_bytes) {
185
+                        lastBuffer = data;
186
+                        break;
187
+                    }
188
+                    let dataLength = data.readUIntLE(1, 2)
189
+                    let frameLength = frame_header_and_size_bytes + dataLength
190
+
191
+                    if(data.length > frameLength) {
192
+                        let frame = data.slice(0, frameLength);
193
+                        setAllCommand(frame)
194
+                        lastBuffer = data.slice(frameLength)
195
+
196
+                        if(lastBuffer.length >= frame_header_and_size_bytes) {
197
+                            data = lastBuffer
198
+                            continue;
199
+                        }
200
+                    } else if(data.length === frameLength) {
201
+                        setAllCommand(data)
202
+                        lastBuffer = null;
203
+                    } else {
204
+                        lastBuffer = data;
205
+                    }
206
+                    flag = 0;
207
+                }
208
+                // io.sockets.emit('blueToothData', buffer)
209
+            }
210
+            nobleWrapper.connectDevice(request.args,function(resp) {
211
+                io.sockets.emit('connectDeviceBack', resp)
212
+                if(resp.ok) {
213
+                    nowConnectedBlueDeviceId  = request.args.id
214
+                } else {
215
+                    if(resp.msg == 'connectError') {
216
+                        // 连接错误
217
+                        console.log('server connectError')
218
+                    }
219
+                }
220
+                //io.sockets.emit('blueDeviceList', resp)
221
+            })
222
+           // nobleWrapper.connectDevice(request.args, request.args.successCallBack, request.args.errorCallBack);
223
+        } else if (request.cmd ==='disConnectDevice') {
224
+            // 断开蓝牙设备
225
+            if(nowConnectedBlueDeviceId !='') {
226
+                nobleWrapper.disConnectDevice(nowConnectedBlueDeviceId,function (resp) {
227
+                    io.sockets.emit('disConnectDeviceBack', resp)
228
+                })
229
+            }
230
+        } else if(request.cmd ==='getCharServiceList') {
231
+            // 获取CharServiceList
232
+            if(nowConnectedBlueDeviceId !='') {
233
+                nobleWrapper.getCharList(nowConnectedBlueDeviceId,function (info) {
234
+                    io.sockets.emit('pushCharacteristicServerList', info)
235
+                })
177
             }
236
             }
178
-            let resp_cb =  function(resp) {
179
-                io.sockets.emit('blueConnect', resp)
237
+        } else if(request.cmd == 'setup') {
238
+            if(nowConnectedBlueDeviceId !='') {
239
+                nobleWrapper.setUp(request.data)
180
             }
240
             }
181
-            nobleWrapper.connect(request.args, on_data_cb, resp_cb);
182
-        } else if (request.cmd === 'send') {
183
-            nobleWrapper.write(request.args.msg, function(resp) {
184
-                io.sockets.emit('blueSend', resp)
185
-            });
241
+        } else if(request.cmd == 'closeSetUp') {
242
+            // 用户关闭dialog  并把CharacteristicServer 取消订阅 unsubscribe
243
+            console.log('close')
244
+            nobleWrapper.disSetUp()
186
         }
245
         }
187
     });
246
     });
188
 
247
 
248
+    socket.on('sendBleData',function (data) {
249
+        let sendData = ''
250
+        switch (data.type) {
251
+            case 'setZero' :
252
+                sendData = sendProtocal.beginSetZero()
253
+                break;
254
+            case 'stopSetZero':
255
+                sendData = sendProtocal.endSetZero()
256
+                break;
257
+            case 'startCollection':
258
+                sendData = sendProtocal.startCollection()
259
+                break;
260
+            case 'stopCollection':
261
+                sendData = sendProtocal.endCollection()
262
+                break;
263
+            case 'resetDevice':
264
+                sendData = sendProtocal.resetDevice()
265
+                break;
266
+        }
267
+        nobleWrapper.write(sendData,function (res) {
268
+            console.log(res)
269
+        })
270
+    })
271
+
272
+
189
 })
273
 })
190
 
274
 
191
 const frame_header_and_size_bytes = 1 + 2;
275
 const frame_header_and_size_bytes = 1 + 2;
@@ -206,14 +290,12 @@ socketServer.on('connection',(socket)=>{
206
         if (lastBuffer !== null) {
290
         if (lastBuffer !== null) {
207
             data = Buffer.concat([lastBuffer, data]);
291
             data = Buffer.concat([lastBuffer, data]);
208
         }
292
         }
209
-
210
         let flag = 1;
293
         let flag = 1;
211
         while(flag){
294
         while(flag){
212
             if (data.length < frame_header_and_size_bytes) {
295
             if (data.length < frame_header_and_size_bytes) {
213
                 lastBuffer = data;
296
                 lastBuffer = data;
214
                 break;
297
                 break;
215
             }
298
             }
216
-
217
             let dataLength = data.readUIntLE(1, 2)
299
             let dataLength = data.readUIntLE(1, 2)
218
             let frameLength = frame_header_and_size_bytes + dataLength
300
             let frameLength = frame_header_and_size_bytes + dataLength
219
 
301
 

+ 93 - 89
static/server/ble/nobleWrapper.js

@@ -26,92 +26,30 @@ noble.on('discover', function (peripheral) {
26
     'rssi': peripheral.rssi,
26
     'rssi': peripheral.rssi,
27
   }
27
   }
28
   blueDeviceList.push(blueDevice)
28
   blueDeviceList.push(blueDevice)
29
-  explore(peripheral)
30
-});
31
-
32
-
33
-function explore(peripheral) {
34
-  peripheral.connect(error => {
35
-    peripheral.discoverServices([], function (error, services) {
36
-      for (let serviceIndex = 0; serviceIndex < services.length; serviceIndex++) {
37
-        (function (service) {
38
-          service.discoverCharacteristics([], function (error, characteristics) {
39
-            for (let charIndex = 0; charIndex < characteristics.length; charIndex++) {
40
-              let c = characteristics[charIndex];
41
-              let info = {
42
-                id: peripheral.id,
43
-                uuid: service.uuid,
44
-                name: service.name,
45
-                characteristic: {
46
-                  'uuid': c.uuid,
47
-                  'properties': c.properties
48
-                }
49
-              }
50
-              blueCharacteristics.push(info)
51
-            }
52
-          })
53
-        })(services[serviceIndex])
54
 
29
 
55
-      }
56
-      setTimeout(function () {
57
-        peripheral.disconnect()
58
-      }, 1000)
59
-    })
60
-  })
61
-}
62
-
63
-function merge_scan_result() {
64
-  for (let i = 0; i < blueDeviceList.length; i++) {
65
-    let device = blueDeviceList[i]
66
-    device.characteristics = []
67
-    for (let j = 0; j < blueCharacteristics.length; j++) {
68
-      let character = blueCharacteristics[j]
69
-      if (device.id == character.id) {
70
-          console.log(character)
71
-          device.characteristics.push(character)
72
-      }
73
-    }
74
-  }
75
- // console.log(blueDeviceList)
76
-  return blueDeviceList
77
-}
78
-
79
-function connectAndSetUp(peripheral,server_uuid,characteristic_uuid) {
80
-    peripheral.connect(error => {
81
-    console.log('Connected to', peripheral.id);
82
-    // specify the services and characteristics to discover
83
-    const serviceUUIDs = [server_uuid];
84
-    const characteristicUUIDs = [characteristic_uuid];
85
-    peripheral.discoverSomeServicesAndCharacteristics(
86
-      serviceUUIDs,
87
-      characteristicUUIDs,
88
-      onServicesAndCharacteristicsDiscovered
89
-    );
90
-  });
30
+});
91
 
31
 
92
-  peripheral.on('disconnect', () => console.log('disconnected'));
93
-}
94
 
32
 
95
 function onServicesAndCharacteristicsDiscovered(error, services, characteristics) {
33
 function onServicesAndCharacteristicsDiscovered(error, services, characteristics) {
96
-  console.log('Discovered services and characteristics');
97
-  const echoCharacteristic = characteristics[0];
98
-
99
-  // data callback receives notifications
100
-  echoCharacteristic.on('data', (data, isNotification) => {
101
-    if (on_data_callback) {
102
-      on_data_callback(data)
103
-    }
104
-  });
105
-
106
-  // subscribe to be notified whenever the peripheral update the characteristic
107
-  echoCharacteristic.subscribe(error => {
108
-    if (error) {
109
-      console.error('Error subscribing to echoCharacteristic');
110
-    } else {
111
-      console.log('Subscribed for echoCharacteristic notifications');
34
+    if(connectedCharacteristic ==null) {
35
+        console.log('Discovered services and characteristics');
36
+        const echoCharacteristic = characteristics[0];
37
+        // data callback receives notifications
38
+        echoCharacteristic.on('data', (data, isNotification) => {
39
+            if (on_data_callback) {
40
+                on_data_callback(data)
41
+            }
42
+        });
43
+        // subscribe to be notified whenever the peripheral update the characteristic
44
+        echoCharacteristic.subscribe(error => {
45
+            if (error) {
46
+                console.error('Error subscribing to echoCharacteristic');
47
+            } else {
48
+                console.log('Subscribed for echoCharacteristic notifications');
49
+            }
50
+        });
51
+        connectedCharacteristic = echoCharacteristic
112
     }
52
     }
113
-  });
114
-  connectedCharacteristic = echoCharacteristic
115
 }
53
 }
116
 
54
 
117
 module.exports = {
55
 module.exports = {
@@ -121,23 +59,89 @@ module.exports = {
121
       blueCharacteristics = []
59
       blueCharacteristics = []
122
       setTimeout(() => {
60
       setTimeout(() => {
123
         noble.stopScanning()
61
         noble.stopScanning()
124
-        resp_callback(resp.ok_resp(merge_scan_result()))
62
+        resp_callback(resp.ok_resp(blueDeviceList))
125
       }, 3000);
63
       }, 3000);
126
       noble.startScanning()
64
       noble.startScanning()
127
     } catch (e) {
65
     } catch (e) {
128
       resp_callback(resp.fail_resp(e.message))
66
       resp_callback(resp.fail_resp(e.message))
129
     }
67
     }
130
   },
68
   },
131
-  connect: function (args, on_data_cb, resp_callback) {
132
-    on_data_callback = on_data_cb;
133
-    connectAndSetUp(inRange[args.id].peripheral,args.server_uuid,args.characteristic_uuid)
69
+  // 连接蓝牙设备
70
+  connectDevice:function (args,callBack) {
71
+      let peripheral = inRange[args.id].peripheral
72
+      on_data_callback = args.on_data_cb;
73
+      peripheral.connect(error => {
74
+          if(error == null) {
75
+              callBack(resp.ok_resp('success'))
76
+          } else {
77
+              callBack(resp.fail_resp('error'))
78
+          }
79
+      })
80
+      peripheral.on('disconnect', () => {
81
+            console.log('disconnect')
82
+          // callBack(resp.fail_resp('connectError'))
83
+      });
84
+  },
85
+  // 断开蓝牙设备
86
+  disConnectDevice:function (id,callBack) {
87
+      let peripheral = inRange[id].peripheral
88
+      peripheral.disconnect(error=>{
89
+          if(error == null) {
90
+            callBack(resp.ok_resp('success'))
91
+          } else {
92
+            callBack(resp.fail_resp('error'))
93
+          }
94
+      })
95
+  },
96
+  getCharList:function (id,callBack) {
97
+      let peripheral = inRange[id].peripheral
98
+      peripheral.discoverServices([], function (error, services) {
99
+          for (let serviceIndex = 0; serviceIndex < services.length; serviceIndex++) {
100
+              (function (service) {
101
+                  service.discoverCharacteristics([], function (error, characteristics) {
102
+                      for (let charIndex = 0; charIndex < characteristics.length; charIndex++) {
103
+                          let c = characteristics[charIndex];
104
+                          let info = {
105
+                              id: peripheral.id,
106
+                              uuid: service.uuid,
107
+                              name: service.name,
108
+                              characteristic: {
109
+                                  'uuid': c.uuid,
110
+                                  'properties': c.properties
111
+                              }
112
+                          }
113
+                          callBack(info)
114
+                      }
115
+                  })
116
+              })(services[serviceIndex])
117
+          }
118
+      })
134
   },
119
   },
120
+  setUp:function (row) {
121
+      const serviceUUIDs = [row.uuid];
122
+      const characteristicUUIDs = [row.characteristic.uuid];
123
+      let peripheral = inRange[row.id].peripheral
124
+      peripheral.discoverSomeServicesAndCharacteristics(
125
+          serviceUUIDs,
126
+          characteristicUUIDs,
127
+          onServicesAndCharacteristicsDiscovered
128
+      );
129
+  },
130
+  disSetUp:function () {
131
+      if(connectedCharacteristic != null) {
132
+          connectedCharacteristic.unsubscribe(error =>{
133
+              console.log(error)
134
+              if(error ==null) {
135
+                  console.log('unsubscribe,success!')
136
+                  connectedCharacteristic = null
137
+              }
138
+          })
139
+      }
140
+  },
141
+
135
   write: function (msg, resp_callback) {
142
   write: function (msg, resp_callback) {
136
     try {
143
     try {
137
-      const buffer = new Buffer(msg, 'utf-8');
138
-      if (connectedCharacteristic) {
139
-        connectedCharacteristic.write(buffer);
140
-      }
144
+        connectedCharacteristic.write(msg);
141
     } catch (e) {
145
     } catch (e) {
142
       resp_callback(resp.fail_resp(e.message))
146
       resp_callback(resp.fail_resp(e.message))
143
     }
147
     }

+ 11 - 1
static/server/protocal/commonFunction.js

@@ -35,8 +35,18 @@ module.exports = {
35
                 }
35
                 }
36
             }
36
             }
37
         }
37
         }
38
-        return pressureDataProcess(dataAll,2.5,4)
38
+        return pressureDataProcess(dataAll,1,4)
39
     },
39
     },
40
+    // arrayBuffer 转string
41
+    ab2Ascii(buffer) {
42
+        var str = Array.prototype.map.call(
43
+            new Uint8Array(buffer),
44
+            function (bit) {
45
+                return String.fromCharCode(bit);
46
+            }
47
+        )
48
+        return str.join('');
49
+    }
40
 }
50
 }
41
 
51
 
42
 function pressureDataProcess(a,scale,size) {
52
 function pressureDataProcess(a,scale,size) {