This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from MichalW/gnome-bluetooth-battery-indicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth.js
88 lines (74 loc) · 2.58 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
const GnomeBluetooth = imports.gi.GnomeBluetooth;
const Signals = imports.signals;
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', () => {
this.emit('device-changed');
});
this._connectSignal(this._model, 'row-deleted', () => {
this.emit('device-changed');
});
this._connectSignal(this._model, 'row-inserted', () => {
this.emit('device-changed');
});
}
getDevices() {
const devices = [];
const adapter = this._getDefaultAdapter();
if (!adapter) {
return [];
}
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(({ isConnected }) => isConnected);
}
getPairedDevices() {
return this.getDevices().filter(({ isPaired }) => isPaired);
}
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);
}
}