I am working on an Android application which relays data from a socket connection to a Bluetooth interface. For comfortably communicating I have instantiated two BufferedReader
s which I want to listen to simultaneously in an IntentService
. Furthermore, I want the service to be able to react to events such as a Bluetooth or WiFi connection loss. However, the service should run until a specific exit command is received on the server interface.
So far, I use two threads, but I can't figure out how to comply with my requirements.
SOLUTION
The solution is to use a regular Service
instead of an IntentService
. In the service I use two asynchronous methods which are AsyncTasks
in my case for connecting to the Bluetooth device and to the server. Once the connections are established I start two threads listening for data using the blocking readLine()
method. In the service I simply do nothing which keeps the main thread free for handling Bluetooth connection loses. The service is terminated by my activity once the user wishes to disconnect.