Question

I have an Android service that starts at boot, and I want to get events from an external device using bluetooth. I wrote a sample activity that connects to the device via bluetooth using spp and eveything works ok. I now want to integrate this code into the service. what is the best practice to get data from bluetooth spp using a service (and not activity)? how will it impact the battery life? Thanks!

Était-ce utile?

La solution

Your Bluetooth code should be roughly the same for the service as for the activity. The fact that you got it working in an activity means you've already moved all blocking operations off the main thread. It will probably be easiest to use a regular Service (vs. IntentService) since that is more similar to an Activity.

Regarding best practices, you will probably want to hold a CPU wake lock for the duration of comm with your external device - to ensure the comm completes - and that certainly has implications for the battery, but that seems reasonable and necessary to me. Other then that it shouldn't be much different then doing it in the activity. It would be more battery friendly if you didn't start at boot, but I guess that depends on your requirements.

(Note that SPP is the normal mode of bluetooth comm on Android, so you can look at the Bluetooth chat sample for applicable code - but it sounds like you are probably beyond that stage.)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top