Class

TurnTouch

TurnTouch(poweredNoble)

TurnTouch class, which allows you to connect to your TurnTouch remote, query it for info and receive events from it.

Constructor

# new TurnTouch(poweredNoble)

Parameters:
Name Type Description
poweredNoble Noble

Instance of the Noble BLE library.

Implements:
  • events.EventEmitter

View Source index.js, line 92

Example

Simple bootstrapping

var Noble = require("noble");
var TurnTouch = require("turntouch");

var gTurnTouch;

Noble.on("stateChange", function(state) {
  console.log("Noble state change.", state);
  if (state == "poweredOn") {
    gTurnTouch = new TurnTouch(Noble);
    gTurnTouch.on("button", button => {
      console.log("BUTTON EVENT", button);
    });
    gTurnTouch.on("battery", batteryLevel => {
      console.log("BATTERY LEVEL", batteryLevel + "%");
    });
    gTurnTouch.on("error", err => console.error(err));
  } else {
    process.exit();
  }
});

process.on("exit", function() {
  Noble.stopScanning();
  if (gTurnTouch) {
    gTurnTouch.disconnect();
  }
});

Methods

# batteryGaugePoll()

Start polling for the battery level every BATTERY_POLL_TIMEOUT_MS milliseconds.

View Source index.js, line 370

# createEvent(buttonString) → {ButtonEvent}

Creates a nice, inspectable event object that

Parameters:
Name Type Description
buttonString string

Textual representation of the button event that was captured.

View Source index.js, line 289

ButtonEvent

# disconnect()

Disconnect from the remote and clean up thoroughly.

View Source index.js, line 415

# async discoverAndConnect() → {Promise}

Starts scanning for Bluetooth devices and tries to filter out the TurnTouch remote. When it's discovered, and attempt to make a connection is done. This method is called by TurnTouch#setup, so you probably won't need it.

See:

View Source index.js, line 151

If the connection with the remote could not be established.

Error

Promise that resolves when done.

Promise

# async getBatteryLevel() → {Promise.<number>}

Read the battery level from the remote's battery service.

View Source index.js, line 396

If reading the battery level failed somehow.

Error

Promise that resolves with the battery level when done or rejects with an error upon failure.

Promise.<number>

# onButtonData(data)

Event handler, invoked when a button event comes in through the subscription as made in TurnTouch#setupButtonListener.

Parameters:
Name Type Description
data Buffer

NodeJS Buffer object that signifies the button event. This should match with one of the hex codes in the BUTTONS map (see source code).

View Source index.js, line 237

# onDisconnect()

Event handler that fired when the connection between Noble and the remote is gone. It will re-connect automatically when the disconnect was unintential, i.e. without having called TurnTouch#disconnect.

View Source index.js, line 442

# resetButtonTracker()

Part of the double-tap debouncing mechanism; it makes sure that a certain amount of time between taps is allowed to not send events for a single AND double tap, but to de-duplicate. When a button is held down, this method is responsible for firing multiple ButtonEvents every HOLD_REPEAT_TIMEOUT_MS milliseconds.

View Source index.js, line 317

# async setup() → {Promise}

Powers on the Noble instance, if it's not running yet, and attempts to discover and connect to the TurnTouch remote. Once the connection is established successfully, it'll start listening for button events and start the battery gauge.

View Source index.js, line 115

Promise that resolves when done or when an error has occurred.

Promise

# async setupBatteryGauge() → {Promise}

Start gauging the battery level of the remote continuously, at a set interval of BATTERY_POLL_TIMEOUT_MS milliseconds.

See:

View Source index.js, line 343

If there's no connection with the remote yet or when the battery service can not be discovered.

Error

Promise that resolves when done.

Promise

# async setupButtonListener() → {Promise}

Discover the button service of the remote and attempts to subscribe to data from it, which will contain button press events. This method is called by TurnTouch#setup, so you probably won't need it.

See:

View Source index.js, line 197

If there's no connection with the remote yet, when the button service can not be discovered or when subscription to button events fails.

Error

Promise that resolves when done.

Promise

Events

number

# battery

Battery event.

View Source index.js, line 381

ButtonEvent

# button

Button event.

View Source index.js, line 276

Error

# error

Error event.

View Source index.js, line 98