• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Bluetooth in background service

sebswed

Lurker
Hi there.

I'm working on this tutorial that makes the app communicate with an Arduino via Bluetooth. The app works fine but I can see that it looses connectivity when the screen on my phone goes in sleep mode.

Since I in a further step like to experiment with SQL I would like to keep the Bluetooth connection open for incoming data.

From what I understand is that I have to create a service for this. I followed this little video and created the extra buttons, java class and included the code in the files shown in the video. I have spend the last 8 hours reading up via google and testing but I'm sorry to say that my knowledge is not sufficient. I have all the files and code from the video in my project.

I don't know how to proceed and appreciate any help and advice :-)
 
Can you be more specific? What are you having problems with?
 
Hi LV.

Thank you for taking the time to reply.

The Bluetooth module I'm using indicates that the connection is lost as soon as I switch app on my phone, press the "home" button or have my screen off. I would like to keep this connection alive at all times as there is data being transmitted from the Arduino to the phone.

In the near future I would like to experiment with an SQL database to have this data stored on my phone but in order to do that I think I would need to solve the connectivity issue.

I found this article in which someone advises to use serverInstance

Does my explanation make any sense?
 
If your app starts a service, then by default, that service will terminate when the app terminates. If you want the service to be restarted when the parent app terminates, then you can use the START_STICKY flag. However this probably would result in your Bluetooth connection being interrupted.
The only sure way of maintaining a constant connection is to leave the app running.
Or you could start a service at boot time, which continually runs in the background.
The bottom line is that any service started by an app is by default bound to the lifetime of that app.

https://developer.android.com/reference/android/app/Service.html
 
Hi LV.

Thank your for your reply.

I understand it is common practice to terminate a service when the app is closed. That is why I need to have this app to be able to run in the background, like a music app connecting to a Bluetooth speaker. The user is able to use other apps, turn the display off etc, but the music is still playing, data is still being transmitted and received.

I too need this application to be able to communicate over Bluetooth when the display is off or when the user is switching to a different app as the app will need to store information in the SQL database and perform certain functions depending on the data that is received (play sound file etc)

Do you have any advice on how to accomplish such background activity?
 
Here's a technique for starting a service when the system boots. Basically you implement a BroadcastReceiver, and listen for the BOOT_COMPLETED event. Once started, your service should run constantly in the background.
 
Back
Top Bottom