continue bluez classes

This commit is contained in:
2025-12-16 13:54:50 +01:00
parent 3f46c34fca
commit 90ad5bb28c
2 changed files with 36 additions and 5 deletions
+20 -3
View File
@@ -7,17 +7,19 @@ export class Bluez {
this.bus = systemBus();
}
async init() {
async getObjectManager() {
const o = await this.bus.getProxyObject("org.bluez", "/");
this.om = o.getInterface("org.freedesktop.DBus.ObjectManager");
return o.getInterface("org.freedesktop.DBus.ObjectManager");
}
async getDevice(name) {
const objs = await this.om.GetManagedObjects();
const om = await this.getObjectManager();
const objs = await om.GetManagedObjects();
for (const [path, ifaces] of Object.entries(objs)) {
if (
Object.hasOwn(ifaces, DEVICE_IFACE) &&
Object.hasOwn(ifaces[DEVICE_IFACE], "Name") &&
ifaces[DEVICE_IFACE]["Name"].value === name
) {
return new BluezDevice(this.bus, path);
@@ -32,6 +34,21 @@ class BluezDevice {
this.path = path;
}
async getObject() {
return this.dbus.getProxyObject("org.bluez", this.path);
}
async getProperty(name) {
const o = await this.getObject();
const props = o.getInterface("org.freedesktop.DBus.Properties");
return props.Get(DEVICE_IFACE, name);
}
async isConnected() {
const c = await this.getProperty("Connected");
return c.value;
}
async connect() {
const o = await this.dbus.getProxyObject("org.bluez", this.path);
const device = o.getInterface(DEVICE_IFACE);
+16 -2
View File
@@ -92,8 +92,22 @@ async function main() {
console.log("BLE GATT application registered");*/
const bz = new Bluez();
await bz.init();
bz.getDevice("PAFERS_53B9A3");
const pafers = await bz.getDevice("PAFERS_53B9A3");
if (!pafers) {
process.exit(1);
}
const iscon = await pafers.isConnected();
console.log("connected: " + iscon);
bz.on("interfacesAdded", () => {
});
if (!iscon) {
await pafers.connect();
}
return;