Android application
The Android app (you can find its source code on Github) I developed for this tutorial sends/receives data via Bluetooth:
- lets the user connect / disconnect from a device with a button in the toolbar
- when the user click on one of the two buttons, sends the commands “BUTTON1” and “BUTTON2”
- displays what received in a TextView
The communication via Bluetooth is performed using an AsyncTask, BTAsyncTask; let’s see how it works…
Connection
When the user chooses the paired device, the application opens a socket with that device and creates a new instance of the BTAsyncTask object, passing to it the socket that was obtained from the BluetoothAdapter.
Then the application calls the execute() method that starts the doInBackground() method of the BTAsyncTask, where a loop waits for new data coming from the paired device:
Data IN
When BTAsyncTask receives a new data, it calls the publishProgress() passing the data. As you learned, the publishProgress() methods internally calls the onProgressUpdate() one that, running in the GUI thread, can update the GUI with the data received:
Data OUT
When the user clicks a button, the main thread calls the sendCommand() method of BTAsyncTask to send the corresponding command:
Demo
I developed a simple .Net application to respond to the Android app; here’s a short video showing how the app works:
very good luca, thanks!
waiting the next.
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.
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 ?
Hi! My suggestion is to have only ONE “manager” that performs all the communication tasks and dispatches the messages to the different activities
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.
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
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