-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbluetoothManager.js
More file actions
204 lines (166 loc) · 5.87 KB
/
Copy pathbluetoothManager.js
File metadata and controls
204 lines (166 loc) · 5.87 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
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
const BLUEZ_NAME = 'org.bluez';
const BLUEZ_ROOT = '/';
const DEVICE_IFACE = 'org.bluez.Device1';
const BATTERY_IFACE = 'org.bluez.Battery1';
const AUDIO_UUIDS = new Set([
'00001108-0000-1000-8000-00805f9b34fb',
'0000110b-0000-1000-8000-00805f9b34fb',
'0000110e-0000-1000-8000-00805f9b34fb',
'0000111e-0000-1000-8000-00805f9b34fb',
]);
const PROFILE_LABELS = new Map([
['00001108-0000-1000-8000-00805f9b34fb', 'HSP'],
['0000110b-0000-1000-8000-00805f9b34fb', 'A2DP'],
['0000110e-0000-1000-8000-00805f9b34fb', 'A2DP'],
['0000111e-0000-1000-8000-00805f9b34fb', 'HFP'],
]);
function _deepUnpack(property, fallback = null) {
if (!property)
return fallback;
return property.deepUnpack();
}
function _normalizeUuid(uuid) {
return uuid.toLowerCase();
}
export const BluetoothManager = GObject.registerClass({
Signals: {
'devices-changed': {},
},
}, class BluetoothManager extends GObject.Object {
_init() {
super._init();
this._objectManager = null;
this._managerSignals = [];
this._proxySignals = new Map();
this._devices = new Map();
this.refresh();
}
destroy() {
for (const signalId of this._managerSignals)
this._objectManager?.disconnect(signalId);
this._managerSignals = [];
for (const [proxy, signalId] of this._proxySignals)
proxy.disconnect(signalId);
this._proxySignals.clear();
this._devices.clear();
this._objectManager = null;
}
refresh() {
this._ensureObjectManager();
this._rebuildDevices();
this.emit('devices-changed');
}
getConnectedHeadsets() {
return [...this._devices.values()]
.sort((left, right) => left.name.localeCompare(right.name));
}
getDeviceBattery(devicePath) {
return this._devices.get(devicePath)?.battery ?? null;
}
getDeviceName(devicePath) {
return this._devices.get(devicePath)?.name ?? null;
}
_ensureObjectManager() {
if (this._objectManager)
return;
try {
this._objectManager = Gio.DBusObjectManagerClient.new_for_bus_sync(
Gio.BusType.SYSTEM,
Gio.DBusObjectManagerClientFlags.NONE,
BLUEZ_NAME,
BLUEZ_ROOT,
null,
null
);
} catch (error) {
logError(error, 'Bluetooth Headset Monitor: Failed to create BlueZ object manager');
return;
}
this._managerSignals = [
this._objectManager.connect('object-added', () => this._syncState()),
this._objectManager.connect('object-removed', () => this._syncState()),
this._objectManager.connect('interface-added', (_, object, iface) => {
this._watchProxy(iface);
this._syncState();
}),
this._objectManager.connect('interface-removed', (_, object, iface) => {
this._unwatchProxy(iface);
this._syncState();
}),
];
for (const object of this._objectManager.get_objects()) {
for (const ifaceName of [DEVICE_IFACE, BATTERY_IFACE]) {
const proxy = object.get_interface(ifaceName);
if (proxy)
this._watchProxy(proxy);
}
}
}
_watchProxy(proxy) {
if (this._proxySignals.has(proxy))
return;
const signalId = proxy.connect('g-properties-changed', () => this._syncState());
this._proxySignals.set(proxy, signalId);
}
_unwatchProxy(proxy) {
const signalId = this._proxySignals.get(proxy);
if (!signalId)
return;
proxy.disconnect(signalId);
this._proxySignals.delete(proxy);
}
_syncState() {
this._rebuildDevices();
this.emit('devices-changed');
}
_rebuildDevices() {
this._devices.clear();
if (!this._objectManager)
return;
for (const object of this._objectManager.get_objects()) {
const deviceProxy = object.get_interface(DEVICE_IFACE);
if (!deviceProxy)
continue;
const device = this._readDevice(object, deviceProxy);
if (!device)
continue;
this._devices.set(device.path, device);
}
}
_readDevice(object, deviceProxy) {
const connected = Boolean(_deepUnpack(deviceProxy.get_cached_property('Connected'), false));
if (!connected)
return null;
const uuids = (_deepUnpack(deviceProxy.get_cached_property('UUIDs'), []) || [])
.map(_normalizeUuid);
if (!uuids.some(uuid => AUDIO_UUIDS.has(uuid)))
return null;
const batteryProxy = object.get_interface(BATTERY_IFACE);
const percentage = _deepUnpack(batteryProxy?.get_cached_property('Percentage'));
const rssi = _deepUnpack(deviceProxy.get_cached_property('RSSI'));
const alias = _deepUnpack(deviceProxy.get_cached_property('Alias'), 'Unknown Device');
const address = _deepUnpack(deviceProxy.get_cached_property('Address'), 'Unknown');
return {
path: object.get_object_path(),
name: alias,
address,
connected,
battery: Number.isInteger(percentage) ? percentage : null,
profile: this._getProfileLabel(uuids),
rssi: typeof rssi === 'number' ? rssi : null,
uuids,
lastSeen: GLib.DateTime.new_now_local().format('%FT%T%z'),
};
}
_getProfileLabel(uuids) {
for (const uuid of uuids) {
const label = PROFILE_LABELS.get(uuid);
if (label)
return label;
}
return 'Audio';
}
});