Android and Bluetooth (5)

by luca
7 comments

If you tested the application I wrote for the previous tutorial, you could have noticed a small problem: while the connection is established and the “Hello World!” message is sent, the GUI of the application does not respond. The reason is simple: most of the methods used are blocking, i.e. they stop the execution of the process until they get a result (or a timeout).

If, for example, you call the read() method to get some data from a socket, that method stops the execution until the data is available!

To solve the problem, you must learn how to write a multitasking application, that is an application with different processes, each one independent from the other.

Threads and GUI

To simplify, an application may be composed by one or more processes (thread), executed in parallel by the Android O.S. Simple applications, like the ones from the previous examples, have a single thread, called main thread. This thread manages, among the other tasks, the components (text boxes, images, buttons…) that forms the graphical interface (GUI) of your application.

The first rule you must consider when writing multithread application is that only the main thread can update the graphical interface:

This rule often causes headaches to programmers: consider when a dedicated thread receives data from a Bluetooth socket: it’s common that, when you get a command, you should update the GUI accordingly…

Normally, a suggested solution is to ask to the main thread to update the GUI for you… today I’d like to show you a different approach, that leverages the use of the AsyncTask object.

AsyncTask

The AsyncTask object was included in Android to easily manage tasks that have to be executed in the background and have to interact with the application’s GUI.

The advantage is that – transparently for the developer – some of its methods are executed in the GUI (main) thread and others are executed in a different, dedicated thread.

Developers can therefore use the methods executed in the GUI thread to update the interface and the ones executed in the second thread for the background operations that should not block the main thread (for example send/receive data through a socket):

Let’s see the methods in detail:

[checklist]

  • onPreExecute() – GUI – executed just before the start of the background activity, can be used to inform the user (with an animation, message…) that the required operation has started
  • doInBackground() – background – main method where to perform the background task
  • publishProgress() – background – method, normally called in the doInBackground(), used to notify a “progress” during the task execution
  • onProgressUpdate() – GUI – method, invoked by the publishProgress(), that can update the GUI with the “progress” of the execution
  • onPostExecute() and onCancelled() – GUI – methods that are executed at the end of the task (or when it’s cancelled)

[/checklist]

In the next page you’ll learn how to use what explained before in a real Android application…

Related Posts

7 comments

jmv Monday May 5th, 2014 - 04:48 PM

very good luca, thanks!
waiting the next.

Reply
Matthew Sunday September 7th, 2014 - 07:38 AM

Hi Luca,
Android coding is not simple.

I have developed pfodApp that does all the Android stuff for you. Lets you display menus and screens from your Arduino by means of simple text strings.
Take a look at http://www.pfod.com.au for all the details.

Reply
norm Saturday March 7th, 2015 - 11:53 AM

Hi Luca

Great example, thank you !

Would you recommend using this method to send/receive bluetoothdata from different activities within an app ??

If so how would you modify your code to work ?

Reply
luca Sunday March 8th, 2015 - 01:32 PM

Hi! My suggestion is to have only ONE “manager” that performs all the communication tasks and dispatches the messages to the different activities

Reply
Mauro Monday June 20th, 2016 - 01:22 PM

GRANDE GIOVE !!!

MITICO LUCA !
Funziona e non ho più gli errori in Eclipse.
Questo era lo scoglio da superare per avere il punto di partenza
interattivo con la macchina che monta l’ HC-06.
Ora è una passeggiata nel parco !

Grazie.

Reply
Mauro Monday June 20th, 2016 - 01:54 PM

Riciao.

Non ho capito come mai l’ XML per l’UI è
nel Fragment_main anziché nel activity_main
e funziona ugualmente….

Cos’è che non so ?

Grazie

Mauro

Reply
Mauro Wednesday June 29th, 2016 - 10:17 AM

Ciao Luca !

Non riesco a cancellare il contenuto di tvResponse. Ho implementato un pulsante per pulire il contenuto dalle risposte della macchina via BT ma vorrei che a piacimento si potesse cancellare.

Ti ringrazio per la collaborazione.

Mauro

Reply

Leave a Comment

thirteen + thirteen =