-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
251 lines (211 loc) · 7.38 KB
/
index.js
File metadata and controls
251 lines (211 loc) · 7.38 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
var DBus = require('dbus');
var bus = DBus.getBus('system');
const Properties = 'org.freedesktop.DBus.Properties';
const ObjectManager = 'org.freedesktop.DBus.ObjectManager';
const Device1 = 'org.bluez.Device1';
const Adapter1 = 'org.bluez.Adapter1';
const MediaControl1 = 'org.bluez.MediaControl1';
const MediaTransport1 = 'org.bluez.MediaTransport1';
const MediaPlayer1 = 'org.bluez.MediaPlayer1';
const LEAdvertisingManager1 = 'org.bluez.LEAdvertisingManager1';
const AgentManager1 = 'org.bluez.AgentManager1';
const Agent1 = 'org.bluez.Agent1';
var API = module.exports = function (cb) {
bus.getInterface('org.bluez', '/org/bluez/hci0', Properties, function(err, iface) {
iface.on('PropertiesChanged', function(count) {
console.log(arguments);
if (arguments[1].hasOwnProperty('Discovering')) {
cb({name: 'PropertiesChanged', property: 'Discovering', data: arguments[1]['Discovering']});
}
});
});
bus.getInterface('org.bluez', '/', ObjectManager, function(err, iface) {
iface.on('InterfacesAdded', function(count) {
console.log('InterfaceAdded');
// console.log(arguments);
if (arguments[1].hasOwnProperty(Device1)) {
cb({name: 'PropertiesChanged', property: 'Discovering', data: arguments[1][Device1]});
bus.getInterface('org.bluez', arguments[0], Properties, (err, iface) => {
iface.on('PropertiesChanged', function(count) {
// console.log(arguments[0] + ' PropertiesChanged');
// console.log(arguments);
if (arguments[1].hasOwnProperty('Paired')) {
cb({name: 'PropertiesChanged', property: 'Paired', data: arguments[1]['Paired']});
}
});
});
cb({name: 'InterfacesAdded', property: Device1, data: arguments[1][Device1]});
} else if (arguments[1].hasOwnProperty(MediaControl1)) {
cb({name: 'InterfacesAdded', property: MediaControl1, data: arguments[1][MediaControl1]});
} else if (arguments[1].hasOwnProperty(MediaTransport1)) {
cb({name: 'InterfacesAdded', property: MediaTransport1, data: arguments[1][MediaTransport1]});
} else if (arguments[1].hasOwnProperty(MediaPlayer1)) {
cb({name: 'InterfacesAdded', property: MediaPlayer1, data: arguments[1][MediaPlayer1]});
}
});
iface.on('InterfacesRemoved', function(count) {
// console.log('InterfaceRemoved');
// console.log(arguments);
});
});
}
addr2path = function(addr) {
var path='dev';
addr.split(":").forEach(e => { path += '_' + e; });
return path;
};
API.prototype.get_managed_objs = function(addr, cb) {
var p = '/org/bluez/hci0/' + addr2path(addr);
bus.getInterface('org.bluez', '/', ObjectManager, function(err, iface) {
iface.GetManagedObjects( function(err) {
arguments[1][p];
});
});
};
API.prototype.pair = function(addr) {
var p = '/org/bluez/hci0/' + addr2path(addr);
bus.getInterface('org.bluez', p, Device1, function(err, iface) {
iface.Pair( function(err) {
if (err) throw err;
console.log('Pair');
});
});
};
API.prototype.connect = function(addr) {
var p = '/org/bluez/hci0/' + addr2path(addr);
bus.getInterface('org.bluez', p, Device1, function(err, iface) {
iface.Connect( function(err) {
if (err) throw err;
console.log('Connect');
});
});
};
API.prototype.scan = function(onoff) {
if (onoff === 'on') {
bus.getInterface('org.bluez', '/org/bluez/hci0', Adapter1, function(err, iface) {
iface.StartDiscovery({}, function(err) {
console.log('StartDiscovery');
});
});
} else if (onoff === 'off') {
bus.getInterface('org.bluez', '/org/bluez/hci0', Adapter1, function(err, iface) {
iface.StopDiscovery({}, function(err) {
console.log('StopDiscovery');
});
});
}
};
API.prototype.discoverable = function(onoff, cb) {
if (onoff === 'on') {
bus.getInterface('org.bluez', '/org/bluez/hci0', Properties, function(err, iface) {
iface.Set('org.bluez.Adapter1', 'Discoverable', true, function() {
console.log('Discoverable On');
});
});
} else if (onoff === 'off') {
bus.getInterface('org.bluez', '/org/bluez/hci0', Properties, function(err, iface) {
iface.Set('org.bluez.Adapter1', 'Discoverable', false, function() {
console.log('Discoverable Off');
});
});
}
};
API.prototype.agent = function(cb) {
var service = DBus.registerService('system', 'org.bluez');
var obj = service.createObject('/bluenode/agent');
var iface1 = obj.createInterface(Agent1);
iface1.addMethod('Release', {}, function(callback) {
console.log('Release');
});
iface1.addMethod('RequestPinCode', { in: ['o'], out: ['s'] }, function(callback) {
console.log('RequestPinCode');
});
iface1.addMethod('DisplayPinCode', { in: ['o'], out: ['s'] }, function(callback) {
console.log('DisplayPinCode');
});
iface1.addMethod('RequestPasskey', { in: ['o'], out: ['u'] }, function(callback) {
console.log('RequestPasskey');
});
iface1.addMethod('DisplayPasskey', { in: ['o'], out: ['u', 'q'] }, function(callback) {
console.log('DisplayPasskey');
});
iface1.addMethod('RequestConfirmation', { in: ['o'], out: ['u'] }, function(callback) {
console.log('RequestConfirmation');
});
iface1.addMethod('RequestAuthorization', { in: ['o']}, function(callback) {
console.log('RequestAuthorization');
});
iface1.addMethod('AuthorizeService', { in: ['o'], out: ['s'] }, function(callback) {
console.log('AuthorizeService');
});
iface1.addMethod('Cancel', {}, function(callback) {
console.log('Cancel');
});
bus.getInterface('org.bluez', '/org/bluez', AgentManager1, function(err, iface) {
if (err) throw err;
iface.RegisterAgent('/bluenode/agent', 'KeyboardDisplay', function() {
console.log('RegisterAgent');
});
});
}
API.prototype.adv = function(onoff, cb) {
if (onoff === 'on') {
// Create a new service, object and interface
var service = DBus.registerService('system', 'org.bluez');
var obj = service.createObject('/bluenode/adv');
// Create interface
var iface1 = obj.createInterface('org.bluez.LEAdvertisement1');
iface1.addMethod('Release', {}, function(callback) {
console.log('Release');
});
// Writable property
iface1.addProperty('Type', {
type: DBus.Define(String),
getter: function(cb) {
cb(null, 'broadcast');
}
});
iface1.addProperty('ServiceUUIDs', {
type: 'as',
getter: function(cb) {
cb(null, ['cccc']);
}
});
iface1.addProperty('ManufacturerData', {
type: 'a{qv}',
getter: function(cb) {
cb(null, []);
}
});
iface1.addProperty('SolicitUUIDs', {
type: 'as',
getter: function(cb) {
cb(null, []);
}
});
iface1.addProperty('ServiceData', {
type: 'a{sv}',
getter: function(cb) {
cb(null, ['cccc', 0x01, 0x02, 0x03, 0x04]);
}
});
iface1.addProperty('IncludeTxPower', {
type: DBus.Define(Boolean),
getter: function(cb) {
cb(null, true);
}
});
bus.getInterface('org.bluez', '/org/bluez/hci0', LEAdvertisingManager1, function(err, iface) {
if (err) throw err;
iface.RegisterAdvertisement('/bluenode/adv', function() {
console.log('RegisterAdvertisement');
});
});
} else if (onoff ==='off') {
bus.getInterface('org.bluez', '/org/bluez/hci0', LEAdvertisingManager1, function(err, iface) {
iface.UnregisterAdvertisement('/bluenode/adv', function() {
console.log('UnregisterAdvertisement');
});
});
}
};