forked from bjarosze/gnome-bluetooth-quick-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetooth.js
121 lines (103 loc) · 3.82 KB
/
bluetooth.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
// Copyright 2018 Bartosz Jaroszewski
// SPDX-License-Identifier: GPL-2.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const GnomeBluetooth = imports.gi.GnomeBluetooth;
const Signals = imports.signals;
const GLib = imports.gi.GLib;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
var BluetoothController = class {
constructor() {
this._client = new GnomeBluetooth.Client();
this._model = this._client.get_model();
}
enable() {
this._connectSignal(this._model, 'row-changed', (arg0, arg1, iter) => {
if (iter) {
let device = this._buildDevice(iter);
this.emit('device-changed', device);
}
});
this._connectSignal(this._model, 'row-deleted', () => {
this.emit('device-deleted');
});
this._connectSignal(this._model, 'row-inserted', (arg0, arg1, iter) => {
if (iter) {
let device = this._buildDevice(iter);
this.emit('device-inserted', device);
}
});
}
getDevices() {
let adapter = this._getDefaultAdapter();
if (!adapter)
return [];
let devices = [];
let [ret, iter] = this._model.iter_children(adapter);
while (ret) {
let device = this._buildDevice(iter);
devices.push(device);
ret = this._model.iter_next(iter);
}
return devices;
}
getConnectedDevices() {
return this.getDevices().filter((device) => {
return device.isConnected;
});
}
destroy() {
this._disconnectSignals();
}
_getDefaultAdapter() {
let [ret, iter] = this._model.get_iter_first();
while (ret) {
let isDefault = this._model.get_value(iter, GnomeBluetooth.Column.DEFAULT);
let isPowered = this._model.get_value(iter, GnomeBluetooth.Column.POWERED);
if (isDefault && isPowered)
return iter;
ret = this._model.iter_next(iter);
}
return null;
}
_buildDevice(iter) {
return new BluetoothDevice(this._model, iter);
}
}
Signals.addSignalMethods(BluetoothController.prototype);
Utils.addSignalsHelperMethods(BluetoothController.prototype);
var BluetoothDevice = class {
constructor(model, iter) {
this._model = model;
this.update(iter);
}
update(iter) {
this.name = this._model.get_value(iter, GnomeBluetooth.Column.NAME);
this.isConnected = this._model.get_value(iter, GnomeBluetooth.Column.CONNECTED);
this.isPaired = this._model.get_value(iter, GnomeBluetooth.Column.PAIRED);
this.mac = this._model.get_value(iter, GnomeBluetooth.Column.ADDRESS);
this.isDefault = this._model.get_value(iter, GnomeBluetooth.Column.DEFAULT);
}
disconnect(callback) {
Utils.spawn(`bluetoothctl -- disconnect ${this.mac}`, callback)
}
connect(callback) {
Utils.spawn(`bluetoothctl -- connect ${this.mac}`, callback)
}
reconnect(callback) {
Utils.spawn(`bluetoothctl -- disconnect ${this.mac} && bluetoothctl -- connect ${this.mac}`, callback)
}
}