Solved

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

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wifi Access 4 52
Setting up new wifi in new office 3 45
DNS with WiFi Network 5 56
saving voicemail from mobile phone to pc.. 4 49
This is an explanation of a simple data model to help parse a JSON feed
This subject  of securing wireless devices conjures up visions of your PC or mobile phone connecting to the Internet through some hotspot at Starbucks. But it is so much more than that. Let’s look at the facts: devices#sthash.eoFY7dic.
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…

863 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

19 Experts available now in Live!

Get 1:1 Help Now