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 9177465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:19:57+00:00 2026-06-17T17:19:57+00:00

I’m sending continuous data from PIC (from analog input) to the bluetooth module and

  • 0

I’m sending continuous data from PIC (from analog input) to the bluetooth module and then I’m trying to read them with android (in HyperTerminal I get values from 0 to 255). How could I change my code to read strings of 3 bytes and immediately display them into the TextView. Now I can display data but it’s too slow and values update only when button is pressed. Here is my MainActivity.java:

public class MainActivity extends Activity {
  private static final String TAG = "bluetooth2";

  Button btn1, btn2, btn3, btn4;
  TextView txtArduino;
  Handler h;

  private static final int REQUEST_ENABLE_BT = 1;
  final int RECIEVE_MESSAGE = 1;        // Status  for Handler
  private BluetoothAdapter btAdapter = null;
  private BluetoothSocket btSocket = null;
  private StringBuilder sb = new StringBuilder();

  private ConnectedThread mConnectedThread;

  // SPP UUID service
  private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

  // MAC-address of Bluetooth module (you must edit this line)
  private static String address = "00:12:09:25:92:57";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);



btn1 = (Button) findViewById(R.id.btn1);                    // activity no. 1
btn2 = (Button) findViewById(R.id.btn2);                    // activity no. 2
btn3 = (Button) findViewById(R.id.btn3);                    // activity no. 3
btn4 = (Button) findViewById(R.id.btn4);                    // activity no. 4
txtArduino = (TextView) findViewById(R.id.txtArduino);      // for display the received data from the Arduino






h = new Handler() {
    public void handleMessage(android.os.Message msg) {

        switch (msg.what) {
        case RECIEVE_MESSAGE:
            byte[] readBuf = (byte[]) msg.obj;
            String strIncom = new String(readBuf, 0, msg.arg1);                     // create string from bytes array
            //Toast.makeText(MainActivity.this, strIncom, Toast.LENGTH_SHORT).show();
            sb.append(strIncom);                                                // append string
            int endOfLineIndex = sb.indexOf("\r\n");                            // determine the end-of-line
            if (endOfLineIndex > 0) {                                           // if end-of-line,
                String sbprint = sb.substring(0, endOfLineIndex);               // extract string
                sb.delete(0, sb.length());                                      // and clear
                txtArduino.setText("Data from PIC: " + sbprint);                // update TextView
                //btn2.setEnabled(true);
                //btn1.setEnabled(true); 
            }
            //Log.d(TAG, "...String:"+ sb.toString() +  "Byte:" + msg.arg1 + "...");
            break;
        }
    };
};


btAdapter = BluetoothAdapter.getDefaultAdapter();       // get Bluetooth adapter
checkBTState();

btn1.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    //btn1.setEnabled(false);
    mConnectedThread.write("0");    // Send "0" via Bluetooth
    //Toast.makeText(getBaseContext(), "Turn on LED", Toast.LENGTH_SHORT).show(); 
    if(v.getId() == R.id.btn1){
    //define a new Intent for the second Activity
    Intent intent = new Intent(MainActivity.this,NewClass1.class);
    //start the second Activity
    MainActivity.this.startActivity(intent);
  }}
});

btn2.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    //btn2.setEnabled(false);  
    mConnectedThread.write("1");    // Send "1" via Bluetooth
    if(v.getId() == R.id.btn2){
        //define a new Intent for the second Activity
        Intent intent = new Intent(MainActivity.this,NewClass2.class);
        //start the second Activity
        MainActivity.this.startActivity(intent);
  }}
});

btn3.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    //btn3.setEnabled(false);  
    mConnectedThread.write("2");    // Send "2" via Bluetooth
    if(v.getId() == R.id.btn3){
        //define a new Intent for the second Activity
        Intent intent = new Intent(MainActivity.this,NewClass3.class);
        //start the second Activity
        MainActivity.this.startActivity(intent);
  }}
  });

