Main part of my project is to create a specialized Android client, to get exactly the information I need in a compact and easy to understand way. The official OpenHAB client has two drawbacks that make it not suitable for my needs:

  • it runs only as full-screen application, but I would need a widget for the home screen
  • the update speed is rather slow - changes to the sensors are not reflected right away.

Thats why I set out to a journey in the OpenHAB REST API and the world of Android programming. I already wrote about the experiences with the REST API, so this time its about Android.

Android is different

Being a regular Java developer (doing this on my day job I'm no stranger to the programming language, and also not to multi-threaded programming or GUI stuff (the OpenHAB library with all its background handling was not that complicated as such).

But since Android as OD for mobile devices needs to care a lot more about  system resources and power management, some concepts differ form what I was used to.

Android Studio can create a basic application for what I needed - a homepage widget with a configuration screen. That included all the needed configuration files. There is also a GUI editor to create the layout for the GUI elements. But there it ended - the is for example no support for editing the configuration files and manifest files (e.g. for adding permissions).

Indirection everywhere

That time-consuming operations are not allowed to be done in the main GUI code (the widegts update() method) is a common pattern for all GUI applications. But its also discouraged to just create a background thread for doing the heavy lifting. Instead, one is supposed to write a background service class, that then gets started in the background. This service then sends around broadcast messages (called 'Intent'), to which the widget then can listen (or rather a special broadcast listener get created that then updates the widget). To make it worse, the explanation in the Android documentation explains that, but the example shown is wrong (the code itself would not compile at all) and leaves out some details.

So it took me several days to find out whats happening here. To make it worse, Android comes with its own version of the Apache HTTP client (that my library uses), which handles some internals slightly different.

Debugging an external device?

And since the app does not run directly on the PC, debugging is a little bit more difficult than what I'm used to. There is an Android emulator available, to which the IDE can connect its debugger. But when an exception is thrown by the code, I did not get a log message - I only saw whats happening when stepping through my code. And even then I did not get a proper stacktrace (but at least an error message: 'Item may not be null' - huh?).

In the end I did fall back to do whats usually the last resort in the debugging world: paving my code with log statements. That allowed me to get a proper stacktrace, and to trace the execution of my program. Also, to make changes and debugging easier, I copied the code of my library into my Android project, instead of just using it as library.

Together with Google I also found the missing pieces of my program configuration (e.g. where to configure the permissions to allow network access) and so I got my first result back from the OpenHAB server (running in the emulator):


(I'm using the rocker switches for my experiments because they are much easier to handle)

Whats next?

Next steps will be to clean up the code I wrote and make sure there is nothing in there thats not needed for the current functionality. Then I want to use my library again (so it needs some small modifications). When that has been done, I can go an create a proper widget that shows what I need it to show.