Cordova plugin for Android Serial USB communication (easily connect an Arduino board to an Android device).

Overview

PR-DC cordova-plugin-serialusb

Cordova plugin for Android Serial USB communication. This plugin makes a connection to the external board trivial, for example, you can connect an Arduino board to an Android device and get power, storage, a high-quality touch interface and many possible ways of connecting with the rest of the world (mobile network, WiFi, Bluetooth...).

Requirements

Apache Cordova

This library is tested with Cordova Android 9.1.0

Installation

From the root folder of your Cordova project, run:

cordova plugin add https://github.com/PR-DC/cordova-plugin-serialusb.git

Usage

Thanks to usb-serial-for-android library, you can communicate with CDC, FTDI, Arduino and other devices.

Copy device_filter.xml document to www/res/xml.

Append the following to the project's config.xml document within the Android platform-specific configuration.

">
<config-file parent="/manifest/application/activity[@android:name='MainActivity']" target="AndroidManifest.xml">
    <intent-filter>
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    intent-filter>
    <meta-data
      android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
      android:resource="@xml/device_filter" />
config-file>
<resource-file src="www/res/xml/device_filter.xml" target="app/src/main/res/xml/device_filter.xml" />

Append following preference to project's config.xml document.

">
<preference name="AndroidXEnabled" value="true" />

Now you can proceed with the app code, request permission to use the serial port to the system:

SerialUSB.requestPermission(function success(), function error());

After permission is granted you can open the serial port:

SerialUSB.open(opts, function success(), function error());

opts is a JSON object with the following properties:

  • baudRate: defaults to 9600
  • dataBits: defaults to 8
  • stopBits: defaults to 1
  • parity: defaults to 0
  • dtr: defaults to false (it may be needed to be true for some Arduino)
  • rts: defaults to false (it may be needed to be true for some modules)
  • sleepOnPause: defaults to true. If false, the OTG port will remain open when the app goes to the background (or the screen turns off). Otherwise, the port automatically closes and resumes once the app is brought back to the foreground.

You're now able to read and write:

SerialUSB.write(data, function success(), function error());
SerialUSB.read(function success(buffer), function error());

data is the string representation to be written to the serial port. buffer is a JavaScript ArrayBuffer containing the data that was just read.

Apart from using SerialUSB.write(), you can also use SerialUSB.writeHex() to have an easy way to work with RS232 protocol driven hardware from your javascript by using hex-strings.

In a nutshell, SerialUSB.writeHex('ff') would write just a single byte where SerialUSB.write('ff') would let java write 2 bytes to the serial port.

Apart from that, SerialUSB.writeHex() works the same way as SerialUSB.write() does.

Register a callback that will be invoked when the driver reads incoming data from your serial device. The success callback function will receive an ArrayBuffer filled with the data read from serial:

SerialUSB.registerReadCallback(
  function success(data){
    var view = new Uint8Array(data);
    console.log(view);
  },
  function error(){
    new Error("Failed to register read callback");
  });

Register a callback that will be invoked when the device is detached:

SerialUSB.detached(
  function(success_message) {
    
  }, function(err) {
    console.log("Device detached!");
  }
);

And finally, you can also close the port with:

SerialUSB.close(function success(), function error())

Example with Cordova app and Arduino sketch

An example of connecting Cordova application to Arduino board is available at: https://github.com/PR-DC/PRDC_TestSerialUSB

Change log

2021.12: Miloš Petrašinović: renamed to cordova-plugin-serialusb (SerialUSB as in Arduino) and implemented detached event.

2018.02: Dario Cavada: renamed to cordova-plugin-usbserial and refactory internal to follow cordova naming convention.

2015.10: Ed. Lafargue: Implemented "sleepOnPause" flag in the 'open' options to prevent closing the OTG port when app goes to background.

2014.08: Zevero: Option to find device by VID and PID, that let you use "unrecognized" devices.

