USB Communication using Android Accessory Kit

android usb communication
Android USB Communication

Android

USB Communication using Android Accessory Kit

Prerequisite

Open your Android Studio – create a new project and follow the instructions below.

AndroidManifest.xml

Open AndroidManifest.xml file from yourapp/app/src/main/ and add android.hardware.usb.accessory feature

Add intent-filter in your <activity> tag having following code.

Create a folder xml  in yourapp/app/src/main/res/ add a new resource name  "accessory_filter"  in your xml and put the following code in it.

Now create two new JAVA classes inside yourapp/app/src/main/java/  called USBAccessoryManager.java & USBAccessoryManagerMessage.java and copy following code in it.

USBAccessoryManager.java

USBAccessoryManagerMessage.java

Now navigate to your content_main.xml or activity_main.xml  and add a Toggle Button

Open MainActivity.java  class and add a listener to that Toggle Button

MainActivity.java

Now call USBAccessoryManager  from MainActivity

Implement handler in the MainActivity.java

Add getFirmwareProtocol()  method

Now send byte code to Android Accessory by adding following code in your Toggle Button Listener.

Finally override onStart()  and onResume()

Connect your Android with Android Accessory Development kit and allow USB connection you will see a toast message saying “Accessory Connected”. Congrats 🙂

Now click on Toggle LED button on your MAIN Activity, Boooom LED 1 on your Accessory kit will lighten up.

Now try pressing PUSH button on your Accessory kit you will see a toast message on your app saying “Push Button Pressed”.

That’s it, Let me know if you guys have any question or difficulty in the comment section below.

Continue Reading
You may also like...

I am a Sr. Android App & Web Developer having 5 plus years of extensive hands-on experience in Object Oriented Programming, Database Design and Implementation using Agile & UML. Pro in Yii2 PHP Framework, Android App Development, MYSQL & a Blockchain enthusiast.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

More in Android

Recent Posts

Categories

Tags

To Top