btn4.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    //btn4.setEnabled(false);  
    mConnectedThread.write("3");    // Send "3" via Bluetooth          
    if(v.getId() == R.id.btn4){
        //define a new Intent for the second Activity
        Intent intent = new Intent(MainActivity.this,NewClass4.class);
        //start the second Activity
        MainActivity.this.startActivity(intent);
  }}
  });
  }



  @Override
  public void onResume() {
super.onResume();

Log.d(TAG, "...onResume - try connect...");

// Set up a pointer to the remote node using it's address.
BluetoothDevice device = btAdapter.getRemoteDevice(address);

// Two things are needed to make a connection:
//   A MAC address, which we got above.
//   A Service ID or UUID.  In this case we are using the
//     UUID for SPP.
try {
  btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
  errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
}

// Discovery is resource intensive.  Make sure it isn't going on
// when you attempt to connect and pass your message.
btAdapter.cancelDiscovery();

// Establish the connection.  This will block until it connects.
Log.d(TAG, "...Connecting...");
try {
  btSocket.connect();
  Log.d(TAG, "....Connection ok...");
} catch (IOException e) {
  try {
    btSocket.close();
  } catch (IOException e2) {
    errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
  }
}

// Create a data stream so we can talk to server.
Log.d(TAG, "...Create Socket...");

mConnectedThread = new ConnectedThread(btSocket);
mConnectedThread.start();
  }

  @Override
  public void onPause() {
    super.onPause();

Log.d(TAG, "...In onPause()...");

try     {
  btSocket.close();
} catch (IOException e2) {
  errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
}
  }

  private void checkBTState() {
    // Check for Bluetooth support and then check to make sure it is turned on
    // Emulator doesn't support Bluetooth and will return null
    if(btAdapter==null) { 
      errorExit("Fatal Error", "Bluetooth not supported");
    } else {
      if (btAdapter.isEnabled()) {
        Log.d(TAG, "...Bluetooth ON...");
      } else {
        //Prompt user to turn on Bluetooth
        Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        Toast.makeText(getBaseContext(), "Connected!", Toast.LENGTH_SHORT).show();
      }
    }
  }

  private void errorExit(String title, String message){
    Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
finish();
  }

  private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer = new byte[256];  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);        // Get number of bytes and message in "buffer"
                h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();     // Send to message queue Handler
            } catch (IOException e) {

                break;
            }
        }
    }

    /* Call this from the main activity to send data to the remote device */
    public void write(String message) {
        Log.d(TAG, "...Data to send: " + message + "...");
        byte[] msgBuffer = message.getBytes();
        try {
            mmOutStream.write(msgBuffer);
        } catch (IOException e) {
            Log.d(TAG, "...Error data send: " + e.getMessage() + "...");     
          }
    }

    /* Call this from the main activity to shutdown the connection */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}

}

And activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true" >


<TextView
    android:id="@+id/txtArduino"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="" />

<Button
    android:id="@+id/btn1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txtArduino"
    android:text="@string/btn_1" />

<Button
    android:id="@+id/btn2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/btn1"
    android:text="@string/btn_2" />

<Button
    android:id="@+id/btn3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/btn2"
    android:text="@string/btn_3" />

<Button
    android:id="@+id/btn4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/btn3"
    android:text="@string/btn_4" />

</RelativeLayout>
  • 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-17T17:19:58+00:00Added an answer on June 17, 2026 at 5:19 pm

    When I need to run something every so often I use a handler.

    Handler viewHandler = new Handler();
    Runnable updateView = new Runnable() {
            @Override
            public void run() {
    
                    mEmulatorView.invalidate();
                    viewHandler.postDelayed(updateView, 1000);
    
    
            }
        };
    

    To call this in my method:

    viewHandler.post(updateView);
    

    In this case I am updating my screen every 1000ms by invalidating it.

    EDIT: full code sample

    Handler viewHandler = new Handler();
    Runnable updateView = new Runnable() {
    @Override
    
    public void run() {
    
        mEmulatorView.invalidate();
    
        if (statusBool == true) {
            for (int i = 1; i < dataReceived.length() - 1; i++) {
    
                if (dataReceived.charAt(i) == '>') {
    
                    Log.d(TAG, "found >");
                    deviceStatus = 0;
                }
                if (dataReceived.charAt(i) == '#'
                        && dataReceived.charAt(i - 1) != ')') {
    
                    Log.d(TAG, "found #");
                    deviceStatus = 1;
                }
                if ((i + 1) <= (dataReceived.length())
                        && dataReceived.charAt(i) == ')'
                        && dataReceived.charAt(i + 1) == '#') {
    
                    Log.d(TAG, "found config )#");
                    deviceStatus = 2;
                }
    
            }
            statusBool = false;
            viewHandler.postDelayed(updateView, 1000);
    
        }
    }
    };
    
    
    public void onDataReceived(int id, byte[] data) {
    
    dataReceived = new String(data);
    
    statusBool = true;
    Log.d(TAG, "in data received " + dataReceived);
    ((MyBAIsWrapper) bis).renew(data);
    
    
    runOnUiThread(new Runnable(){
    
        @Override
        public void run() {
    
            mSession.write(dataReceived);
    
        }});
    
    
    viewHandler.post(updateView);
    
    
    
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group

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.