-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
201 lines (174 loc) · 5.99 KB
/
index.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
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
process.env.DISPLAY = ':0';
process.env.DBUS_SESSION_BUS_ADDRESS = 'unix:path=/run/dbus/system_bus_socket';
const dbus = require('dbus-native');
const fs = require('fs');
const { exec, spawn } = require("child_process");
//First, start streaming Bluez in to Alsa out
spawn("bluealsa-aplay", ["00:00:00:00:00:00"]);
//Shoot off a notification to let the user know the network is up
playRandomTrack('ready');
//Wire up our buttons
const ButtonClass = require('./buttons.js');
var buttons = new ButtonClass();
buttons.on("ready", function() {
console.log("Buttons on and listening");
});
buttons.on("play-pause", function() {
togglePlayPause();
});
buttons.start();
var bus = dbus.systemBus();
var connectedDevice = false;
var disconnectBound = false;
function togglePlayPause(){
bus.getService('org.bluez').getInterface(connectedDevice+'/player0', 'org.bluez.MediaPlayer1', function(err, MP){
//HEADS UP, if this ever fails to work in the future, check here:
// https://github.com/sidorares/dbus-native/pull/254/files
MP.Status ( function(e, propValue){
if (e) {
console.error ('Could not get play / pause status: ' + e)
}
else{
if(propValue == "playing"){
console.log('Pausing Playback');
MP.Pause();
}
else{
console.log('Playing');
MP.Play();
}
}
})
});
}
function playRandomTrack(type){
fs.readdir(__dirname+'/sounds/'+type, (err, files) => {
var file = files[Math.floor(Math.random() * files.length)];
var soundFile = __dirname+'/sounds/'+type+'/'+file;
exec('mpg321 -g 30 '+soundFile);
console.log('Playing '+soundFile);
});
}
function startBluetoothDiscovery(){
bus.getService('org.bluez').getInterface('/org/bluez/hci0', 'org.bluez.Adapter1', function(err, Intf){
Intf.Powered = true;
Intf.Discoverable = true;
Intf.Pariable = true;
console.log('Bluetooth on and pairing');
});
}
function stopBluetoothDiscovery(){
bus.getService('org.bluez').getInterface('/org/bluez/hci0', 'org.bluez.Adapter1', function(err, Intf){
Intf.Discoverable = false;
Intf.Pairable = false;
console.log('Shutting down Bluetooth discovery');
});
}
function connectToDevice(){
}
function bindDisconnectAction(){
if(!disconnectBound){
bus.getService('org.bluez').getInterface(connectedDevice, 'org.freedesktop.DBus.Properties', function(err, properties){
properties.on('PropertiesChanged', function(device, props){
var connectStatus = true;
if(props[0][0] == "Connected"){
connectStatus = props[0][1];
}
//Check to see if disconnected and run disconnect stuff
if(connectStatus !== true && connectedDevice){
playRandomTrack("disconnect");
startBluetoothDiscovery();
console.log("Device Disconnected: "+connectedDevice);
connectedDevice = false;
}
});
});
}
disconnectBound = true;
}
startBluetoothDiscovery();
bus.getService('org.bluez').getInterface('/', 'org.freedesktop.DBus.ObjectManager', function(err, objectManager){
//Wire up any functionality that has to run when devices connect
objectManager.on('InterfacesAdded', function(interface, props){
var regex = /^\/org\/bluez\/hci0\/dev_[A-Z0-9_]+\/fd[0-9]+$/;
if(regex.test(interface) && !connectedDevice){
for(var i in props){
if(props[i][0] == "org.bluez.MediaTransport1"){
var values = props[i][1];
for(var j in values){
if(values[j][0] == "Device"){
var deviceId = values[j][1][1][0];
playRandomTrack("connect");
stopBluetoothDiscovery();
connectedDevice = deviceId;
bindDisconnectAction();
console.log("Device Connected: "+deviceId);
}
}
}
}
}
});
});
var agentPath = "/com/jarethmt/bluetooth";
var agent = {
mode: "NoInputNoOutput",
exitOnRelease: true,
Registered: function(err) {
//console.log('Registered agent', arguments);
},
Release: function() {
console.log('Release', arguments);
},
RequestPinCode: function(device) {
//console.log("RequestPinCode", arguments);
//console.log('requesting pin code');
},
DisplayPinCode: function(device, pincode) {
//console.log("DisplayPinCode", arguments);
},
RequestPasskey: function(device) {
//console.log("RequestPasskey", arguments);
//console.log('requesting pass key');
},
DisplayPasskey: function(device, passkey, entered) {
//console.log("DisplayPasskey", arguments);
},
RequestConfirmation: function(device, passkey) {
console.log("RequestConfirmation", arguments);
},
RequestAuthorization: function(device) {
console.log("RequestAuthorization", arguments);
},
AuthorizeService: function(device, uuid) {
//console.log("AuthorizeService", arguments);
//connectedDevice = device;
},
Cancel: function() {
//console.log("Cancel", arguments);
},
};
bus.exportInterface(agent, agentPath, {
name: 'org.bluez.Agent1',
methods: {
Release: [ '', '' ],
RequestPinCode: [ 'o', 's' ],
DisplayPinCode: [ 'os', '' ],
RequestPasskey: [ 'o', 'u' ],
DisplayPasskey: [ 'ouq', '' ],
RequestConfirmation: [ 'ou', '' ],
RequestAuthorization: [ 'o', '' ],
AuthorizeService: [ 'os', '' ],
Cancel: [ '', '' ]
}
});
var bluez = bus.getService('org.bluez');
bluez.getInterface('/org/bluez', 'org.bluez.AgentManager1', function(err, agentmanager) {
agentmanager.RegisterAgent(agentPath, agent.mode || "NoInputNoOutput", function(err, res) {
agentmanager.RequestDefaultAgent(agentPath, function(data, data2){
//console.log('testing');
//console.log(data);
//console.log(data2);
});
});
});