Monday, April 20, 2015

My first Github Repository - USBSerialDevice

I've finally created my own github repo that hosts a Android Java class called USBSerialDevice for Android. It wraps around mik3y's usb-serial-for-android library but has the added bonus of including the USB permission and enumeration code. Having to constantly make Android apps that collect data from serial devices, it'd make much more sense to create this so that I don't have to copy-paste my code that does the USB enumeration and permission request (it's a lot of lines of code to copy every single time). In this repository, I've included an example subclass called ArduinoSensor as I play with Android-to-Arduino connections a lot.

Notes on usage: As stated on the README.md, protected method processData(byte buffer[], int numBytesRead) needs to be overridden if you want to actually do anything with the data, otherwise it just writes the output to a file.

Example usage:

USBSerialDevice mySerialDev = new USBSerialDevice(this, 1027, 19200, 10){
            @Override
            protected void processData(byte[] buffer, int numBytesRead) {
                // Print out the buffer to log or something
            }
        };
mySerialDev.setDirectory("/sdcard/mydirectory/");
mySerialDev.setFileNameUsingTimestamp("ARDUINO");
mySerialDev.setRecord(true);
mySerialDev.start();

No fuss, no muss. Much easier than having to copy the 100+ lines of code for USB broadcast receiver, intent filters, usb device listing, etc

No comments :

Post a Comment