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...
Sources for the LiveBoot app for rooted Android devices

This is the sauce for the LiveBoot app. License Copyright © 2011-2020 Jorrit Chainfire Jongma This code is released under the GPLv3. LICENSE, COPYING.

android logcat
android logcat

android logcat

A local ADB shell for Android!

LADB A local ADB shell for Android! How does it work? LADB bundles an ADB server within the app libraries. Normally, this server cannot connect to the

btrace(AKA RheaTrace) is a high performance Android trace tool which is based on Systrace
btrace(AKA RheaTrace) is a high performance Android trace tool which is based on Systrace

btrace README 中文版 btrace(AKA RheaTrace) is a high performance Android trace tool

Android QA/Debug tools to speed up and streamline the development progress.
Android QA/Debug tools to speed up and streamline the development progress.

Android Dev Tools is a library that contains various QA/Debug tools to speed up and streamline the development progress.

Android camera and serial communication utility that interacts with another device via a USB connection.

PVIT-Payload-Source Android camera and serial communication utility that interacts with another device via a USB connection. PVIT = Palos Verdes Insti

Android USB host serial driver library for CDC, FTDI, Arduino and other devices.

usb-serial-for-android This is a driver library for communication with Arduinos and other USB serial hardware on Android, using the Android USB Host M

An android & arduino project. The android app is used to control motors via Arduino.
An android & arduino project. The android app is used to control motors via Arduino.

Boccia ramp project An android & arduino project. The android app is used to control motors via Arduino. Description This project is made in the conte

[UNMAINTAINED][Android] Bluetooth Serial Port Profile which comfortable to developer application to communication with microcontroller via bluetooth
[UNMAINTAINED][Android] Bluetooth Serial Port Profile which comfortable to developer application to communication with microcontroller via bluetooth

⚠ WARNING: This project is no longer being maintained Android-BluetoothSPPLibrary Bluetooth Serial Port Profile which comfortable to developer applica

Smooth communication via bluetooth with other android devices or microcontrollers such as Arduino.

Android Smooth Bluetooth Smooth communication via bluetooth with other android devices or microcontrollers such as Arduino. Getting Started Add Gradle

Connect adb from devices to androidstudio by wifi without usb anymore
Connect adb from devices to androidstudio by wifi without usb anymore

WIFI ADB There are two projects in the repository. The first one is an android project,and the second is an intellij plugin project. You can see more

Apache Cordova Android

Cordova Android Cordova Android is an Android application library that allows for Cordova-based projects to be built for the Android Platform. Cordova

A webserver interface to the same methods and code that Cordova plugins install.

cordova-plugin-webserver Cordova plugin for localhost web server written in Kotlin and Ktor Install plugin cordova plugin add https://github.com/Qbix/

Connect is an Android Application to connect people for a project and can then work on the application to efficiently complete the project
Connect is an Android Application to connect people for a project and can then work on the application to efficiently complete the project

Connect is an Android Application to connect people for a project and can then work on the application to efficiently complete the project

This project aims to simplify creation of basic Arduino programs by just editing a UI on Android.
This project aims to simplify creation of basic Arduino programs by just editing a UI on Android.

ArdUI A video explanation If you are more a fun of video explanation go to this youtube video Project aim This project aims to simplify creation of ba

This application will provide information about almost all currently plugged-in USB devices.

USB Device Info Android 3.1 introduced USB host mode which allows the user to plug USB devices to your Android tablet in the same way as a Desktop PC

A Kotlin/Java library to connect directly to an Android device without an adb binary or an ADB server
A Kotlin/Java library to connect directly to an Android device without an adb binary or an ADB server

dadb Blog Post: Our First Open-Source Project A Kotlin/Java library to connect directly to an Android device without an adb binary or an ADB server de

Therapeutic is a platform to help easily connect patients or generally anyone struggling to get through tough times with motivating content and  professional therapists.
Therapeutic is a platform to help easily connect patients or generally anyone struggling to get through tough times with motivating content and professional therapists.

Therapeutic Apk - https://github.com/develNerd/Therapeutic/blob/main/androidApp/release/androidApp-release6.apk Therapeutic is a Kotlin Mobile Multipl

Drag and drop to reorder items in a list, grid or board for Android. Based on RecyclerView. Also supports swiping items in a list.
Drag and drop to reorder items in a list, grid or board for Android. Based on RecyclerView. Also supports swiping items in a list.

DragListView DragListView can be used when you want to be able to re-order items in a list, grid or a board. It also supports horizontal swiping of it

Releases(1.0.0)
Owner
PR-DC
Aerospace company - Pink Research & Development Center
PR-DC
Pluto: An on-device debugging framework for Android applications

Pluto is an on-device debugging framework for Android applications, which helps in the inspection of HTTP requests/responses, captures Crashes, and ANRs, and manipulates application data on the go.

Pluto 550 Dec 27, 2022
Under the Hood is a flexible and powerful Android debug view library. It uses a modular template system that can be easily extended to your needs, although coming with many useful elements built-in.

Under the Hood - Android App Debug View Library Under the Hood is a flexible and powerful Android debug view library. It uses a modular template syste

Patrick Favre-Bulle 217 Nov 25, 2022
Stetho is a debug bridge for Android applications, enabling the powerful Chrome Developer Tools and much more.

Stetho Stetho is a sophisticated debug bridge for Android applications. When enabled, developers have access to the Chrome Developer Tools feature nat

Facebook 12.6k Jan 7, 2023
A library for debugging android databases and shared preferences - Make Debugging Great Again

Android Debug Database Android Debug Database is a powerful library for debugging databases and shared preferences in Android applications Android Deb

AMIT SHEKHAR 8.1k Dec 29, 2022
🔪Swiss-army knife for Android testing and development 🔪 ⛺

ADB Enhanced ADB-Enhanced is a Swiss-army knife for Android testing and development. A command-line interface to trigger various scenarios like screen

Ashish Bhatia 938 Dec 20, 2022
Android library to record the network calls through the interceptor mechanism of the http clients.

Android Snooper Introduction Android Snooper is a library which helps in debugging issues while running the applications on android devices. One of th

Prateek 151 Nov 25, 2022
A Read-Eval-Print-Loop server for Android and SQLite

Android DebugPort Android DebugPort is a drop-in utility which allows you to write and execute code within your app's context, at runtime, and from th

Jason Feinstein 148 Nov 14, 2022
traffic debugging library for android

TrafficMonitor About Display traffic per Activity.Observing traffic is used by TrafficStats API. OkHttp Interceptor observer is implementing. Demo Bai

Tetsuya Masuda 17 Feb 4, 2022
Easy android exception tracer and handler.

Introduction Lup is a small android library that can help you to tracking bug that causes application stopped working (force close). Whiting this libr

icodeu 4 Sep 29, 2022