Solved

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

Posted on 2011-09-14
3
403 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
  • 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

In this article we have discussed about the OS X EI Capitan and how to fix Wi-Fi issue in OS X El Capitan. We have explained how to delete system level preferences and create a new Wi-Fi location to resolve Wi-Fi issue.
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
This Micro Tutorial will show you how to maximize your wireless card to its maximum capability. This will be demonstrated using Intel(R) Centrino(R) Wireless-N 2230 wireless card on Windows 8 operating system.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.

770 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