Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.vldmkr:android-accessory-interface:1.0.0'
}
dependencies {
implementation("com.github.vldmkr:android-accessory-interface:1.0.0")
}
<dependency>
<groupId>com.github.vldmkr</groupId>
<artifactId>android-accessory-interface</artifactId>
<version>1.0.0</version>
</dependency>
libraryDependencies += "com.github.vldmkr" % "android-accessory-interface" % "1.0.0"
:dependencies [[com.github.vldmkr/android-accessory-interface "1.0.0"]]
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}'
}
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)
communicationHandler
— android.os.Handler involved in the communication with application-level code.
If it is null, messages are processed by the internal android.os.Handler and
pushed to callback as a parameter.bufferSize
— Expected data size of the java.io.FileInputStream from the USB device.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.
context
— android.content.Context for which the android.content.BroadcastReceiver will be registered.public final void destroy(final Context context)
This method closes the connection with USB accessory if it is attached.
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.
msg
— android.os.Message received after processing by internal android.os.Handler.public abstract String getManufacturer()
public abstract String getModel()
public abstract String getVersion()
protected final void write(byte[] data)
Send data to the USB device. java.nio.channels.FileChannel from the non-blocking IO package is used.
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.
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