Beesabt Beesabt - 4 months ago 38
Java Question

How can I run bluetooth connection in the background in android?

I would like to write an app which get numbers as input from a microcontroller through bluetooth connection and it represents them in a diagram for the user. My first problem is how can I build up the bluetooth connection with the device in the background (without user interruption)?

Now, if the user clicks on the 'Measure' button the next page will appear with the diagram and the Start and Stop buttons and the Bluetooth will be enabled.
Here, I would like to build up a connection under the progress bar appears for the user.

Is it possible? Could anybody show me an example for it when the connection is built up in the background?


don't go far first You Have to learn the service Here is the Example of Service

Create a new Class and Name it for Exmaple: MyService

public class MyService extends Service {
public MyService() {

public IBinder onBind(Intent intent) {
    return Null;

public void onCreate() {
    Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();


public void onStart(Intent intent, int startId) {
    // For time consuming an long tasks you can launch a new thread here...
    // Do your Bluetooth Work Here
    Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();


    public void onDestroy() {
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();


to start the activity simply write this line in your main activity

startService(new Intent(this, MyService.class));

to stop write this

stopService(new Intent(this, MyService.class));

visit this