|
|
|
@ -7,17 +7,19 @@ export class Bluez { |
|
|
|
this.bus = systemBus(); |
|
|
|
this.bus = systemBus(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async init() { |
|
|
|
async getObjectManager() { |
|
|
|
const o = await this.bus.getProxyObject("org.bluez", "/"); |
|
|
|
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) { |
|
|
|
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)) { |
|
|
|
for (const [path, ifaces] of Object.entries(objs)) { |
|
|
|
if ( |
|
|
|
if ( |
|
|
|
Object.hasOwn(ifaces, DEVICE_IFACE) && |
|
|
|
Object.hasOwn(ifaces, DEVICE_IFACE) && |
|
|
|
|
|
|
|
Object.hasOwn(ifaces[DEVICE_IFACE], "Name") && |
|
|
|
ifaces[DEVICE_IFACE]["Name"].value === name |
|
|
|
ifaces[DEVICE_IFACE]["Name"].value === name |
|
|
|
) { |
|
|
|
) { |
|
|
|
return new BluezDevice(this.bus, path); |
|
|
|
return new BluezDevice(this.bus, path); |
|
|
|
@ -32,6 +34,21 @@ class BluezDevice { |
|
|
|
this.path = path; |
|
|
|
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() { |
|
|
|
async connect() { |
|
|
|
const o = await this.dbus.getProxyObject("org.bluez", this.path); |
|
|
|
const o = await this.dbus.getProxyObject("org.bluez", this.path); |
|
|
|
const device = o.getInterface(DEVICE_IFACE); |
|
|
|
const device = o.getInterface(DEVICE_IFACE); |
|
|
|
|