2014.07: Hendrik Maus: Implemented writeHex for working with RS232 protocol, i.e. javascript can now pass "ff", java turns it into a 1 byte array and writes to the serial port - naturally, java, and the existing write method here, would create a 2 byte array from the input string.

2014.04: Derek K: Implemented registerReadCallback for evented reading and Android onPause/onResume

2014.03: Ed. Lafargue: Implemented read(). The success callback returns a Javascript ArrayBuffer which is the best way to handle binary data in Javascript. It is straightforward to convert this to a string if required - a utility function could be implemented in this plugin.

2013.11: Xavier Seignard: First implementation

License

The MIT License

Copyright (c) 2021 Miloš Petrašinović PR-DC. https://pr-dc.com

Copyright (c) 2015 Xavier Seignard. http://drangies.fr

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

You might also like...

Spigot plugin featuring a wide variety of features for a server based on modules.

CTSNC, standing for Custom Chat, Tablist, Scoreboard, NameTag & Chat, is a all-round solution based on multiple modules each featuring a dedicated function while CTSNC acts as the core. Here all configuration files are housed for easy management and customization.

Dec 30, 2022

An open source, modular alternative of sketchware. Create your own app in android using block programming like scratch!

An open source, modular alternative of sketchware. Create your own app in android using block programming like scratch!

OpenBlocks An open source, modular alternative of sketchware. Create your own app in android using block programming like scratch! What is OpenBlocks?

Dec 16, 2022

An easy to use Hindi keyboard Android app

AasaanHindi-Keyboard-app An easy to use Hindi keyboard Android app. Easy and fast Hindi typing.

Jan 20, 2022

An android application to make students life easier by reducing their backpack weight.

An android application to make students life easier by reducing their backpack weight.

Smart-Schooling An android application to make students life easier by reducing their backpack weight. Dont forget to ⭐ the repo Overview Almost every

Jan 31, 2022

A todo app with Alan AI smart voice assistant android application

A todo app with Alan AI smart voice assistant android application

Todo App A todo app with Alan AI smart voice assistant android application. Experience a hands-free voice app where you can add, edit, delete and make

Jan 26, 2022

Crackersanimator is a particle system library that works with the standard Android UI

Crackersanimator is a particle system library that works with the standard Android UI

Crackersanimator is a particle system library that works with the standard Android UI. This library build from https://github.com/plattysoft/Leonids library but make some update to support for latest version of android.

Jun 14, 2022

RealmeDirac - an open-source Android application written in java

RealmeDirac - an open-source Android application written in java

RealmeDirac is an open-source Android application written in java. RealmeDirac is a feature introduced by realme & dirac in realmeUI for sound optimisation, trying to recreate same thing, this is scratch written app aiming to provide same features as realmeDirac..

Feb 21, 2022

Arduino-Bluetooth-Connect - This is an Arduino Bluetooth Connect App that works as a Serial Monitor.

Arduino-Bluetooth-Connect An app that works as a Serial Monitor. This app can connect to Arduino through Bluetooth HC-05 Module. Commands can be sent

Aug 6, 2022

Simple Cordova plugin to save a pdf file in MediaStore.Downloads

com-thesis-plugins-pdfstore This simple Cordova plugin saves a bytestring (the byte image of a pdf) to a pdf file in MediaStore.Downloads. The purpose

Feb 2, 2022

Live video out from your DJI FPV Goggles via USB.

Live video out from your DJI FPV Goggles via USB.

DigiView is an Android app that allows you to get a live preview from your DJI FPV Goggles (V1 & V2). Working with Android 7+ and devices supporting U

Dec 19, 2022

Automated door opening system w/GUIs programmed in Java and Arduino

Automated door opening system w/GUIs programmed in Java and Arduino

The Automated Door Opening System is a major project created in both Java and Arduino for a first year Engineering course. This project is an iteration of my MATLAB version of this project.

Jan 28, 2022

An implementation of the classic board game.

An implementation of the classic board game.

