Sfoglia il codice sorgente

ver:noble ECHO demo

xiaoyuzhang 4 anni fa
parent
commit
049dbb642a

File diff suppressed because it is too large
+ 4704 - 5254
package-lock.json


+ 4 - 2
package.json

@@ -1,5 +1,5 @@
1 1
 {
2
-  "name": "zxy",
2
+  "name": "elctronDemo",
3 3
   "version": "0.0.4",
4 4
   "author": "xiaoyuzhang <771799477@qq.com>",
5 5
   "description": "An electron-vue project",
@@ -17,6 +17,7 @@
17 17
     "pack:renderer": "cross-env NODE_ENV=production webpack --progress --colors --config .electron-vue/webpack.renderer.config.js",
18 18
     "test": "npm run unit && npm run e2e",
19 19
     "unit": "karma start test/unit/karma.conf.js",
20
+    "rebuild": ".\\node_modules\\.bin\\electron-rebuild.cmd",
20 21
     "postinstall": ""
21 22
   },
22 23
   "build": {
@@ -55,11 +56,12 @@
55 56
   },
56 57
   "dependencies": {
57 58
     "axios": "^0.18.0",
58
-    "bleno": "^0.5.0",
59 59
     "bufferutil": "^4.0.1",
60 60
     "echarts": "^4.7.0",
61 61
     "element-ui": "^2.13.1",
62 62
     "koa": "^2.2.0",
63
+    "noble": "^1.9.1",
64
+    "node-bluetooth": "^1.2.6",
63 65
     "socket.io": "^2.3.0",
64 66
     "vue": "^2.5.16",
65 67
     "vue-echarts": "^5.0.0-beta.0",

+ 1 - 1
src/index.ejs

@@ -2,7 +2,7 @@
2 2
 <html>
3 3
   <head>
4 4
     <meta charset="utf-8">
5
-    <title>zxy</title>
5
+    <title>electronDemo</title>
6 6
     <% if (htmlWebpackPlugin.options.nodeModules) { %>
7 7
       <!-- Add `node_modules/` to global paths so `require` works properly in development -->
8 8
       <script>

+ 1 - 1
src/renderer/components/BlueTooth.vue

@@ -7,7 +7,7 @@
7 7
       <el-button @click="setModel" :disabled="blueToothDevice == ''" >进入配置模式</el-button>
8 8
       <el-button @click="InModel" :disabled="blueToothDevice == ''">进入传输模式</el-button>
9 9
       <br/>
10
-      <el-row :gutter="20" style="padding-top: 20px" >
10
+      <el-row :gutter="20" style="padding-top: 40px" >
11 11
         <el-col :span="6">
12 12
           <el-select v-model="value" filterable placeholder="请选择">
13 13
             <el-option

+ 5 - 3
static/server/app.js

@@ -8,8 +8,10 @@ io = require('socket.io').listen(server);
8 8
 var sendProtocal = require('./protocal/sendProtocal')
9 9
 var command = require('./protocal/command')
10 10
 var commonFunction = require('./protocal/commonFunction')
11
-var blueTouth = require('./blueTouth')
11
+//var blueTouth = require('./blueTouth')
12
+//var nodeBlueTooth = require('./nodeBlutTooth')
12 13
 // socket 服务开启
14
+var noble = require('./noble')
13 15
 
14 16
 let socketEvent = null;
15 17
 var connectCount = 0;
@@ -56,9 +58,9 @@ io.sockets.on('connection', function (socket) {
56 58
             console.log('已有初始连接' +connectCount +'个')
57 59
         }
58 60
         io.sockets.emit('connectCount', connectCount);
59
-        io.sockets.emit('setDeviceStatus', blueTouth.getBlueServerStatus());
61
+      //  io.sockets.emit('setDeviceStatus', blueTouth.getBlueServerStatus());
60 62
         // 将io对象传递给blueTooth js (发送连接客户端已经断开客户端消息发送)
61
-        blueTouth.setIo(io)
63
+      //  blueTouth.setIo(io)
62 64
     })
63 65
     // 发送获取采样频率指令
64 66
     socket.on('checkCaiyang',function (sendData) {

+ 61 - 0
static/server/noble.js

@@ -0,0 +1,61 @@
1
+var noble = require('noble');
2
+const ECHO_SERVICE_UUID = 'ec00';
3
+const ECHO_CHARACTERISTIC_UUID = 'ec0e';
4
+
5
+noble.on('stateChange', state => {
6
+    if (state === 'poweredOn') {
7
+        console.log('Scanning');
8
+        noble.startScanning([ECHO_SERVICE_UUID]);
9
+    } else {
10
+        noble.stopScanning();
11
+    }
12
+});
13
+
14
+noble.on('discover', peripheral => {
15
+    // connect to the first peripheral that is scanned
16
+    noble.stopScanning();
17
+    const name = peripheral.advertisement.localName;
18
+    console.log(`Connecting to '${name}' ${peripheral.id}`);
19
+    connectAndSetUp(peripheral);
20
+});
21
+
22
+function connectAndSetUp(peripheral) {
23
+    peripheral.connect(error => {
24
+        // specify the services and characteristics to discover
25
+        const serviceUUIDs = [ECHO_SERVICE_UUID];
26
+        const characteristicUUIDs = [ECHO_CHARACTERISTIC_UUID];
27
+        peripheral.discoverSomeServicesAndCharacteristics(
28
+            serviceUUIDs,
29
+            characteristicUUIDs,
30
+            onServicesAndCharacteristicsDiscovered
31
+        );
32
+    });
33
+    peripheral.on('disconnect', () => console.log('disconnected'));
34
+}
35
+
36
+function onServicesAndCharacteristicsDiscovered(error, services, characteristics) {
37
+    console.log(123123123123123)
38
+    console.log('Discovered services and characteristics');
39
+    const echoCharacteristic = characteristics[0];
40
+    // data callback receives notifications
41
+    echoCharacteristic.on('data', (data, isNotification) => {
42
+        console.log('Received: "' + data + '"');
43
+    });
44
+    // subscribe to be notified whenever the peripheral update the characteristic
45
+    echoCharacteristic.subscribe(error => {
46
+        if (error) {
47
+            console.error('Error subscribing to echoCharacteristic');
48
+        } else {
49
+            console.log('Subscribed for echoCharacteristic notifications');
50
+        }
51
+    });
52
+
53
+    // create an interval to send data to the service
54
+    let count = 0;
55
+    setInterval(() => {
56
+        count++;
57
+        const message = new Buffer('hello, ble ' + count, 'utf-8');
58
+        console.log("Sending:  '" + message + "'");
59
+        echoCharacteristic.write(message);
60
+    }, 2500);
61
+}

+ 36 - 0
static/server/nodeBlutTooth.js

@@ -0,0 +1,36 @@
1
+const bluetooth = require('node-bluetooth');
2
+//console.log(bluetooth)
3
+
4
+const device = new bluetooth.DeviceINQ();
5
+try {
6
+    device
7
+        .on('finished', console.log.bind(console, 'finished'))
8
+        .on('found', function found(address, name){
9
+            console.log('Found: ' + address + ' with name ' + name);
10
+            // find serial port channel
11
+            device.findSerialPortChannel(address, function(channel) {
12
+                console.log('Found RFCOMM channel for serial port on %s: ', name, channel);
13
+                // make bluetooth connect to remote device
14
+                try {
15
+                    bluetooth.connect(address, channel, function (err, connection) {
16
+                        if (err) return console.error(err);
17
+                        connection.delimiter = Buffer.from('\n', 'utf8');
18
+                        connection.on('data', (buffer) => {
19
+                            console.log('received message:', buffer.toString());
20
+                        });
21
+                        connection.write(new Buffer('Hello!', 'utf-8'), () => {
22
+                            console.log('wrote');
23
+                        });
24
+                    });
25
+                } catch (e) {
26
+                    console.log(123123)
27
+                     console.log(e)
28
+                }
29
+
30
+            });
31
+
32
+        })
33
+    device.scan();
34
+} catch (err) {
35
+    console.log(err)
36
+}