-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientDevice-socketio.js
More file actions
158 lines (141 loc) · 3.95 KB
/
Copy pathClientDevice-socketio.js
File metadata and controls
158 lines (141 loc) · 3.95 KB
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
var noble = require('noble');
var io = require('socket.io-client');
var IM_UUID = "12db3240d2c64d48b83914f259102c84";
var TS_UUID = "018adaea6abc4794bbaff057a93cbad2"
var AX_UUID = "a7c18ce4282b4eb6b7b7d42dc9b6b792";
var AY_UUID = "fd28c492d9af4c88b4d91be5a0266fac"
var AZ_UUID = "4da2ab5c1c714236975645250cf43a13"
var GX_UUID = "c39537b41cfb4b9081146b93119d1f3f"
var GY_UUID = "5a7d839e639e4678b678ca803e377bc4"
var GZ_UUID = "9767f420c0ee44f5a531191913ce5d41"
var ts = null;
var ax = null;
var ay = null;
var az = null;
var gx = null;
var gy = null;
var gz = null;
var data = {};
var socket = io.connect('http://192.168.2.12:8000', {reconnect: true}); //Insert your server's ip address here
socket.on('connect', function() {
console.log('Connected to server');
socket.emit('chat message', 'hello');
});
//This Function starts a scan. Must detect bluetooth on or not first.
noble.on('stateChange', function(state){
if(state == 'poweredOn'){
noble.startScanning([IM_UUID],false);
console.log('Scanning...');
}
else{
noble.stopScanning();
}
});
noble.on('discover', function(peripheral) {
// we found a peripheral, stop scanning
noble.stopScanning();
console.log('found peripheral:', peripheral.advertisement);
peripheral.connect(function(err) {
peripheral.discoverServices([IM_UUID],function(err,services){
services.forEach(function(service){
console.log('Found Service: ', service.uuid);
service.discoverCharacteristics([], function(err, characteristics){
characteristics.forEach(function(characteristic){
console.log('found characteristic: ', characteristic.uuid);
if(TS_UUID==characteristic.uuid){
ts = characteristic;
}
if(AX_UUID==characteristic.uuid){
ax = characteristic;
}
if(AY_UUID==characteristic.uuid){
ay = characteristic;
}
if(AZ_UUID==characteristic.uuid){
az = characteristic;
}
if(GX_UUID==characteristic.uuid){
gx = characteristic;
}
if(GY_UUID==characteristic.uuid){
gy = characteristic;
}
if(GZ_UUID==characteristic.uuid){
gz = characteristic;
}
});
if(ts && ax && az && az && gx && gy && gz){
shiraseru();//turn on on notifications
detawoOkuru();
freqwrite();
console.log("Found all Services!");
}
else{
console.log("Error...");
}
});
});
});
});
});
function shiraseru(){
ts.notify('true', function(error) {
if (error) throw error;
});
ax.notify('true', function(error) {
if (error) throw error;
});
ay.notify('true', function(error) {
if (error) throw error;
});
az.notify('true', function(error) {
if (error) throw error;
});
gx.notify('true', function(error) {
if (error) throw error;
});
gy.notify('true', function(error) {
if (error) throw error;
});
gz.notify('true', function(error) {
if (error) throw error;
});
}
function detawoOkuru(){
//Note: Data is harcoded to be sent in a certain order at a lower level...
ts.on('read', function(data) {
//console.log("AX: ",data.readInt32LE(0));
socket.emit('t',data.readInt32LE(0));
});
ax.on('read', function(data) {
//console.log("AX: ",data.readInt32LE(0));
socket.emit('ax',data.readInt32LE(0));
});
ay.on('read', function(data) {
//console.log("AY: ", data.readInt32LE(0));
socket.emit('ay',data.readInt32LE(0));
});
az.on('read', function(data) {
//console.log("AZ: ",data.readInt32LE(0));
socket.emit('az',data.readInt32LE(0));
});
gx.on('read', function(data) {
//console.log("GX: ", data.readInt32LE(0));
socket.emit('gx',data.readInt32LE(0));
});
gy.on('read', function(data) {
//console.log("GY: ",data.readInt32LE(0));
socket.emit('gy',data.readInt32LE(0));
});
gz.on('read', function(data) {
//console.log("GZ: ", data.readInt32LE(0));
socket.emit('gz',data.readInt32LE(0));
});
}
function freqwrite(){
var buf = new Buffer(2);
socket.on('freq',function(data){
buf.writeInt16LE(data, 0);
ts.write(buf);
})
}