Han Qin . Me

Always being better

Ardroid Environment Sensor - My First Android - Arduino Project

Recently, I have spent most of my spare time on arduino, an open-source electronics prototyping platform.

Arduino is very easy to use that one with little hardware knowledge like me could start making something quickly, and with various shields and sensors, it is also very powerful.

Ok, let’s go to the main topic today, my first android arduino project.

The reason for the small project is very tricky, that is, I usually doubt the weather and envrionmental info from official departments, especially for the PM 2.5, As a result I decided to build an environment sensor and take it with me every day. The data around me is the most accurate one, right?

images Above is how ardorid looks like, at the time of writing, I havn’t found a satisfying PM2.5 sensor, so you can only see two lines showing.

The basic idea is:

  1. android device (my Huawei Y300) will act as a collector, analyser and display of the envriomental data.

  2. arduino(openjumper mega2560, openjumper sensor shield v3, bluetooth adapter, dht11 sensor) will retrieve data and feed to the collector.

Some details of the tech stack:

  1. For the beautiful dynamic chart, the framework is AChartEngine.

  2. For communication between android and arduino, Firmata is your friend.

  3. Also, bluetooth is a nice choice besides google ADK board. OpenJumper sensor shield v3 makes the integration of bluetooth module, DHT11 sensor supper easy.

  4. Others like the battery-powersource, gradle will lead you a good life:)

Please refer to Github for the code.

Android Toast Usages and Tests

Usage tips

I have been perplexed for a long time by the duration parameter of android Toast function, before I noticed the warning message from android studio

images

The right way is

1
Toast.makeText(activity, "Email was sent successfully.", Toast.LENGTH_LONG).show();

or

1
Toast.makeText(activity, "Email was sent successfully.", Toast.LENGTH_SHORT).show();

Test

I will demonstrate how to test toast function with Robolectric

1, For default toast message

1
2
Toast toast = ShadowToast.getLatestToast();
String text = Robolectric.shadowOf(toast).text;

Also, you can use

1
String text = ShadowToast.getTextOfLatestToast();

To check shown toast count

1
int count = ShadowToast.shownToastCount();

To verify a toast message has been displayed

1
2
CharSequence message = "custom message"
boolean showed = ShadowToast.showedToast(message);

To get all shown toasts

1
List<Toast> toasts = Robolectric.getShadowApplication().getShownToasts();

To reset all shown toasts list

1
2
ShadowToast.reset(); // or..
Robolectric.getShadowApplication().getShownToasts().clear();

2, For custom toasts

1
2
3
CharSequence message = "custom message";
int layoutResourceIdToCheckForMessage = R.id.custom_toast_text_view_id;
boolean showed = ShadowToast.showedCustomToast(message, layoutResourceIdToCheckForMessage);

For busy developers who don’t have time to look at source codes of robolectric. :)

Implement Email Function in Android

There are several ways to implement email function in android, with their own pros and cons. I will try to summarize everything here, and you can find a demo application at the end.

1) Use installed mail apps

  • Create an intent with ACTION_SEND and mailto scheme:

  • MimeType can be set to message/rfc822 instead of text/plain, to limit applications displayed in the chooser dialog.

  • message/rfc822 stands for

    Email; EML files, MIME files, MHT files, MHTML files; Defined in RFC 2045 and RFC 2046

  • There is also an enhanced version, from which you can determine what will be displayed in the chooser dialog.

1
2
3
4
5
6
Intent intent = new Intent(Intent.ACTION_SEND, Uri.fromParts("mailto", TO_ME, null));
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{TO_ME});
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, text);
activity.startActivity(Intent.createChooser(intent, "Send mail..."));

2) Use the java mail api

(ported to android by Jon Simon).

Firstly, you will need to download three jar files from https://code.google.com/p/javamail-android/ .

Then, simply follow the code provided in the demo application, or you can refer to http://www.tutorialspoint.com/java/java_sending_email.htm for a comprehensive guide.

3) Use specifically built backend Api

Most applications have a backend service, it is easy to add an api that collects info from handsets and send mail on the server side.

Demo application: (last update on Aug, 4, 2013)

https://github.com/hanqin/AndroidEmailDemo

References:

http://stackoverflow.com/questions/2197741/how-to-send-email-from-my-android-application http://stackoverflow.com/questions/6827407/how-to-customize-share-intent-in-android http://en.wikipedia.org/wiki/Internet_media_type http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android http://developer.android.com/guide/components/intents-filters.html

Add an Android Library Project in Intellij or Android Studio

It can be tricky sometimes when adding an android library project in intellij or android studio. The reasons may vary from time to time but two I recently encountered are:

  1. Automatic update of project.properties is disabled in intellij, thus the android library project cannot be added.
  2. Creating a new library module in android studio, for an ant based project, Studio only creates a settings.gradle, but forget to change the project.properties.

So far, the root cause is clear, the inconsistency between ide settings and android configs.

  1. You can manually update project.properties to a correct value
1
2
target=android-16
android.library.reference.1=../../actionbarsherlock

and

1
2
android.library=true
target=android-14
  1. Remember to update settings in your IDE

images

That’s it.

Octopress on Heroku - Day 2

Tasks

  1. Added several plugins, including comments from disqus, my reading from douban, QR code and readability
    • All these plugins and even more can be found here

Problems

  1. The explanation for Douban plugin is misleading, fixed and sent a pull request

Next

  1. Use sub modules to manage third party plugins?
  2. A better place for facebook like and twitter buttons?
  3. Themes update
  4. Blogs!

Octopress on Heroku - Day 1

Tasks

  1. Setup octopress according to the documentation
  2. Update blog configuration, ruby version as well(required by heroku)
  3. Created a test blog
  4. Update google analytics
  5. Markdown cheat sheet

Problems

  1. Google analytics not working, solution as mentioned above
  2. Slow connection to heroku
1
git push heroku master

but only got Operation/Connecting timed out error. To fix the issue, manually add ~/.ssh/config, and put following lines in it:

1
2
3
4
5
6
Host heroku.com
User freemember007
Hostname 107.21.95.3
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
port 22

Thanks to this article

Next

  1. Comment