@tanislav000/bluez
Version:
Bluez5 D-Bus bindings for easy to use bluetooth access in Node.js
186 lines • 7.62 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Bluez = void 0;
const DBus = __importStar(require("dbus-next"));
const profile_1 = require("./profile");
const adapter_1 = require("./adapter");
const device_1 = require("./device");
const profileWrapper_1 = require("./profileWrapper");
const agentWrapper_1 = require("./agentWrapper");
const utilts_1 = require("./utilts");
const ProfileManager1_1 = require("./dbus/ProfileManager1");
const AgentManager1_1 = require("./dbus/AgentManager1");
const DBus_ObjectManager_1 = require("./dbus/DBus-ObjectManager");
const HAS_WEAKREF = typeof WeakRef === "function";
class Bluez {
constructor(options) {
this.adapterCache = new Map();
this.bus =
options && options.bus
? options.bus
: DBus.systemBus({
negotiateUnixFd: true,
});
this.options = Object.assign({
bus: this.bus,
userInterfacesPath: "/org/bluez",
}, options);
}
async init() {
this.objectManager = await DBus_ObjectManager_1.OrgFreedesktopDBusObjectManager.Connect(this.bus);
this.bluezRootObject = await this.bus.getProxyObject("org.bluez", "/org/bluez");
this.agentManager = new AgentManager1_1.OrgBluezAgentManager1(this.bluezRootObject);
this.profileManager = new ProfileManager1_1.OrgBluezProfileManager1(this.bluezRootObject);
//await this.bus.requestName('org.test.name');
}
getBus() {
return this.bus;
}
getObjectManager() {
return this.objectManager;
}
getAdapter(searchedName) {
const adapterNode = this.bluezRootObject.nodes.find((node) => {
const path = node.split("/");
const name = path[path.length - 1];
if (searchedName === undefined) {
return name.startsWith("hci");
}
else {
return name === searchedName;
}
});
if (adapterNode === undefined)
throw new DBus.DBusError("org.bluez.Error.DoesNotExist", "Adapter not found");
return this.getAdapterFromObject(adapterNode);
}
listAdapters() {
return Promise.all(this.bluezRootObject.nodes.map((node) => {
return this.getAdapterFromObject(node);
}));
}
/**
* This registers a profile implementation.
* If an application disconnects from the bus all
* its registered profiles will be removed.
* HFP HS UUID: 0000111e-0000-1000-8000-00805f9b34fb
* Default RFCOMM channel is 6. And this requires
* authentication.
* Possible errors: org.bluez.Error.InvalidArguments
* org.bluez.Error.AlreadyExists
**/
registerProfile(profile, path) {
const wrappedProfile = new profileWrapper_1.ProfileWrapper(profile, this);
// register wrapped service
if (path === undefined) {
path = this.options.userInterfacesPath;
}
this.bus.export(path, wrappedProfile);
return this.profileManager.RegisterProfile(path, profile.UUID, (0, utilts_1.wrapDbusVariantObject)(profile.ProfileOptions, profile_1.ProfileOptionsSignature));
}
unregisterProfile(path) {
if (path === undefined) {
path = this.options.userInterfacesPath;
}
return this.profileManager.UnregisterProfile(path);
}
/**
* This registers an agent handler.
* The object path defines the path of the agent
* that will be called when user input is needed.
* Every application can register its own agent and
* for all actions triggered by that application its
* agent is used.
* It is not required by an application to register
* an agent. If an application does chooses to not
* register an agent, the default agent is used. This
* is on most cases a good idea. Only application
* like a pairing wizard should register their own
* agent.
* An application can only register one agent. Multiple
* agents per application is not supported.
* The capability parameter can have the values
* "DisplayOnly", "DisplayYesNo", "KeyboardOnly",
* "NoInputNoOutput" and "KeyboardDisplay" which
* reflects the input and output capabilities of the
* agent.
* If an empty string is used it will fallback to
* "KeyboardDisplay".
* Possible errors: org.bluez.Error.InvalidArguments
* org.bluez.Error.AlreadyExists
**/
async registerAgent(agent, requestAsDefault) {
const wrappedAgent = new agentWrapper_1.AgentWrapper(agent, this);
// register wrapped service
this.bus.export(this.options.userInterfacesPath, wrappedAgent);
await this.agentManager.RegisterAgent(this.options.userInterfacesPath, agent.AgentCapabilities);
if (requestAsDefault) {
await this.agentManager.RequestDefaultAgent(this.options.userInterfacesPath);
}
}
unregisterAgent(agent) {
return this.agentManager.UnregisterAgent(this.options.userInterfacesPath);
}
/**
* Gat an Adapter from at that path.
* For node > 14.6 Adapters are cached using WeakRefs
* @param object
*/
async getAdapterFromObject(object) {
var _a;
// We should cache adapters since they have event listeners attached to the ObjectManager
if (HAS_WEAKREF) {
const adapter = (_a = this.adapterCache.get(object)) === null || _a === void 0 ? void 0 : _a.deref();
if (adapter)
return adapter;
}
const obj = await this.bus.getProxyObject("org.bluez", object);
const adapter = new adapter_1.Adapter(obj, this);
if (HAS_WEAKREF) {
this.adapterCache.set(object, new WeakRef(adapter));
}
return adapter;
}
/**
* Get a Device Interface at the Object.
* Shortcut for `Bluez.getDbusObjectInterface(Device, object)`
* @param object
*/
async getDeviceFromObject(object) {
return this.getDbusObjectInterface(device_1.Device, object);
}
/**
* Get an low level dbus interface from the given object path
* @param type Low level dbus interface from ./dbus/*
* @param object dbus object path
*/
async getDbusObjectInterface(type, object) {
const obj = await this.bus.getProxyObject("org.bluez", object);
return new type(obj, this);
}
}
exports.Bluez = Bluez;
//# sourceMappingURL=bluez.js.map