Solved

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

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
This paper addresses the security of Sennheiser DECT Contact Center and Office (CC&O) headsets. It describes the DECT security chain comprised of “Pairing”, “Per Call Authentication” and “Encryption”, which are all part of the standard DECT protocol.
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.

705 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