How to run Bluetooth connection in the Android background?
I want to write an application that takes numbers from a microcontroller as input via a Bluetooth connection and represents those numbers in a diagram for the user. My first question is how to establish a Bluetooth connection to the device in the background (without user interruptions)?
Now, if the user clicks the “Measure” button, the next page will display the chart, start and stop buttons, and Bluetooth will be enabled.
Here I want to establish a connection under the progress bar displayed for the user.
Is this possible? Can anyone give me an example when the connection is established in the background?
Solution
Don’t go far first, service must be learned
Here is an example of a service
Create a new class and name it 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 this activity, simply write this line in your main activity
startService(new Intent(this, MyService.class));
Stop writing this
stopService(new Intent(this, MyService.class));
Visit this
http://www.javacodegeeks.com/2014/01/android-service-tutorial.html