Category Archives: Android/Java

Posts about Java and Android

Transferring data from Android using Audio

Just came back from monthly Bangalore Android meetup. Today I gave a talk about transferring data from in and out of Android using the audio jacket.

About the talk

In the talk I mainly discussed about the way by which you can transfer data in and out of Android using the audio.

I also discussed about the ways by which you can read this data in Arduino from Android.

Slides

I have uploaded the slides, which I have used in the talk to my slideshare account. I have also embedded them below so that you can easily view them.

Video

I have embedded the video fo the talk as well. My talk starts after 20 minutes.

Discussion about my marriage card

Badrinath also gave talk about NFC in the meetup and he used my wedding card for my demo.

It’s kind of great to know that he remembered my marriage card even after my marriage which happened around 2 months before ;)

Questions/Feedback

If you have a question or feedback/comments about my slide, then do let me know by leaving a comment below.

Buying Arduino in Bangalore

Lot of people asked me about buying arduino in Bangalore. Instead of answering them separately, I consolidated the different options in my blog. Let me know if I missed out any place.

Posted in Android/Java, Events/Camps | Tagged , , | 2 Comments

Building Robots using Arduino and Android at DroidCon

I just came back from yet another amazing event by HasGeek. This time it is DroidCon.

DroidCon

DroidCon is a developer conference and is part of world’s largest series of Android conferences. It happened at Bangalore on 18th and 19th of November.

Slides from my talk

I gave a talk about building robots using Android and Arduino. I explored the different ways by which you can make both Android and Arduino talk to each other, concentrating on the advantages and disadvantages of the different approaches.

When I find some free time, I will write about these approaches in more detail with code samples for each of them. So stay tuned :)

You can find the slides from my talk below.

Demo Video

Thanks to Arun for shooting this video

Demo source code

I also showed demo of my pet bot Asimi (more details about her later). You can download the source code and learn more about the bot from its homepage.

Update

Here is the video of my talk.

Posted in Android/Java, Arduino, Events/Camps | Tagged , , , , , | 10 Comments

Using standard Arduino board as Google’s ADK kit

When Google announced about the Open Accessory Development kit (ADK) in the last Google IO conference, I was really excited since it was based on Arduino boards.

But after the initial bliss went off, I was disappointed for two reasons. First you need an Android phone running Android version 2.3.4 or above. The second reason is that the original ADK kit is not compatible with the plain Arduino board. The downside of this is that all your existing Arduino Shields will not work with that board and not to mention you have to buy a rather expensive ADK kit. Now there is also an Arduino compatible ADK kit, but it is again expensive and it didn’t made sense for me to buy another Arduino board when I already had a bunch of them lying around.

Since it is based on Oleg’s USB Host Shield, I was trying to see if I can build a board with just plain Arduino board and Oleg’s USB Host Shield. It turns out it is possible and with the release of Oleg’s second version of the USB Host Library, it is much simpler.

Things you need

Any Arduino board.

You can use either UNO or Duemilanove. You can also use Arduino Mega, if you have one lying around. The advantage of using Mega is that you will have additional I/O pins.

USB Host Shield

You need a USB Host shield for Arduino. You can use the original shield by Oleg for $40, or you can buy a cheaper clone from sparkfun for $25. I recommend the original one by Oleg, since it doesn’t need an external power supply. If you are going to use Sparkfun’s version you need an external power supply. Also make sure you don’t buy the older version of Sparkfun shield. It has pins marked wrongly and you need to make changes to the library to make it work.

ADK compatible Android phone

You need an Android Phone which is ADK compatible, which means you need a phone which is running 2.3.4 or above. I tested this setup in Nexus S and it is working properly.

Sensors, LED’s, buttons

You would need additional sensors, LEDs or buttons to test the flow. To start with, you can just use some LED’s

Connections

Stack the USB Host shield on top of the Arduino board. Connect an LED on pin number 3. Connect your phone’s cable to the USB port of the shield.

Arduino Firmware

Download v2 of the USB Host Shield Library and place it in the library folder of your Arduino setup. Open the demokit_20 example sketch from the /examples folder and upload it to your Arduino board.

Android App

The Android demo app for ADK, can be found under the /app folder of the ADK package. Upload the app to your Android phone.

Testing it

Power on Arduino and connect your Android phone to the other side of the cable. You should see a dialog box, which asks you whether you want to launch the app. Click yes. Once the app is launched you can control the LED connected on pin 3, by moving the slider in the Android app.

Now enjoy your new cheaper ADK kit ;)

Posted in Android/Java, Arduino | Tagged , , , | 11 Comments

Conditional code compilation in Java

In C/C++ there is a construct which allows you to conditionally compile code. Yes I am talking about the #ifdef... #endif Java doesn’t have an equivalent. But you can use the following clever trick to achieve the same functionality.

Why conditionally compile code?

So before we proceed, you might ask me, why we need to do conditionally compiling? There are couple of reasons, but the main reason which motivated me to look for the solution is to conditionally enable debug statements. You can say that we can probably use a boolean variable or a function call, to determine whether we need to output the debug statement or not. But if the number of debug statements is high then they could add up.

Setting up final boolean variable

So the trick is simple. You have to create a if statement with a boolean private variable and if you  make the boolean private variable as final and set the value to false, then at compile time, the compiler will be able to determine that these code branches are unreachable and will not include them as part of the compiled code.

Sample Code

The following sample code explains this trick.

Posted in Android/Java | Tagged , | 5 Comments

Finding the song/track which is currently playing in Android

For my iAndroidRemote project, I had to find the currently playing song/track in Android phone and the ability to change to next/previous song/track.