General Information An implementation of the classic board game Monopoly. Made by five students at BTU Cottbus-Senftenberg (Chair of Software Engineer

Mar 29, 2022

Tabletop Games Framework (TAG) - a Java-based benchmark for developing modern board games for AI research

Tabletop Games Framework (TAG) - a Java-based benchmark for developing modern board games for AI research

The Tabletop Games Framework (TAG) is a Java-based benchmark for developing modern board games for AI research

Dec 12, 2022

ASCII Roguelike with Euro Strategy Board Game Influences

EuroRogue ASCII Roguelike with Euro Strategy Board Game Influences --Run the RunEuroRogue.bat file in the bin folder. Requires Java - https://www.java

Feb 1, 2022

VelocityControl is a BungeeControl-fork plugin enabling ChatControl Red to connect with your Velocity network.

VelocityControl is a BungeeControl-fork plugin enabling ChatControl Red to connect with your Velocity network.

Oct 24, 2022

Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.

Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.

EventBus EventBus is a publish/subscribe event bus for Android and Java. EventBus... simplifies the communication between components decouples event s

Jan 3, 2023

Ribbon is a Inter Process Communication (remote procedure calls) library with built in software load balancers. The primary usage model involves REST calls with various serialization scheme support.

Ribbon Ribbon is a client side IPC library that is battle-tested in cloud. It provides the following features Load balancing Fault tolerance Multiple

Jan 1, 2023
Releases(1.0.0)
Owner
PR-DC
Aerospace company - Pink Research & Development Center
PR-DC
A small program to allow you to easily generate fabric mods

FabricModGenerator A small program to allow you to easily generate fabric mods. Most of the code was taken from GeneratorFabricMod and TemplateMakerFa

Aaron 2 Mar 3, 2022
CodeLocator is a toolset that includes Android SDK and Android Studio plugins

CodeLocator is a toolset that includes Android SDK and Android Studio plugins. It has the following functions(Support Mac Only):

Bytedance Inc. 948 Jan 5, 2023
STxMobile is a proof of concept of an Android app for remotely controlling the Raymarine ST2000+ tiller pilot from an Android phone.

STxMobile is a proof of concept of an Android app for remotely controlling the Raymarine ST2000+ tiller pilot from an Android phone.

Marco 8 Sep 1, 2022
A theoretically-infinitely-scalable Skyblock plugin.

hyperblock A theoretically-infinitely-scalable Skyblock plugin. wat Worlds are persisted in S3 buckets, write-through cached in Redis.

amy null 16 Aug 10, 2022
A lightweight staff chat plugin for BungeeCord and Spigot.

A lightweight staff chat plugin for BungeeCord and Spigot with 2-way Discord chat support and many other great features.

null 4 Mar 30, 2022
A simple ping plugin for amazing people.

SimplisticPing Download: https://github.com/LoJoSho/SimplisticPing/releases Paper Forum Post: https://forums.papermc.io/threads/simplisticping-a-ping-

null 3 Jan 9, 2022
Prism (Refracted) is a change-tracking plugin for Bukkit-based servers

Prism (Refracted) is a change-tracking plugin for Bukkit-based servers. Supports rollbacks, restores, previews, wands, and so much more. Tracking so good, the NSA stole our name.

Darkhelmet 29 Dec 30, 2022
A plugin for the ja-netfilter, it can delete expired log files

A plugin for the ja-netfilter, it can delete expired log files

EFL 3 Feb 14, 2022
Lightweight and easy-to-use SkinChangerAPI for Bukkit plugin

Lightweight and easy-to-use SkinChangerAPI for Bukkit plugin

Gabriel MERCIER 6 Jul 1, 2022
A Nexus Repository 3 plugin that allows usage of Terraform repositories

Nexus Repository Terraform Format Table Of Contents Developing Requirements Download Building Using Terraform with Nexus Repository Manager 3 Compatib

null 8 Dec 5, 2022