From 90ad5bb28c9deed6c258255649d1ec3652977556 Mon Sep 17 00:00:00 2001 From: Anthony Hinsinger Date: Tue, 16 Dec 2025 13:54:50 +0100 Subject: [PATCH] continue bluez classes --- src/bluez.js | 23 ++++++++++++++++++++--- src/index.js | 18 ++++++++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/bluez.js b/src/bluez.js index 1d53b72..4e627f1 100644 --- a/src/bluez.js +++ b/src/bluez.js @@ -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); diff --git a/src/index.js b/src/index.js index eadfd5e..642cc5e 100644 --- a/src/index.js +++ b/src/index.js @@ -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;