Bohmian
Bohmian

Reputation: 75

Android Service versus Intent Service for connecting to multiple Bluetooth devices

My app connects to multiple SPP Bluetooth devices all of which stream data. There are four devices streaming, one at 250 Hz and the rest at 100 Hz. I want to put them in a Service but also want each device to run in a separate Thread. Each Thread must also connect to the SQLite database to insert data in real-time. The devices can connect for long periods and can disconnect at anytime and reconnect at anytime. Once started the app could be running for 24 hours or more and always looking for the availability of already paired devices.

The questions is: is it better performance-wise to do this in a separate Service running multiple Threads (a Thread for each connecting device) or is is better to run a separate IntentService (which will of course runs in its own worker Thread?

I have read many other questions and answers in SO and elsewhere about this and can see the advantages and disadvantages of each approach but I cannot find an answer that specifically answers my question. I am not sure if running multiple Threads in a Service will not still block the main Thread at times. On the other hand I do not think I can run multiple Threads in an IntentService at the same time.

Upvotes: 0

Views: 1213

Answers (1)

323go
323go

Reputation: 14274

In your intended scenario, IntentService is not a bad idea, but not in the way you are proposing. Based on the documentation:

IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.

So, launching an IntentService for each bluetooth device would mean one or the other would be blocked. Whenever I communicate with multiple sockets (devices), I assign a thread to each socket... this thread is then responsible for connecting to, communicating with, and reconnecting to the socket as necessary. This pattern is the same used in any server program: There's a ClientThread for each connection, which gets spawned by the connection listener.

However, there's a good place for an IntentService as well: It's great to deliver the data to you sqlite database. Place the datum in an intent, send it to your IntentService, and it will open the database (possibly even start a transaction) when it needs to, then post all data, and when it runs out of work, commits the transaction and closes the database. A lot of the necessary plumbing for that is provided by the API.

Upvotes: 1

Related Questions