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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:44:03+00:00 2026-05-30T04:44:03+00:00

In my application i want to do bluetooth chat. I’m facing a problem in

  • 0

In my application i want to do bluetooth chat. I’m facing a problem in threading. In my application my android phone will work as server which has a blocking statement

 socket=mServerSocket.accept();

for this purpose i’ve created a child thread so that it will run separately. But before finishing this child thread main thread goes down giving Force Close and if i use the .join() method it hangs up my UI.

What is the solution to run both threads parallel?

this is my code
main Activity

package com.my.bluechat_2_1;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class BlueChat extends Activity {
/** Called when the activity is first created. */
private BlueHandler btHandler=null;
private BluetoothAdapter btAdapter = null;
private Context context=this;
TextView chatWindow=null;


    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    chatWindow=(TextView)findViewById(R.id.textView1);
    doStart();
}

private void doStart(){
    Button btnStart=(Button)findViewById(R.id.button1);
    btnStart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // Get local Bluetooth adapter
                btAdapter = BluetoothAdapter.getDefaultAdapter();
                // If the adapter is null, then Bluetooth is not supported
                if(btAdapter == null)
                {
                    Toast.makeText(context, "Device does not support Bluetooth", Toast.LENGTH_LONG).show();
                }
                if (!btAdapter.isEnabled()) {
                    Intent discoverableIntent = new
                    Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
                    startActivity(discoverableIntent);
                }

                chatWindow.append("Waiting for connection...\n");
                btHandler=new BlueHandler(context,chatWindow,btAdapter);
                Thread acceptThread=new Thread(btHandler);
                acceptThread.start();



            }
        });
}

}

BlueHandler

    package com.my.bluechat_2_1;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.UUID;

    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothServerSocket;
    import android.bluetooth.BluetoothSocket;
    import android.content.Context;
    import android.widget.TextView;
    import android.widget.Toast;

public class BlueHandler implements Runnable{
    // Name for the SDP record when creating server socket
    private static final String SMARTCAM_BT_SERVICE_NAME = "SmartCam";

    // Unique UUID for this application
    private static final UUID SMARTCAM_BT_SERVICE_UUID = UUID.fromString("95b82690-4c94-11e1-b86c-0800200c9a66");

    private BluetoothAdapter btAdapter = null;
    private BluetoothServerSocket btServerSocket = null;
    private BluetoothSocket btSocket = null;
    private InputStream btInputStream=null;
    private Context contextObj=null;
    private TextView textView;



    public BlueHandler(Context contextObj,TextView textView,BluetoothAdapter btAdapter){
        this.contextObj=contextObj;
        this.btAdapter=btAdapter;
        this.textView=textView;

        try {
            btServerSocket=this.btAdapter.listenUsingRfcommWithServiceRecord(SMARTCAM_BT_SERVICE_NAME, SMARTCAM_BT_SERVICE_UUID);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Toast.makeText(this.contextObj, "Service not created", Toast.LENGTH_LONG);
        }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        textView.append("Inside child thread.\n");
        textView.append(btServerSocket+"\n");

         while (true) {
                try {
                    btSocket = btServerSocket.accept();
                } catch (IOException e) {
                    break;
                }
                // If a connection was accepted
                if (btSocket != null) {
                    // Do work to manage the connection (in a separate thread)
                    try {
                        btServerSocket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
                }
            }

         textView.append("Connected.\n");

        try {
            btInputStream=btSocket.getInputStream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        byte[] buffer = new byte[1024];  // buffer store for the stream
        String s;
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream

                bytes=btInputStream.read(buffer);
                s= new String(buffer);
                // Send the obtained bytes to the UI Activity
                textView.append("received ::" +s+"\n");
            } catch (IOException e) {
                break;
            }
        }

    }

    }
  • 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-05-30T04:44:05+00:00Added an answer on May 30, 2026 at 4:44 am

    Are you performing a long operation in the constructor of your children thread? Each long operation must be done in the run() method.

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

Sidebar

Related Questions

I want to write application on android and windows which will use bluetooth. In
I am trying to create a Service for my application which will negotiate Bluetooth
Possible Duplicate: Bluetooth support on Android Emulator I want to develop an Android Application
Hi I am developing an android application in which a form has to be
In my application,I want to display some static files(.html,.htm,.txt) which will be uploaded by
May i achieve that server side application (using android bluetooth API)connect to particular client
I've written Client Server application for Android mobile and PC using bluetooth connectivity, in
I am developing one Android application which communicate with Wifi network. I want to
I am trying to build a bluetooth chat application using j2me. I created a
I want to make Bluetooth Discoverable , is there any way and does android

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.