Solved

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

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

Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

Join & Write a Comment

Need WiFi? Often, there are perfectly good networks that don't have WiFi capability - and there's a need to add it.  - Perhaps you have an Ethernet port into a network but no WiFi nearby. - Perhaps you have a powerline extender and no WiFi at the…
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.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now