GitXplorerGitXplorer
e

node-termux-api

public
35 stars
16 forks
3 issues

Commits

List of commits on branch master.
Verified
54541be00e6eb67904c267e76b2965930d284810

Merge pull request #1 from h0ru5/master

eedupsousa committed 7 years ago
Verified
0723e9599e50b979e05047f9af7f5b86adf8421f

Update readme.md

eedupsousa committed 7 years ago
Unverified
6291690229334587cec872e11539b12bc12a0219

moving types to devDeps

hh0ru5 committed 8 years ago
Unverified
c9442272ce8cfbbde91fe18e9ae55bd284555650

adding and adjusting for node typings

hh0ru5 committed 8 years ago
Unverified
34ed708b522846fdd919138170c3e35682fb54b9

Added readme.md

eedupsousa committed 8 years ago
Unverified
c399b19817de707b7d37b5cb30f6cc5c29b74c83

Changed config for publishing to NPM

eedupsousa committed 8 years ago

README

The README file for this repository.

Termux-API Wrapper for Node.JS

This Repository is Unmaintained and Archived (Read-Only)

Unfortunately, due to my work, i could not maintain this repository updated at this moment.

This project aims to create a simple interface to invoke Termux-API commands directly from Node.JS applications. To use it you need to have Termux and Termux-API installed on your Android, and Node.JS installed on the Termux environment.

What is Termux?

From Termux Website: "Termux is a terminal emulator and Linux environment bringing powerful terminal access to Android."

With Termux you can run a small linux environment on your Android device, that means that you can run a small command-line utilities or a full-featured webserver directly from your Android phone.

Install Termux from Google Play

Termux-API

Termux-API is a companion app to Termux, it creates an interface from Termux to your device features, with Termux-API you can take photos from your phone camera, get location from GPS, read or send SMS, and a lot of other features. Everything directly from the emulated Linux terminal.

Install Termux-API from Google Play

Node.JS on Termux

Termux brings the possibily of running Node.JS on your Android device by simply installing it with APT (without rooting):

apt update
apt install nodejs

Usage

Install the package on yout NPM project by:

npm install --save termux-api

Import the default object on JavaScript:

var api = require('termux-api').default;

Or with TypeScript:

import default as api from 'termux-api';

Now you have a instance of the class TermuxApi as the api variable. To invoke API commands you must build it using the method createCommand().

The method createCommand() returns an instance of the ApiCommandFactory where you find one method for each API command you could invoke on Termux-API app. For example, to show a small popup, or toast for those familiarized with Android UI you can use:

api.createCommand()
    .toast()
    .setText('Can you see me?')
    .shortDuration()
    .build()
    .run();
  • line 1: api.createCommand() returns an instance of ApiCommandFactory class.
  • line 2: ApiCommandFactory.toast() returns an instance of ToastBuilder class.
  • line 3: ToastBuilder.setText() sets the text to be displayed and returns the ToastBuilder himself.
  • line 4: ToastBuilder.shortDuration() configures the toast to be displayed for a short while and returns himself (ToastBuilder).
  • line 5: ToastBuilder.build() builds and return a ApiCommand instance.
  • line 6: The command is run.

The process for calling other commands are very similar, you only need to change the command (line 2) and the options (lines 3 and 4). For example, to vibrate the phone for 1 second (1000 ms):

api.createCommand()
    .vibrate()
    .setDuration(1000)
    .build()
    .run();

You can see all commands available on ApiCommandFactory source-code. And options for each command are availabe on the correspondent class in the builders folder.

Returning data from the API

Some API commands return data as String or JSON. To gather that data you could use the instance of ApiResult class returned by the method run() by invoking the methods getOutputObject() or getOutputString().

Notice that those 2 methods executes asynchronously returning ES6 Promises that resolves to an Object ou a String, respectively.

For example to get a location update from GPS and show the returned object on console:

let result = api.createCommand()
            .location()
            .fromGPSProvider()
            .requestOnce()
            .build()
            .run();

result.getOutputObject()
    .then(function(location) {
        console.log('Last known location: ', location);
    });