文章總列表

Holiday Bluetooth/Android Developer: Read Heart Rate In Android

I'm always curious how the smart phone interacts with BLE device. I would like to write own APP to interact with my BLE heart rate sensor. To do this, I have to overcome 3 difficulties:
  • I don't know Java
  • I don't know Android GUI programming
  • I don't know Android Bluetooth programming
I spent several weeks to overcome these difficulties. Topic is too large to describe everything clearly. I just can describe some landmark when I was studying.


Android Development Environment
Almost all Android development book has a chapter illustrating how to setup environment. I installed Eclipse ADT Bundle for both WIN/MAC. With the aid of Eclipse, I had my 1st Android application running on the phone easily. 

The books also illustrated how to use Android simulator. But Bluetooth is not possible to simulate over that. I always run the program directly on the phne.


JAVA
I'm not 100% Java newbie. I learned Java about 10 years ago and gave up very soon. The only thing I learned is something like "Hello World" example:

    // hello.java
    public class hello {
        public static void main(String[] args) {
            System.out.println("Hello World");
        }
    }

Command to compile:
    javac hello.java

Command to run:
    java hello

After the 1st Java program running, the playground becomes quite useful. I tested If-Else, For-Loop, Function-call, FILE-IO, String-processing, ... here, and grab some "feeling" of Java quickly. Thanks to huge user base around the world, I can almost find any solution with keyword "java open file".


Android GUI Programming
Android GUI framework defines a flow behind the scene, which invokes callback functions when event happened. Users' job is to understand the framework, and provide callback functions to respond to the event. The GUI itself is complex, not surprisingly, the coding is also complex.

For example, when users click the button, Android framework invokes callback function onClickListener(). Users can add the desired operation in callback function. Java "enhanced" complexity that there are at least 4 ways to write such callback functions.

Eclipse provides quite advanced development environment. It compiles the code in background, and give users hints when compile error/warning on the fly. The function name completion is also important. I wonder how could people remember long API name such as "Textview.OnEditorActionListener".


Android Bluetooth Low Energy Programming
My reference book Getting Started with Bluetooth Low Energy, O'Reilly, have a useful chapter illustrating how to use Bluetooth Application Accelerator to write Android application. The library wraps the underlying Android Bluetooth class into simpler interface. Due to the asynchronous nature of BLE protocol, the library provides a lot of callback functions to indicate event arriving. Here is the callback functions I used in this project:
  1. uiDeviceFound()
  2. uiDeviceConnected()
  3. uiDeviceDisconnected()
  4. uiAvailableServices()
  5. uiNewValueForCharacteristic()

Show Time
If the bluetooth is not enabled when Application is started, then it will ask users to enable BT.

Then users press "SCAN" button in the top-middle of the screen, then heart rate count appears in the bottom-left corner. In this picture, it's "75bpm".
Android development environment provided friendly GUI to show log. In the screenshot, when receiving notification from BLE device, onNotify() is invoked. Finally, the users' callback function is invoked and show the heart rate count in the screen.


留言

這個網誌中的熱門文章

幼犬書桌椅選擇心得 升降桌 兒童桌椅

Herman Miller Aeron 一代氣壓棒維修

STM32 UART + DMA,使用HAL實作TX/RX,以及不定長度接收