Coder Social home page Coder Social logo

node-bluez's Introduction

Bluez D-Bus

Easy to use Node.js Bluez5 D-Bus library.

Install

Required Packages:

  • libglib2.0-dev
  • libdbus-1-dev
npm install bluez

Usage

const Bluez = require('bluez');

const bluetooth = new Bluez();

// Register callback for new devices
bluetooth.on('device', async (address, props) => {
    console.log("Found new Device " + address + " " + props.Name);
});

// Initialize bluetooth interface
bluetooth.init().then(async ()=>{
    // listen on first bluetooth adapter
    const adapter = await bluetooth.getAdapter('hci0');
    await adapter.StartDiscovery();
    console.log("Discovering");
});

Custom Agents and Profiles can be implemented by extending Agent / Profile base classes. Then use bluez.registerAgent(agent, capability) and bluez.registerProfile(profile, options) to activate them.

Examples

Have a look at the examples for more detailed usage information.

API

Bluez

Contains most management functions.

constructor(options?: BluezOptions): Bluez

options:

  • bus: DBus | undefined: an existing DBus connection. Default: create new connection
  • service: DBus.Service | string | null | undefined: service where to register default Profiles / Agents. Default: null (connection local service)
  • objectPath: string | undefined: object path where to register default Profiles / Agents. Default: "/org/node/bluez"
init(): Promise<void>

Initializes DBus and interfaces. MUST be called bevor any bluetooth interaction is done. Attach device event listeners first, because calling init() will emit device events for paired devices.

registerProfile(profile: Profile, options: ProfileOptions): Promise<void>

Registers a Profile Bluetooth Service.

For available options see Bluez Docs.

registerAgent(agent: Agent, capabilities: string): Promise<void>

Registers a Agent required for pairing.

For available options see Bluez Docs.

getDevice(address: string): Promise<Device>

Returns a Device for a given address.

address might be a adress in format XX:XX:XX:XX:XX:XX or XX_XX_XX_XX_XX_XX or /org/bluez/hciX/dev_XX_XX_XX_XX_XX_XX

Events
on("device", address: string, props: DeviceProperties)

Emitted if a device was discovered.

Note that for paired devices this will be emitted no matter if the devices are in range or not.

Adapter

A Adapter represents a local Bluetooth adapter.

constructor(interface: DBus.Interface): Adapter

interface is the DBus Interface corresponding the the Adapter.

Should not be called directly. Use Bluez.getAdapter().

For available methods and properties see Bluez Docs.

Agent

A Agent is required for pairing with devices.

This default implementation accepts every pair request and has the passkey 1234

constructor(bluez: Bluez, DBusObject: DBus.ServiceObject): Agent

DBusObject is the DBus Object under witch the interface should be registerd.

For available methods and properties see Bluez Docs.

Device

A Device represents a remote Bluetooth device.

constructor(interface: DBus.Interface): Device

interface is the DBus Interface corresponding the the Device.

Should not be called directly. Use Bluez.getAdapter().

For available methods and properties see Bluez Docs.

Profile

A Profile is a service provided by this Bluetooth device.

constructor(bluez: Bluez, DBusObject: DBus.ServiceObject): Profile

DBusObject is the DBus Object under witch the interface should be registerd.

get uuid: string

returns the service UUID.

For other available methods and properties see Bluez Docs.

SerialProfile

A Profile implementation for Serial communication.

constructor(bluez: Bluez, DBusObject: DBus.ServiceObject, listener: (device: Device, socket: RawFdSocket)=>void): SerialProfile

listener is a callback that is called for each new connection to the Profile. Its socket parameter is the established channel between the two devices.

RawFdSocket

A stream.Duplex implementation similar to net.Socket that is able to use RFCOMM sockets provieded by NewConnection callback of Profiles.

constructor(fd: int, options: stream.Duplex.options): RawFdSocket

TODO

  • Complete the API docs
  • More examples
  • Tests

node-bluez's People

Contributors

mateussilva92 avatar niels-be avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.