Constructor
# new TurnTouch(poweredNoble)
Parameters:
Name | Type | Description |
---|---|---|
poweredNoble |
Noble | Instance of the Noble BLE library. |
- Implements:
- events.EventEmitter
Example
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.
# 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. |
# 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.
If the connection with the remote could not be established.
Promise that resolves when done.
# async getBatteryLevel() → {Promise.<number>}
Read the battery level from the remote's battery service.
If reading the battery level failed somehow.
Promise that resolves with the battery level when done or rejects with an error upon failure.
# 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
|
# 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.
# 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.
# 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.
Promise that resolves when done or when an error has occurred.
# async setupBatteryGauge() → {Promise}
Start gauging the battery level of the remote continuously, at a set
interval of BATTERY_POLL_TIMEOUT_MS
milliseconds.
If there's no connection with the remote yet or when the battery service can not be discovered.
Promise that resolves when done.
# 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.
If there's no connection with the remote yet, when the button service can not be discovered or when subscription to button events fails.
Promise that resolves when done.