?
Solved

How to stop a thread that is suspended on a Bluetooth read.

Posted on 2011-09-14
3
Medium Priority
?
407 Views
Last Modified: 2013-12-14
favorite
share [fb] share [tw]
      

My app displays data from a steady stream of Bluetooth text input.

In Android 2.3.4, if I close the socket the read immediately throws an IO exception. In 2.2 is does only most of the time.

I am trying to stop the reading when onStop() is called. Then in onStart() I reconnect.

Is there a better way to kill the thread that is suspended on an inputStream read that is likely to work over all versions?

Thanks TomZ
0
Comment
Question by:Z-Nerd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Expert Comment

by:Gyufa
ID: 36542423
How about using the Thread class's static methods, like currentThread() and interrupt()? When your code runs into an exception, you can interrupt the thread in the catch block.

http://developer.android.com/reference/java/lang/Thread.html#interrupt()
0
 

Accepted Solution

by:
Z-Nerd earned 0 total points
ID: 36554330
Thanks for the suggestion, I tried it and got bogged down in multiple types of exceptions depending on what it was doing at the time of the interrupt and getting compile errors that I was catching exceptions that it said could not be thrown. Even when I did get some working code, it still had reliability problems on Froyo (Galaxy S - Vibrant).

So I backed up and tried using InputStream.available in a loop with a short sleep and a check of a flag that is set to end the read task (so the task was never suspended except on the sleep). This worked great on various android versions.

It seems the trick is to not externally stop the thread but to let it detect the need to quit and return on it's own.

This was all very frustrating since no matter what I did, it worked on my phone,  2.3.4 (Nexus S).
0
 

Author Closing Comment

by:Z-Nerd
ID: 36578995
Seems a bit of a kludge, sort of polling the reads. But the code is now stable and the phone performance does not seem to suffer.
0

Featured Post

Need protection from advanced malware attacks?

Look no further than WatchGuard's Total Security Suite, providing defense in depth against today's most headlining attacks like Petya 2.0 and WannaCry. Keep your organization out of the news with protection from known and unknown threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

DECT technology has become a popular standard for wireless voice communication. DECT devices are not likely to be affected by other electronic devices and signals because they operate in a separate frequency-band.
Here, we have shared an easy step by step tutorial on how to download instagram images and videos on PC, Android Mobile and iOS mobile.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question