0

I will like to send data from my app to a server. I collect data from a device that is connected via Bluetooth to the phone. The user opens the app, pair the android phone to the device and then the device sends the data to the phone and the phone sends it to the server.

Because the user needs to use the phone, like make a phone call, send a message or anything else. I need to send the data continuously, every 5 min but the problem is when the app is not active it will not send the data to the server.

It will be better to use a widget? or a service in background? I hope you can help me out.

Thank you.

2 Answers2

1

The way to do this is to put the sending/aggregating code into a service which will run in the background. Then have the app send the data to the service often. There are many places that explain how to do this, for example android developer site and tutorials. In addition, one answer to this Stack Overflow Question goes into some detail that would be relevant.

I would put the Bluetooth communications code in the service itself so it can continue to run when the app is paused or stopped. Then make the app able to stop/start the Bluetooth when the user wants so that the battery isn't wasted.

Community
  • 1
  • 1
hack_on
  • 2,532
  • 4
  • 26
  • 30
1

Take a look into SyncAdapter it allows the framework to optimize data transfers to preserve device's battery life.

The sync adapter component in your app encapsulates the code for the tasks that transfer data between the device and a server. Based on the scheduling and triggers you provide in your app, the sync adapter framework runs the code in the sync adapter component.

http://developer.android.com/training/sync-adapters/creating-sync-adapter.html

Ruslan Ulanov
  • 947
  • 1
  • 10
  • 12