Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8313289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:26:35+00:00 2026-06-08T20:26:35+00:00

I am writing an Android app that connects to a Bluetooth device, reads data

  • 0

I am writing an Android app that connects to a Bluetooth device, reads data sent from the device, adds it to an AChartEngine graph, and displays the data in a TextView.

My Bluetooth code is quite similar to the threaded implementation in the BluetoothChat sample code (it comes with the SDK). I can see in LogCat that the ConnectedThread loop is executing and thus getting new data, but my TextView stops updating after 7 lines and the graph pauses intermittently (not to mention that it only responds to interaction intermittently). There aren’t any errors being shown in LogCat. Also, if I remove the graph, the problem with the TextView persists.

Why is my UI thread not working when updated from my other threads?


Below are the relevant parts of my code. Each string sent over Bluetooth is received in ConnectedThread and sent to BluetoothController.addToGraph(), which then runs the NewPoints AsyncTask from the viewer class.

private class ConnectedThread extends Thread {
    public ConnectedThread(BluetoothSocket socket, String socketType) { ... } // Initialize input and output streams here

    public void run() {
        while (true) {
            Log.i(TAG, "READ mConnectedThread");
            // Read from the InputStream
            byte[] buffer = new byte[1024];
            bytes = mmInStream.read(buffer);

            // Send the obtained bytes to the UI Activity
            mHandler.obtainMessage(BluetoothController.MESSAGE_READ, bytes, -1, buffer)
                    .sendToTarget();
            Log.i(TAG, "LOOPEND mConnectedThread");
        }
    }
}
public class BluetoothController extends Activity {
    private viewer plotter;
    public static final int MESSAGE_READ = 2;
    // The Handler that gets information back from the BluetoothClass
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_READ:
                    byte[] readBuf = (byte[]) msg.obj;
                    // construct a string from the valid bytes in the buffer
                    String readMessage = new String(readBuf, 0, msg.arg1);
                    addToGraph(readMessage);
                    break;
            }
        }
    };

    protected void addToGraph(String result) {
        // process the string, create doubles x and y that correspond to a point (x,y)
        plotter.new NewPoints().execute(x, y);
    }
}
public class viewer extends Activity {
    // initialize graph, etc.

    @Override
    protected void onResume() {
        // Create handlers for textview
        textHandler = new Handler();

        // Set scrolling for textview
        myTextView.setMovementMethod(new ScrollingMovementMethod());

    protected class NewPoints extends AsyncTask<Double, Void, Void> {
        @Override
        protected Void doInBackground(Double... values) {
            mCurrentSeries.add(values[0], values[1]); // x, y

            if (mChartView != null) {
                mChartView.repaint();
            }

            final Double[] messages = values;
            textHandler.post(new Runnable() {
                @Override
                public void run() {
                    myTextView.append("(" + messages[0].toString() + ", " + messages[1].toString() + ") \n");
                }
            });

            return null;
        }
    }
}

What gives? If more code is necessary, let me know.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:26:37+00:00Added an answer on June 8, 2026 at 8:26 pm

    seems backwards to me… your AsyncTask is updating your textview and currentseries, but AsyncTask should be used to for long running tasks like communicating with other devices/networks. Your UI thread should be doing the updating of the textview and you’ve got it the other way around

    doInBackground should contain the code to talk to your BlueTooth device

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an Android app that connects to a Bluetooth device, reads in data,
So I'm writing an android app that needs to grab book price data from
I'm writing an Android app that reads and parses NMEA sentences from GPS receiver
I am writing an android app that recieves data over bluetooth. The bytes comming
Im writing an android app that connects to my own Jersey rest client. HTTP
I'm writing an Android app that is parsing RSS feeds from different sources. What
I'm writing an Android app that communicates with a PC via bluetooth. During normal
I'm planning on writing an android app that can view and update data on
I just finished building my android app that reads NFC tags. I've been writing
I am currently writing an Android app that, among other things, uses text information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.