Skip to content

Repository files navigation

Android-USB

The existing samples that ilustrate how to use the Android USB API are a little messy. This apply for both modes of USB Host and Accessory. This library is made with the purpose of transform the existing API into a more Socket oriented API. The purpose is to remove the boilerplate code of scanning for attached devices, select a specific device and then create a connection.

dependencies {
implementation "com.letmeaccess:usb:1.0.0"
}

Using the library

privateUsbAoaManagermUsbAoaManager = newUsbAoaManager(Context);
privateSocketmSocket;
mUsbAoaManager.probe(newUsbAoaManager.Listener() {
@OverridepublicvoidonSelectAccessory(UsbAccessory[] accessoryArray) {
mUsbAoaManager.createSocket(accessoryArray[0], mAccessoryListener);
}
@OverridepublicvoidonSocketCreated(Socketsocket) {
mSocket = socket;
mSocket.open();
}
});
privateSocket.AccessoryListenermAccessoryListener = newSocket.AccessoryListener() {
@OverridepublicvoidonError(Socket.AccessoryErrorerror) {
isUsbSocketOpen = false;
}
@OverridepublicvoidonOpen() {
isUsbSocketOpen = true;
mSocket.write(newbyte[]{(byte)'h',(byte)'e',(byte)'l',(byte)'l',(byte)'o'});
}
@OverridepublicvoidonRead(byte[] data) {
Timber.d("AccessoryListener.onRead() -> " + newString(data));
}
};

Enjoy and feel free to PR.

About

A library that transform the Android USB API to an easy to use Socket API. Have support for both modes Host and Accessory.

Resources

Stars

10 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages