0

I'm developing a bluetooth chat for android. This is the problem:

My main activity A starts a thread to manage bt connection, and then A starts a new activity B. I must know a way to send String continually from the activity B to the thread called in main activity A. How can I do it?

2 Answers2

0

If you directly start a thread, then there is no way of accessing that thread from another activity and may cause memory leaks.

Your best option is to have separate Service to manage Bluetooth connection.

You can have a separate Service and initiate the tread form the Service (Service runs in the main thread).

And You have to be careful with the starting and stopping of the Service.

There are 2 ways to start the Service,

You can start a Service or you can bind to a Sstrong textervice.

In your case you will have to start the service in can bind to the service form both Activity A and Activity B, or you can start the service in Activity A and bind to it from Activity B.

But only thing you will have to be careful is when to stop the service. otherwise you will have memory leaks.

Once the service is up and running you can communicate with it from-to your Activity. and you can pass what ever information to the tread running in the Service component.

You can read on this in processes-and-threads and services

diyoda_
  • 5,274
  • 8
  • 57
  • 89
0

You can do it via Bind and Callback interface. Please refer old thread.

Communication between Activity and Service

Community
  • 1
  • 1
Rakesh
  • 756
  • 1
  • 9
  • 19