After some digging, I found that there is no documented way to do it. It depends on an undocumented way which may not work well in all Android phones.

I found an undocumented way which works for HTC phones and some other stock Android phones that are using the default music player.

I thought of sharing the code here, so that it would be useful for others and also I would know where to look for it when I need it for the next time.

Copying the aidl files

Any Android app, can play music by using the com.android.music.MediaPlaybackService class. In order to find the currently playing song, we need to create a Service which will interact with this class

In order to do that, we need to copy the IMediaPlaybackService.aidl file from the source code of the default music app. For HTC phones we need to copy the IMediaPlaybackService.aidl file inside the com.htc.music package and for other Android phones we have to copy it from the com.android.music package.

Create the following packages to your android project.

  • com.htc.music
  • com.android.music

Copy the com.htc.music.IMediaPlaybackService.aidl and com.android.music.IMediaPlaybackService.aidl files to the newly created packages.

Creating the ServiceConnection class

The next step is to create the ServiceConnection class which will allow us to interact with the MediaPlaybackService class.

Copy the following code and create the MediaPlayerServiceConnection inside your activity as an inner class.

We are using the boolean isHtc to determine if we are on an HTC phone. We would be using this flag before invoking the methods on the ServiceConnection class.

Getting the current song

Now we can call the getTrackName() method on the corresponding object and we can get the track information.

Playing next song

To play the next song in the track we have to just call the next() method on the corresponding object

Playing Previous song

To play the previous song in the album we have to just call the prev() method on the correct object

I have created a small sample project to show the entire flow in action. You can download the project from my Github page.

Posted in Android/Java | Tagged , , | 7 Comments

Adjusting the volume in Android through code

For my iAndroidRemote project, I had to adjust the volume of my Android phone. I found the code after digging around a bit.

I thought of sharing the code here, so that it would be useful for others and also I would know where to look for it when I need it for the next time.

Changing the volume in Android is pretty easy. You just need to know which class and method to call.

Getting the AudioManager Instance

First you have to get an instance of the AudioManager. You can get it by calling the getSystemService method of the Context object. If you are inside an Activity, you can get the instance by making the following code

Once you have the AudioManager instance, all you have to do is to call the adjustVolume method with appropriate arguments.

Increase Volume

To increase the volume you have to pass the AudioManager.ADJUST_RAISE constant.

Reduce Volume

And to reduce the volume you have to pass the AudioManager.ADJUST_LOWER constant.

I have created a small sample project to show the entire flow in action. You can download the project from my Github page.

Posted in Android/Java | Tagged , | 2 Comments

iAndroidRemote – Control your Android phone using Apple Remote

Long time readers of my blog, would know that I hack around Arduino. Couple of days ago, I was playing around with Arduino while listening to music from my Android phone. I wanted to increase the volume of the song that was playing.

At that moment an idea struck me and I thought it would be cool if I can control the volume using some kind of remote. I opened my bag and found an old Apple remote.

Couple of hours later, I was continuing hacking around Arduino, listening to music from my Android phone, but now if I had to change the volume or change the track, I don’t have to reach for my phone, I can do it using my Apple remote itself ;)

iAndroidRemote

If you want to know more about how I did it, or want to try it out yourself, then head over to the project page, where I describe the entire process including the schematics and the source code for both Arduino and Android that I am using.

Try it out and let me know how it works for you. Happy hacking ;)

Posted in Android/Java, Arduino | Tagged , , | 3 Comments

Sharing content in Android using ACTION_SEND Intent

Very often, you might want to enable the ability for users to share some content (either text, link or an image) from your Android app. Users can share the content using email, twitter, Facebook, sms or through numerous other ways.

The users might already have installed some custom apps for each one of the above service. So instead of coding all these again, it would be really nice (for both your users as well as for you as a developer) if you can invoke any one of these apps, where users want to share content from your app.

Sharing text

Android provides a built-in Intent called ACTION_SEND for this purpose. Using it in your app is very easy. All you have to do is to use the following couple of lines.

In my phone, it invokes the following dialog box listing the apps that have registered to get notification for this intent.

sharing-content-android

Sharing binary objects (Images, videos etc.)

In addition to supporting text, this intent also supports sharing images or any binary content. All you have to do is to set the appropriate mime type and then pass the binary data by calling the putExtra method.

Registering for the Intent

If you want your app to be listed when this Intent is called, then you have to add an intent filter in your manifest.xml file

android:mimeType specifies the mime type which you are interested in listening.

Happy sharing ;)

Posted in Android/Java | Tagged , , , | 33 Comments

FeedStats, my first Android app

Just a quick note to you let you guys, that I have just pushed my android app to the market. :)

It’s called FeedStats and it allows you to get the stats of a feedburner feed url and shows the data in a graphical format.

I created this app to demonstrate how we can draw graphs in android using JavaScript in a HTML page and then embedding it inside a webview.

If you have an android phone, then you can download it from the Android market, by searching using the term “FeedStats”. Try it out and let me know if you have any feedbacks/comments.

The entire source code of the app is available at my github account. Download and play around with it.

Posted in Android/Java | Tagged , , | 9 Comments

Making Arduino talk with Android using Amarino

Last week, I gave a talk + demo about using Amarino to make both Arduino and Android talk to each other in Bangalore Open source Hardware meetup

I have uploaded the slides which I used for the talk to my slideshare account and you can download it from there.

The slide also includes the schematic diagram for the circuit which I used for the demo. The source code that I used for the demo can be downloaded from the below links

Posted in Android/Java, Arduino, Events/Camps | Tagged , , , | 2 Comments