@tanislav000/bluez
Version:
Bluez5 D-Bus bindings for easy to use bluetooth access in Node.js
52 lines • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Device = void 0;
const gattService_1 = require("./gattService");
const dbus_next_1 = require("dbus-next");
const Device1_1 = require("./dbus/Device1");
const mediaControl_1 = require("./mediaControl");
class Device extends Device1_1.OrgBluezDevice1 {
constructor(dbusObject, bluez) {
super(dbusObject);
this.bluez = bluez;
}
/**
* Find a Gatt Service by UUID
* @param uuid
* @throws {DBusError} org.bluez.Error.DoesNotExist
*/
async getGattService(uuid) {
const services = await this.listGattServices();
for (const path in services) {
if (services[path].UUID === uuid) {
return this.bluez.getDbusObjectInterface(gattService_1.GattService, path);
}
}
throw new dbus_next_1.DBusError("org.bluez.Error.DoesNotExist", "Service not found");
}
/**
* Returns a list of known Gatt services
* It returns key value pairs for each service.
* The key is the object path and the value is a snapshot of the properties of the service.
* To get the full service interface use `Bluez.getDbusObjectInterface(GattService, key)`
*/
async listGattServices() {
const objs = await this.bluez.getObjectManager().GetManagedObjects();
return Object.fromEntries(Object.entries(objs)
.filter(([path, ifs]) => path.startsWith(this.dbusObject.path) && gattService_1.GattService.DbusInterfaceName in ifs)
.map(([path, ifs]) => [path, ifs[gattService_1.GattService.DbusInterfaceName]]));
}
/**
* Returns the MediaControl interface if available
*/
getMediaControl() {
// TODO: interfaces is a fixed snapshot at time of object creation,
// we might need to just try to create the interface
if (this.dbusObject.interfaces[mediaControl_1.MediaControl.DbusInterfaceName]) {
return new mediaControl_1.MediaControl(this.dbusObject);
}
return null;
}
}
exports.Device = Device;
//# sourceMappingURL=device.js.map