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

  • Home
  • SEARCH
  • 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 9211143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:13:43+00:00 2026-06-18T01:13:43+00:00

I have the following Bluetooth Server class that extends a Thread to listen for

  • 0

I have the following Bluetooth Server class that extends a Thread to listen for incoming stream:

class SeverThread extends Thread{
    bytes[] inBuffer = new bytes[1024];
    int bytes; 
    while(true){ 
        try{
            bytes = inputStream.read(inBuffer);
        }catch(IOException e){
                    // handle exception 
        }
        System.out.println(new String(inBuffer));
    }
}

The problem with this implementation:

When client first sends "Sanjeev" , output is "Sanjeev". Cool. After that, the client sends smk, the output now is smkjeev. Note that first 3 letters – san – have become smk. Now when the client sends hell, the output is helleev. If the client again sends "Sanjeev" then output is "Sanjeev" , because length of received string is same as the used space inside the buffer.

This is the expected behavior based on above code. But it’s an unintended bug. I want the outputs to be Sanjeev , smk and hello respectively. The problem is apparent. The same buffer in memory is being used to take all the incoming words.

Any suggestions on how to fix this? Possible approaches:

  1. Clear or refresh the buffer for every loop inside while(true). I don’t know how to do this.
  2. Move the declaration of inBuffer inside the while loop. But this will continuously allocate memory, a big wastage.

Thanks.

  • 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-18T01:13:45+00:00Added an answer on June 18, 2026 at 1:13 am

    How about this ?

    bytes = inputStream.read(inBuffer);
    inBuffer[bytes] = '\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following setup: The server will connect via Bluetooth to several devices
Have following setup: MainActivity class - extends activity MyLayout class - extends View Prefs
I have following script that executes all the .reg files in the current directory.
I have a binding for Bluetooth BLE device with the following binding: [BaseType (typeof
I have following table in SQL server (id,name,parentId) (1,nilesh,NULL) (2,nikhil,1) (3,nitin ,2) (4,Ruchi,3) (5,xx,NULL)
I have the following android code which creates a Bluetooth connection to a remote
I have a bluetooth remote that is paired with my linux box, when active,
I use the following code (from Bluetooth Chat sample app) to read the incoming
I have written a program that constantly reads from the Bluetooth via SPP and
I have created iphone bluetooth chat application using following tutorial:http://www.devx.com/wireless/Article/43502/1954. It works well.But when

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.