Skip to content

Implementation of the basic interaction with the USB device through Android Open Accessory (AOA) protocol

License

Notifications You must be signed in to change notification settings

vldmkr/android-accessory-interface

Repository files navigation

android-accessory-interface

Release

Implementation of the basic interaction with the USB device through Android Open Accessory (AOA) protocol.

You can find examples of interaction with FT311D, Arduino and so on in the android-accessory-bootstrap repository.

Installation

Add it to your build.gradle with:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

and:

dependencies {
    compile 'com.github.vldmkr:android-accessory-interface:{latest version}'
}

Documentation

public abstract class AccessoryInterface

This is an abstract class that implements the basic interaction with the USB device connected as USB host through Android Open Accessory (AOA) protocol.

Not all devices can support accessory mode. Suitable devices can be filtered using an <uses-feature> element in the AndroidManifest.

Communication with the code on the application level is carried out through the android.os.Handler. Posted item will be processed as soon as the message queue will be ready to do so. Although this does not guarantee high speed of android.os.Message processing, but it satisfies the requirements.

The methods create and destroy must be called from the appropriate antagonistic callbacks of the application's life cycle, such as android.app.Activity#onResume and android.app.Activity#onPause.

The methods getManufacturer, getModel, getVersion are used to identify the USB accessory and must be implemented in the extended class.

The callback method requires an override if the extended class does not use a communication android.os.Handler.

protected AccessoryInterface(final Handler communicationHandler, final int bufferSize)

public final void create(final Context context)

This method finds and opens an attached USB accessory if the caller has permission to access the accessory. Otherwise, the corresponding runtime permission request will be sent.

public final void destroy(final Context context)

This method closes the connection with USB accessory if it is attached.

  • Parameters: context — Context, which is accepted in the create method.

protected void callback(Message msg)

If communicationHandler param of AccessoryInterface is null, messages are pushed to this callback.

public abstract String getManufacturer()

  • Returns: The implementation must not return null, it is used to identify the USB accessory.

public abstract String getModel()

  • Returns: The implementation must not return null, it is used to identify the USB accessory.

public abstract String getVersion()

  • Returns: The implementation must not return null, it is used to identify the USB accessory.

protected final void write(byte[] data)

Send data to the USB device. java.nio.channels.FileChannel from the non-blocking IO package is used.

  • Parameters: data — The data array to send.

protected final void directWrite(byte[] data, int byteOffset, int byteCount)

Send data to the USB device. Blocking operation. java.io.FileOutputStream is used.

  • Parameters:
    • data — The data array to send.
    • byteOffset — The offset to the first byte of the data array to be send.
    • byteCount — The maximum number of bytes to send.

The documentation is built using Javadoc-to-Markdown

About

Implementation of the basic interaction with the USB device through Android Open Accessory (AOA) protocol

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages