I'll preface this by saying that I'm not an Android, nor a Java developer, nor am I a fan of either, particularly not after having written this app, and therefore, it's probably quite messy.
Now that we've got that out of the way, one application of the code we wrote to flash the RIOTBoard's onboard LED would be a Gmail notification light: you could modify this method to use any other mail provider as we're just using SMTP rather than the fully fledged library, by the by, as it's simpler for rapid development/hacks.
In order to achieve this, on top of your RIOT board, screen, mouse and keyboard, you'll need an internet connection: the RIOTBoard has an ethernet connector on board, or you could use a USB wireless receiver. I plugged one I commonly use with Raspberry Pi...and it didn't work.
In order to use wireless as another blog mentions, you need to update the Operating System. There's a long tutorial on this elsewhere so I won't go through it, but essentially the factory software is a bit behind the current RIOTBoard android release which contains the proper Wireless drivers. Click here to read the tutorial.
After you've followed this tutorial, pull down on the right of the top taskbar and click "wifi" as with a normal android device, and enter the relevant details.
Moving on: when looking at this app, I thought it would be simple. All you need to do is connect to your inbox, load in the message count and then flash for the number of messages. Took a browse for Google's APIs since as this is Android, that would be the most likely to have the best and easiest integratable support. Whilst this is probably simple with a device which has things like Google Play services, I eventually gave up on the Gmail official APIs and went back to basics: JavaMail, which uses SMTP to connect to the basic server. I will affix this with a side note that as this doesn't use OAuth2, it isn't as secure as other Gmail apps, but this is a simple hack to get you started so it shouldn't matter *too* much.
Next, we need to allow this kind of less secure access: to do this go to this page and enable less secure apps.
Following this we can write some actual code, so open up either Android Studio or Eclipse and either use our previous project, or create a new one.
We now need to download the JavaMail API from Oracle, unzip it and import the necessary libraries: these are all stored within lib of that project: to import the ones we need in Android studio, do the following:
Right click on your project in the left menu, click "Open Module Settings" and click the green add button in the top left corner.
In the next screen press "import .JAR or AAR Package" and next, then browse to the libs folder of your downloaded JavaMail API and import all the JARs in there.
Flick over to the "Dependencies" tab on your project and click the green plus sign.
Select "mail" and press apply, then OK.
From here we need to make a class which will handle any mail tasks: if you're not familiar with Java, a class just means we create a new type of "thing" in which we can define the thing's attributes and behaviours.
Our class is only going to have a few methods: Connect and CountMail. Here's our class to make this a quick process, and then I'll break this down and talk about JavaMail:
From the top, you need to change package to whatever package you defined at the beginning as your application's package. If you're not sure what this is, go to your initial activity, if you have one, and copy the very first line.
Next we import a few things and create the class, which holds the user and password of the developer for your email account, the host which you could potentially change to use hotmail or outlook SMTP servers, and what kind of protocol we're going to use.
We then create what's called a constructor : the part that begins public MailCounter(). Here we define a method which will get called when we instantiate this class, and take in 2 inputs from the developer - user and password.
Te next method literally connects you to the SMTP server, hence "Connect()", by feeding the details we defined earlier, and returns the session.
Following this we have another method, CountMail, which calls connect, gets the inbox and returns the number of unread messages. Simple enough.
Now, we could try calling this from the main UI activity...but this would cause an exception as android doesn't allow anyone to network on the main UI thread, in case it makes the UI lag waiting for a resource, so we now have to make an AsyncTask:
As this goes in the main activity, here is the entire activity code which is the separate class which handles UI. I picked a blank activity on creation: if you don't have a main activity, you can right click on your folder which has your package name (com.yourname.yourappspace.yourapp or something) and select "create Activity".
Simply put, AsyncTask is an abstract class: so we can't instantiate it, because it's not a type of thing, it's only an abstract type. This means we extend it, and then instantiate the extended class.
In order to create a class with the parent AsyncTask, we override 1 method at minimum - doInBackground - and a few others - here we're just overriding this and onPostExecute.
Once we have the class made, which just creates our MailCounter and checks it for the numbers, we instantiate inside onCreate and run it. onPostExecute handles the flashing of the light and changing of the GUI.
A protip about this particular app: I developed and tested this using my Nexus 5 because without a portable screen, I couldn't take my RIOTBoard out with me. I then added in the lines flashing the light.
Nonetheless, when you run this app using your RIOTBoard, you should see your rightmost user LED flash the number of unread emails you have in your inbox!
Of course, this functionality is very very basic, as in a phone situation, we would expect this to run in the background of the OS, but it should give you a basic idea of some real world applications you can create with the riotboard onboard LEDs!
Top Comments