简体   繁体   中英

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() {
}

@Override
public IBinder onBind(Intent intent) {
    return Null;
}

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

}

@Override
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();

    }

    @Override
    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 http://www.javacodegeeks.com/2014/01/android-service-tutorial.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM