-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBluezAgent.js
90 lines (75 loc) · 2.46 KB
/
BluezAgent.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
const EventEmitter = require('events').EventEmitter;
const util = require('util');
const DBus = require('dbus');
const dbus = new DBus();
const bluezDBus = require('./BluezDBus');
module.exports = BluezAgent;
function BluezAgent(serviceName, path, onRegistered) {
this.debug = require('debug')('Bluez:Agent:' + path);
this.path = path;
this.debug('New agent');
/* Create service based on org.bluez.Agent1 definition. We only support BLE
* devices at the moment and can only provide a passkey */
var service = dbus.registerService('system', serviceName);
var obj = service.createObject(path);
var iface = obj.createInterface('org.bluez.Agent1');
iface.addMethod('Release', {}, function(callback) {
this.debug('Release()');
callback();
});
iface.addMethod('RequestPasskey', { in: [{ type: 'o' }], out: { type: 'u' } },
(device, callback) => {
this.debug('RequestPasskey(' + device + ')');
var passkey = null;
if (this.passkeyHandler !== undefined)
passkey = this.passkeyHandler(device);
this.debug('Providing passkey ' + passkey + ' for ' + device);
callback(passkey ? passkey : new Error('org.bluez.Error.Canceled'));
}
);
iface.addMethod('Cancel', {}, function(callback) {
debug('Cancel()');
callback();
});
iface.update();
}
util.inherits(BluezAgent, EventEmitter);
BluezAgent.prototype.register = function(cb) {
/* Register agent with Bluez */
bluezDBus.getInterface('/org/bluez', 'org.bluez.AgentManager1', (err, iface) =>
{
if (err) {
this.debug('Failed getting AgentManager1 interface:' + err);
return;
}
/* Save this interface */
this.iface = iface;
iface.RegisterAgent['finish'] = () => {
this.debug('Registered agent');
if (cb) cb();
};
iface.RegisterAgent['error'] = (err) => {
this.debug('Failed registered agent: ' + err);
if (cb) cb(err);
};
iface.RegisterAgent(this.path, 'KeyboardOnly');
});
}
BluezAgent.prototype.setDefault = function(cb) {
if (!this.iface) {
if (cb) cb('Agent was not registered yet');
return;
}
this.iface.RequestDefaultAgent['finish'] = () => {
this.debug('Set agent as default');
if (cb) cb();
};
this.iface.RequestDefaultAgent['error'] = (err) => {
this.debug('Failed settings as default agent: ' + err);
if (cb) cb(err);
};
this.iface.RequestDefaultAgent(this.path);
}
BluezAgent.prototype.setPasskeyHandler = function(func) {
this.passkeyHandler = func;
}