简体   繁体   中英

Bluetooth Communication in Background. Thread, Service, IntentService, AsyncTask..?

For handling a Bluetooth Socket communication in background, what is the proper tool? Thread, Service, IntentService, AsyncTask or another?

The code will be in a different class than the main class and should be stopped and socket and streams terminated when connection is lost.

According to this useful article: http://www.onsandroid.com/2011/12/difference-between-android.html

Thread might be best, but if so, how do I implement it in a different class file and still call it from Main class?

I think it totally depends on your needs, for example

If you want to keep the bluetooth scanning enable all the time then you should use Service, which would stay in background to search devices to do the socket communication with. In addition, you can use another Intent Service in service to handle different tasks such as sending file, communication etc. However, Intent Service is usually for long running processes, in my opinion this is good model for you.

AsyncTask is usually for handling network oriented task such as HTTP request and all.

In addition, you can think to use handlers instead of thread which provide message communication as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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