Month: September 2015

Introducing ReactiveBeacons

2015-09-30 Android, Context awareness, Java, Open source No comments

Recently, I’ve created yet another reactive library for Android. It’s called ReactiveBeacons and it’s responsible for observing BLE (Bluetooth Low Energy) beacons. Beacons are small devices, which became quite popular in the last years. They can be utilized in creating Contextual Awareness, Contextual Computing and Internet of Things. Beacons have lithium battery, are constantly turned on and emit signals via Bluetooth all the time. ReactiveBeacons library allows to transform these signals into Observable stream compatible with RxJava. Whenever new beacon is detected or RSSI (Received signal strength indication) changes, new immutable beacon data is emitted.

Usage of the library inside the Activity is simple:

We also have to remember to unregister subscription correctly in order to stop BLE scan, which can drain the battery.

If you want to use this library in your project, add the following dependency to your build.gradle file.

dependencies {
  compile 'com.github.pwittchen:reactivebeacons:0.0.1'
}

Don’t forget to add dependency to RxAndroid if you want to use Android-specific features of RxJava.

Source code of the library can be found at: https://github.com/pwittchen/ReactiveBeacons.

Any new issues or pull requests are welcome!
Happy coding!

Using tmux

2015-09-20 Linux No comments

What is a terminal multiplexer? It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal. And do a lot more.

– tmux.github.io

ss-tmux1

Tmux feature, which I find very useful is tiling terminal window. We can have several tiles with different terminals within a single terminal window.

How to use tiling?

First, we need to install tmux:

$ sudo apt-get install tmux

Then, we need to start it:

$ tmux

When we are inside tmux, we can execute its commands. It’s good to check full list of tmux key bindings.

Default initial key binding for different commands is Ctrl+B. When we hold Ctrl and then press B, we can press next key for specific command. It’s tricky and it isn’t intuitive at the first time.

For example, if we want to split terminal window vertically, we need to do the following thing:

Start tmux, Hold Ctrl, press B (while holding Ctrl), release buttons and press % key (equivalent to Shift+5).

If we want to split terminal window horizontally, we need to do the following thing:

Start tmux, Hold Ctrl, press B (while holding Ctrl), release buttons and press " key (equivalent to Shift+' – code for ' sign is 47 for xdotool).

We can create any tile configuration we want like in i3 windows manager.

tmux-tiles

If we want to switch between tiles, we need to use the following key combination:

Hold Ctrl, press B (while holding Ctrl), release buttons and press O key (“O” letter – not zero).

Creating 4 tiles automatically

Popular terminal windows configuration is 4 tiles (2 columns and 2 rows). We can split windows horizontally or vertically pretty fast with default shortcuts, but creating layout consisting of 4 tiles requires some clicking. I’ve written a simple script, which generates such layout for us automatically and saves the time.

First, we need to install xdotool:

$ sudo apt-get install xdotool

Next, we can create file named tmux-4tiles, set its chmod to 777 and save it in /usr/local/bin/ directory.

File should have the following content:

#!/bin/bash
# generates 4 tiles in tmux (requires tmux and xdotool)
xdotool key ctrl+b shift+5 && xdotool key ctrl+b shift+48 && xdotool key ctrl+b o && xdotool key ctrl+b shift+48 ctrl+b o ctrl+b o ctrl+b o && clear

It’s also available in my dotfiles on Github.

When, we are done, we can enter tmux:

$ tmux

and run the script:

$ tmux-4tiles

After that, we’ll get the following layout:

tmux-4tiles

We can automate generating different layouts for our purposes in the same way.

Recap

In my opinion, tmux is very useful tool for people working with terminal who want to have organized windows in an elegant way.

References

Introducing ReactiveSensors

2015-09-05 Android, Java, Open source, RxJava No comments

Another month, another library. Recently, I’ve released yet another reactive library called ReactiveSensors. It’s an open-source Android library monitoring hardware sensors with RxJava Observables. Library is compatible with RxJava 1.0.+ and RxAndroid 1.0.+ and uses them under the hood.

Library is available at: https://github.com/pwittchen/ReactiveSensors.

In my opinion, hardware sensors are perfect case for applying RxJava, because in fact we’re constantly receiving a stream of events emitted by many sensors. With Reactive Programming approach we have plenty of possibilities and easy API for manipulating received sensor’s data.

Usage of the library is really simple. You just need to subscribe an Observable with RxJava in the same way like in any other reactive library.

Code sample below demonstrates how to observe Gyroscope sensor:

Please note that we are filtering events occuring when sensors reading change with ReactiveSensorEvent.filterSensorChanged() method. There’s also event describing change of sensor’s accuracy, which can be filtered with ReactiveSensorEvent.filterAccuracyChanged() method. When we don’t apply any filter, we will be notified both about sensor readings and accuracy changes.

We can observe any hardware sensor in the same way. You can check list of all sensors in official Android documentation.

I’ve created section about Good Practices regarding working with hardware sensors on Android in README.md file in the GitHub repository.You should also read an article about Best Practices for Accessing and Using Sensors in official Android documentation.

Read more in the README.md file located in the repository of the library at: https://github.com/pwittchen/ReactiveSensors.

You can also find JavaDoc at: http://pwittchen.github.io/ReactiveSensors/.

If you want to use ReactiveSensors in your project, add the following dependency to your build.gradle file:

dependencies {
  compile 'com.github.pwittchen:reactivesensors:0.0.1'
}

Feel free to send me feedback, report an issue or fork the library!

Learning Python

2015-09-01 Python No comments

Some time ago, I’ve decided to learn Python programming language. I thought it may be useful during performing some basic tasks in the system and Linux or Windows scripting. Moreover, it’s good language for developing web applications with frameworks like Django, Flask or Bottle. I’ve decided to go through all of the tasks in the Learn Python – The Hard Way course. Basic tasks weren’t surprising. They were similar to other object oriented languages. In the further tasks we can learn basic features available in Python language, which are not so common for e.g. Java programmers. I’ve placed all tasks in learn-python-the-hard-way repository on GitHub.

Moreover, in README.md file I’ve added information about:

  • Installing Python on Windows and Linux
  • Executing Python scripts from terminal
  • Pip package manager and installing it on Windows and Linux
  • Using Pip
  • Unit Testing
  • Virtualenv
  • Scripts on Linux
  • Style Guide for Python Code
  • Development Environments
  • Popular Python web frameworks
  • Useful Python libraries
  • Additional Resources & links

In addition, to learn this language better, I’ve decided to create a few simple projects:

  • android-resource-converter – scripts converting Android xml resources with translations to csv file and backwards
  • git-branch-comparator – checks if development branch has all changes from master branch in Git repository
  • craplog – verifies whether your git log is a crap or not

What’s interesting first two of them were used in production and second project is running regularly on a CI server.
Other developers also create similar tools and scripts. For example Pidcat is pretty handy and useful tool for Android developers and it’s just a single script written in Python.

To sum up, I can tell that Python is great object oriented language with a clean and simple syntax. With this language we can achieve our goals really fast with a few lines of code and readable solution. I recommend to learn this language to broaden our horizon even if we are programming in totally different technologies on daily basis.