Reputation: 53
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?
Upvotes: 5
Views: 1627
Reputation: 123
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
Upvotes: 2