Android and Bluetooth (5)

luca 04/05/2014 7

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.


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:

  • 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)

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

Pages: 1 2


  1. jmv 05/05/2014 at 16:48 - Reply

    very good luca, thanks!
    waiting the next.

  2. Matthew 07/09/2014 at 07:38 - Reply

    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 for all the details.

  3. norm 07/03/2015 at 11:53 - Reply

    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 ?

    • luca 08/03/2015 at 13:32 - Reply

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

  4. Mauro 20/06/2016 at 13:22 - Reply


    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 !


  5. Mauro 20/06/2016 at 13:54 - Reply


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

    Cos’è che non so ?



  6. Mauro 29/06/2016 at 10:17 - Reply

    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.


Leave A Response »

This website uses cookies to ensure you get the best experience on our website maggiori informazioni

Questo sito utilizza i cookie per fonire la migliore esperienza di navigazione possibile. Continuando a utilizzare questo sito senza modificare le impostazioni dei cookie o clicchi su "Accetta" permetti al loro utilizzo.