I am currently trying to rotate/change color of a button when it is clicked. This button retrieves data from sensors on my Arduino and displays it on the phone, it takes up to 10 seconds so I would like to shows that something is running when the button is clicked.
The problem I am facing is that Bluetooth actions seem to take the advantage on displays on the phone, whatever method I try, so the animation/color switch happens when data are retrieved, which is not what I am looking for.
Here is my code:
brefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TRIED WITH START ANIMATION BUT IT STARTS WHEN BLUETOOTH PART IS FINISHED
/* Animation rotate_brefresh = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_clockwise);
brefresh.startAnimation(rotate_brefresh);
*/
// SAME FOR JUST SETTING BACKGROUND IMAGE OF THE BUTTON IN ANOTHER COLOR
/*brefresh.setBackgroundResource(R.drawable.brefresh_red);
System.out.println("aa"); */
// FINALY SAME WHEN DISPLAYING A PROGRESS DIALOG, IT SHOWS UP WHEN BLUETOOTH STUFF IS FINISHED
progress = new ProgressDialog(MainActivity.this);
progress.setTitle("Récolte des informations");
progress.setMessage("Cela prend quelques secondes");
progress.setCancelable(false);
progress.show();
// BEGINS OF BLUETOOTH STUFF
BluetoothSocket btSocket = null;
try {
btSocket = hc05.createRfcommSocketToServiceRecord(mUUID);
btSocket.connect();
} catch (IOException e) {
e.printStackTrace();
}
InputStream inputStream = null;
try {
inputStream = btSocket.getInputStream();
inputStream.skip(inputStream.available());
String s1 = ""; //premier textview
String s2 = ""; //deuxième textview
byte b;
do {
b = (byte) inputStream.read();
s1 = s1 + (char) b;
}
while (b != 94); // 94 c'est ^
do {
b = (byte) inputStream.read();
s2 = s2 + (char) b;
}
while (b != 59); // 59 c'est ;
s1 = s1.replace("^", "");
s2 = s2.replace(";", "");
tvpH.setText("- pH de : " + s1);
tvHumidity.setText("- Humidité relative de : " + s2);
} catch (IOException e) {
e.printStackTrace();
}
try {
btSocket.close();
System.out.println(btSocket.isConnected());
} catch (IOException e) {
e.printStackTrace();
}
/*brefresh.setBackgroundResource(R.drawable.brefresh);*/
}
});
Since I tried several methods, I guess the problem comes from the Bluetooth part, but I have no clue about how to solve it.
Any Ideas?
Thanks
You are trying to perform an IO operation on the Main-UI thread. Doing any kind of blocking operations on Main thread will result in freezing your app.
You need to move the code for communication with Bluetooth to a background thread.
Here are some references that might help you: https://developer.android.com/guide/topics/connectivity/bluetooth/transfer-data https://medium.com/swlh/create-custom-android-app-to-control-arduino-board-using-bluetooth-ff878e998aa8
P.S. Kotlin has more easier ways to do background operations than Java. So, if you really want to go with the latest technology, I'd suggest to explore more on Kotlin.