-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbbowl.js
executable file
·163 lines (122 loc) · 3.79 KB
/
bbowl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env node
var noble = require('noble')
var net = require('net')
var perif
var uart = {
tx : null,
rx : null
}
var connections = []
var uids=[]
var settings = {
HOST :"localhost",
PORT : 8000,
UART : '6e400001b5a3f393e0a9e50e24dcca9e',
RX : '6e400003b5a3f393e0a9e50e24dcca9e',
TX : '6e400002b5a3f393e0a9e50e24dcca9e',
ADDRESS : ""
}
if(process.argv.indexOf("-a") != -1){
settings.ADDRESS = process.argv[process.argv.indexOf("-a") + 1];
}
if(process.argv.indexOf("-s") != -1){
settings.UART = process.argv[process.argv.indexOf("-s") + 1];
}
if(process.argv.indexOf("-RXChar") != -1){
settings.RX = process.argv[process.argv.indexOf("-RXChar") + 1];
}
if(process.argv.indexOf("-TXChar") != -1){
settings.TX = process.argv[process.argv.indexOf("-TXChar") + 1];
}
if(process.argv.indexOf("-b") != -1){
settings.HOST = process.argv[process.argv.indexOf("-b") + 1];
}
if(process.argv.indexOf("-p") != -1){
settings.PORT = process.argv[process.argv.indexOf("-p") + 1];
}
console.log("bbowl started")
console.log("settings: " + JSON.stringify(settings,null,2))
process.on('exit', function() {
if (perif != null) {
console.log("goodbye, disconnecting...")
peripheral.once('disconnect');
}
})
var writeToBT = function(data) {
console.log("->\n"+data.slice(0,-1))
uart.tx.write(new Buffer(data))
}
var writeToSockets = function(data) {
console.log("<-\n" + data.slice(0,-1))
connections.forEach(function (sck) {
sck.write(data)
})
}
function ready(chrRead, chrWrite) {
console.log("connected to bluetooth le UART device")
uart.rx = chrRead
uart.tx = chrWrite
uart.rx.on('data', writeToSockets)
uart.rx.notify(true)
console.log("RX is setup")
net.createServer(function(sock) {
console.log("connected [" + sock.remoteAddress + ":" + sock.remotePort + "]");
sock.on('data', writeToBT)
sock.on('close', function(data) {
console.log("closed [" + sock.remoteAddress + ":" + sock.remotePort + "]");
connections.splice(connections.indexOf(sock), 1);
});
connections.push(sock)
}).listen(settings.PORT, settings.HOST);
}
noble.on('stateChange', function(state) {
console.log("bluetooth state: [" + state + "]")
if(state==="poweredOn") {
noble.startScanning(uids, false,function(error) {
if (!error) {
console.log("scanning for bluetooth le devices...")
} else {
console.log("problems during scanning for bluetooth le devices: " + error)
}
})
}
})
noble.on('discover', function(p) {
perif = p
console.log("found "+ p.advertisement.localName + " " + p.address)
if (settings.ADDRESS == "" || p.address === settings.ADDRESS) {
console.log("stopping scanning...")
noble.stopScanning();
console.log("trying to connect to " + p.advertisement.localName + "["+p.address+"]")
p.connect(function() {
p.discoverAllServicesAndCharacteristics(function(error, services, characteristics){
if (!error) {
console.log("[---")
console.log("Services: \n" + "["+services+"]")
console.log("Characteristics: \n" + "[" + characteristics + "]")
console.log("---]")
var chrRead
var chrWrite
services.forEach(function(s, serviceId) {
if (s.uuid == settings.UART) {
s.characteristics.forEach(function(ch, charId) {
if (ch.uuid === settings.RX) {
chrRead = ch
} else if (ch.uuid === settings.TX) {
chrWrite = ch
}
})
}
})
if (chrRead != null && chrWrite != null) {
ready(chrRead, chrWrite)
} else {
console.log("no UART service/charactersitics found...")
}
} else {
console.log(error)
}
})
})
}
})