Overcoming Bluetooth Issues

Overcoming Bluetooth Input Stream Processing Challenges in Android Applications

During our HangBoulder hackathon, we built an app, that required us to connect a custom-built sensor to an Android phone. The app prototype was simple overall, but we ran into some issues where the UI would blank out when while streaming data from the sensor.

We are Ghost Founder, a full-stack engineering and innovation company, and in this post, we will quickly explain how we used ChatGPT to solve the problem of the UI blanking out while streaming data from Bluetooth.

Ghost Founder is a multidisciplinary team of experts in software engineering, mechanical engineering, physics, marketing, sales, design, and mechanics. They specialize in inventing new technology daily and leverage AI to enhance their output, driving innovation in various fields such as technology invention, AI innovation, and more. In this context, understanding and overcoming Bluetooth input stream processing challenges becomes crucial for the efficient development of Android applications.

The Problem:

Our Android application required monitoring a Bluetooth input stream from an ESP-32s board for specific sequences of 1s and 0s. When a sequence of 1s was detected, the application should call the startTimer() function, and when a sequence of 0s was detected, the application should call the stopTimer() function. The initial implementation caused the UI to become unresponsive due to the while loop running on the main thread, which is responsible for handling UI updates. When the main thread was blocked by the while loop, the UI became unresponsive and appeared blank.

The Solution:

To resolve this issue, we moved the while loop to a separate thread, allowing the main thread to continue updating the UI without being blocked. This ensures a smooth user experience while processing the input stream data as required. We achieved this by creating a new Thread and moving the while loop inside its run() method. The UI updates were still performed on the main thread using the runOnUiThread() method. This approach allowed the application to efficiently process the Bluetooth input stream and update the UI as needed.

javaCopy code

Thread inputThread = new Thread(new Runnable() {

@Override

public void run() {

boolean prevStateIsOne = false;

boolean prevStateIsZero = false;


while (true) {

try {

InputStream inputStream = btsocket.getInputStream();

inputStream.skip(inputStream.available());

byte b = (byte) inputStream.read();

System.out.println("Test 1 completed ? " + b);


if (b == '1' && !prevStateIsOne) { // When input changes from 0s to 1s

prevStateIsOne = true;

prevStateIsZero = false;

runOnUiThread(new Runnable() {

@Override

public void run() {

startTimer();

}

});

} else if (b == '0' && !prevStateIsZero) { // When input changes from 1s to 0s

prevStateIsOne = false;

prevStateIsZero = true;

runOnUiThread(new Runnable() {

@Override

public void run() {

stopTimer();

}

});

}


} catch (IOException e) {

e.printStackTrace();

}

}

}

});


inputThread.start();

Conclusion:

Handling Bluetooth input stream processing in Android applications can be challenging, particularly when dealing with real-time data and UI updates. However, by understanding the importance of separating the input stream processing from the main UI thread, you can create responsive applications that process data efficiently. This approach not only helps in enhancing user experience but also ensures the seamless integration of Bluetooth communication in mobile applications.