2 changed files with 95 additions and 2 deletions
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
import dbus, { systemBus } from "dbus-next"; |
||||
|
||||
const DEVICE_IFACE = "org.bluez.Device1"; |
||||
|
||||
export class Bluez { |
||||
constructor() { |
||||
this.bus = systemBus(); |
||||
} |
||||
|
||||
async init() { |
||||
const o = await this.bus.getProxyObject("org.bluez", "/"); |
||||
this.om = o.getInterface("org.freedesktop.DBus.ObjectManager"); |
||||
} |
||||
|
||||
async getDevice(name) { |
||||
const objs = await this.om.GetManagedObjects(); |
||||
|
||||
for (const [path, ifaces] of Object.entries(objs)) { |
||||
if ( |
||||
Object.hasOwn(ifaces, DEVICE_IFACE) && |
||||
ifaces[DEVICE_IFACE]["Name"].value === name |
||||
) { |
||||
return new BluezDevice(this.bus, path); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
class BluezDevice { |
||||
constructor(dbus, path) { |
||||
this.dbus = dbus; |
||||
this.path = path; |
||||
} |
||||
|
||||
async connect() { |
||||
const o = await this.dbus.getProxyObject("org.bluez", this.path); |
||||
const device = o.getInterface(DEVICE_IFACE); |
||||
await device.Connect(); |
||||
} |
||||
|
||||
async disconnect() { |
||||
const o = await this.dbus.getProxyObject("org.bluez", this.path); |
||||
const device = o.getInterface(DEVICE_IFACE); |
||||
await device.Disconnect(); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue