Browse Source

started bluez classes

master
Anthony Hinsinger 5 days ago
parent
commit
3f46c34fca
  1. 46
      src/bluez.js
  2. 51
      src/index.js

46
src/bluez.js

@ -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();
}
}

51
src/index.js

@ -5,6 +5,7 @@ const { Interface, method, property } = iface;
import { BaseService, BaseCharacteritic } from "./bluez-gatt"; import { BaseService, BaseCharacteritic } from "./bluez-gatt";
import { Advertisement } from "./adv"; import { Advertisement } from "./adv";
import { BaseInterface } from "./base"; import { BaseInterface } from "./base";
import { Bluez } from "./bluez";
class Application extends BaseInterface { class Application extends BaseInterface {
constructor(bus, path) { constructor(bus, path) {
@ -85,10 +86,56 @@ async function main() {
const leamgr = obj.getInterface("org.bluez.LEAdvertisingManager1"); const leamgr = obj.getInterface("org.bluez.LEAdvertisingManager1");
const gattmgr = obj.getInterface("org.bluez.GattManager1"); const gattmgr = obj.getInterface("org.bluez.GattManager1");
leamgr.RegisterAdvertisement("/eu/atoy/advertising", {}); /*leamgr.RegisterAdvertisement("/eu/atoy/advertising", {});
console.log("BLE Advertisement registered"); console.log("BLE Advertisement registered");
gattmgr.RegisterApplication("/eu/atoy", {}); gattmgr.RegisterApplication("/eu/atoy", {});
console.log("BLE GATT application registered"); console.log("BLE GATT application registered");*/
const bz = new Bluez();
await bz.init();
bz.getDevice("PAFERS_53B9A3");
return;
const root = await bus.getProxyObject("org.bluez", "/");
const om = root.getInterface("org.freedesktop.DBus.ObjectManager");
const deviceObj = await bus.getProxyObject(
"org.bluez",
"/org/bluez/hci0/dev_FE_C4_D5_A3_B9_53"
);
const device = deviceObj.getInterface("org.bluez.Device1");
const devProps = deviceObj.getInterface("org.freedesktop.DBus.Properties");
const connected = await devProps.Get("org.bluez.Device1", "Connected");
console.log(connected);
if (connected.value !== true) {
await device.Connect();
}
const managed = await om.GetManagedObjects();
for (const [key, value] of Object.entries(managed)) {
if (Object.hasOwn(value, "org.bluez.GattCharacteristic1")) {
if (
value["org.bluez.GattCharacteristic1"]["UUID"].value ===
"72d70003-501f-46f7-95f9-23846ee1aba3"
) {
console.log(key);
const charObj = await bus.getProxyObject("org.bluez", key);
const charProp = charObj.getInterface(
"org.freedesktop.DBus.Properties"
);
const charChar = charObj.getInterface("org.bluez.GattCharacteristic1");
charProp.on("PropertiesChanged", (iface, changed, invalidated) => {
console.log(iface);
console.log(changed.Value.value);
console.log(invalidated);
});
await charChar.StartNotify();
}
}
}
} }
main(); main();

Loading…
Cancel
Save