In this blog post, I will walk you through the process of setting up Android Studio to work with the Arduino Mega ADK. So lets jump into it.
Step 1: Setup Android Studio and run a hello world app.
Here is how you do that.Step 2: Modify the Manifest file
Once you were able to run a simple hello world app, make the following changes to it.
In the Android Studio on the "Android" pane on the left, navigate to app -> manifests -> AndroidManifest.xml and double click to open. Now on the top of
Change the class name to your main activity. For example, if you have named your main activity as "SomeActivity" change "public class MainActivity extends Activity {" to "public class SomeActivity extends Activity {"
From the code from most of the internet sources, here is something that has to be changed to work with most of the android devices.
By default this file has a..... , remove it and replace with the code below to make it look something like
That is it. Now you should have no errors in the android application. Compile the code and install the application to your phone.
Continue with Arduino Mega ADK and Android Application communication through the end
References:
Thanks to USB_Shield source code by Kristian Lauszus
Accessory Development Kit 2012 Guide
<uses-library android:name="com.android.future.usb.accessory"></uses-library>
Now inside the add the following:
<intent-filter> <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" /> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" android:resource="@xml/accessory_filter" />
Now the overall AndroidManifest should look something similar to:
Step 3: Create an accessory filter
Each time an android application connects with a USB accessory, the android application tries to identify the accessory with combination of values. These values are stored in the android "accessory_filter.xml" on the android side.
From the left hand pane navigate to app -> res. Right click on the "res" folder -> New and click on "Android resource directory"
On the following screen type the "Directory name" as xml and the "Resource type" as xml and click ok.
Don't worry if you don't see the xml folder. Now right click on "res" folder -> New and click on "Android Resource File".
On the following screen type the "File name" as "accessory_filter" without quotes and select the "Resource type" as xml and click ok
Type the following in the xml file and save.
Note: The values in the "accessory_filter.xml" file should match the Arduino ADK initialization values. For example, for my xml code above, my Arduino sketch ADK initialization code would look something like below. Observe the yellow highlighted lines:
Follow this post to configure Android Studio to compile using Google API's
Navigate to the app's MainActivity.java (The name may vary if you have given a different one). In my case it is located at app -> java -> adk.srichakram.in.adk_googleapi -> MainActivity.
In the main Activity replace everything after "package ........." with the below code
From the left hand pane navigate to app -> res. Right click on the "res" folder -> New and click on "Android resource directory"
On the following screen type the "Directory name" as xml and the "Resource type" as xml and click ok.
Don't worry if you don't see the xml folder. Now right click on "res" folder -> New and click on "Android Resource File".
On the following screen type the "File name" as "accessory_filter" without quotes and select the "Resource type" as xml and click ok
Type the following in the xml file and save.
xml version="1.0" encoding="utf-8"?><resources>
<usb-accessory manufacturer="Srichakram" model="ArduinoADK" version="1.0" />
</resources>
Note: The values in the "accessory_filter.xml" file should match the Arduino ADK initialization values. For example, for my xml code above, my Arduino sketch ADK initialization code would look something like below. Observe the yellow highlighted lines:
ADK adk(&Usb, "Srichakram", // Manufacturer Name
"ArduinoADK", // Model Name
"Simple Led blink sketch for the USB Host Shield", // Description (user-visible string) "1.0", // Version
"http://www.tkjelectronics.dk/uploads/ArduinoBlinkLED.apk", // URL (web page to visit if no installed apps support the accessory)
"123456789"); // Serial Number (optional)
Step 4: Configure Android Studio to work with Google API
Step 5: The coding part
As my intention of writing this blog post is to help people struggling to make the Arduino Mega ADK to work, I am not going to explain the coding part in deep. I used the code Developed by Kristian Lauszus and made some minor changes to work with all the android versions. I will explain the part where the changes are to be made inorder to eliminate most of the common errors.Navigate to the app's MainActivity.java (The name may vary if you have given a different one). In my case it is located at app -> java -> adk.srichakram.in.adk_googleapi -> MainActivity.
In the main Activity replace everything after "package ........." with the below code
Change the class name to your main activity. For example, if you have named your main activity as "SomeActivity" change "public class MainActivity extends Activity {" to "public class SomeActivity extends Activity {"
From the code from most of the internet sources, here is something that has to be changed to work with most of the android devices.
Old Code | Changes according to new ADK |
---|---|
import com.android.future.usb.UsbAccessory; import com.android.future.usb.UsbManager; | import android.hardware.usb.UsbAccessory; import android.hardware.usb.UsbManager; |
UsbAccessory accessory = UsbManager.getAccessory(intent); | UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); |
mUsbManager = UsbManager.getInstance(this); | mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); |
Step 6: Modify the Main Activity layout file
Navigate to your MainActivity layout file and double click to open it. In my case it is located at app -> res -> layout -> activity_main.xmlBy default this file has a
Step 7: Add the Strings
One last step is to add the strings. Navigate to app -> res -> values -> strings.xml and replace everything in it with
xml version="1.0" encoding="utf-8"?><resources>
<string name="ToggleButton">ToggleButton</string>
<string name="app_name">ArduinoBlinkLED</string>
</resources>
That is it. Now you should have no errors in the android application. Compile the code and install the application to your phone.
Next Steps:
Setting up Arduino to work with Mega ADK & Simple LED Blink codeContinue with Arduino Mega ADK and Android Application communication through the end
References:
Post a Comment