|
@@ -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 {
|