简体   繁体   English

Android服务与Intent服务可用于连接到多个蓝牙设备

[英]Android Service versus Intent Service for connecting to multiple Bluetooth devices

My app connects to multiple SPP Bluetooth devices all of which stream data. 我的应用程序连接到所有流数据的多个SPP蓝牙设备。 There are four devices streaming, one at 250 Hz and the rest at 100 Hz. 有四台设备在流式传输,一台以250 Hz的频率传输,其余以100 Hz的频率传输。 I want to put them in a Service but also want each device to run in a separate Thread . 我想将它们放在Service但也希望每个设备都在单独的Thread运行。 Each Thread must also connect to the SQLite database to insert data in real-time. 每个Thread还必须连接到SQLite数据库以实时插入数据。 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. 启动后,该应用可能会运行24小时或更长时间,并且始终在寻找已配对设备的可用性。

The questions is: is it better performance-wise to do this in a separate Service running multiple Thread s (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 ? 问题是:在运行多个Thread的单独Service (每个连接设备的一个Thread )中执行此操作是否更好,还是在运行IntentService的单独IntentService (在其自己的工作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. 我已经在SO和其他地方阅读了许多其他关于此的问题和答案,可以看到每种方法的优缺点,但是找不到适合我问题的答案。 I am not sure if running multiple Thread s in a Service will not still block the main Thread at times. 我不确定在Service运行多个Thread是否有时仍不会阻塞主Thread On the other hand I do not think I can run multiple Thread s in an IntentService at the same time. 另一方面,我认为我不能同时在IntentService中运行多个Thread

In your intended scenario, IntentService is not a bad idea, but not in the way you are proposing. 在您想要的方案中, IntentService并不是一个坏主意,但并不是您提出的方式。 Based on the documentation : 根据文档

IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. IntentService是Services的基类,可按需处理异步请求(表示为Intent)。 Clients send requests through startService(Intent) calls; 客户端通过startService(Intent)调用发送请求; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work. 该服务将根据需要启动,使用工作线程依次处理每个Intent,并在工作耗尽时自行停止。

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. 存在IntentService类是为了简化此模式并注意机制。 To use it, extend IntentService and implement onHandleIntent(Intent). 要使用它,请扩展IntentService并实现onHandleIntent(Intent)。 IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate. IntentService将接收Intent,启动工作线程,并在适当时停止服务。

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. 因此,为每个蓝牙设备启动IntentService意味着一个或另一个将被阻止。 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. 此模式与任何服务器程序中使用的模式相同:每个连接都有一个ClientThread,它由连接侦听器生成。

However, there's a good place for an IntentService as well: It's great to deliver the data to you sqlite database. 但是, IntentService也是一个好地方:将数据传递到sqlite数据库非常好。 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. 将数据放置在一个intent中,将其发送到IntentService ,它会在需要时打开数据库(可能甚至开始一个事务),然后发布所有数据,并且在数据用尽时提交事务并关闭数据库。数据库。 A lot of the necessary plumbing for that is provided by the API. API提供了许多必要的管道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM