Got it - just a matter of cleaning up my connect logic. I made the
following improvements, one or all of which has fixed the problem:
1. Stop re-allocating bluetoothdevice and bluetoothadapter bariables
on each connect. just in case it's a reconnect.
2. before trying to bluetoothSocket.connect(), try{} to run
bluetoothsocket.close() first, in case it hasn't been explicitly
closed yet.
On Dec 28, 10:13 am, Brad Hein <[email protected]> wrote:
> I'm developing an application that interfaces with an RFCOMM endpoint.
> Through bluetooth, my Droid phone connects to this device, initializes
> it, and happily sends and receives data.
>
> A problem I have encountered however is when I walk away from the
> device (while it's sending/receiving) and the bluetooth connection
> goes stale. I easily detect this in my code by various exceptions
> thrown by the inputstream/outputstream/socket. When this happens, I
> immediately "disconnect" from the endpoint by doing the following:
>
> try {
> if (instream != null ) instream.close();
> if (outstream != null ) outstream.close();
> if (mobdSock != null ) mobdSock.close(); // this is the socket.
>
> } catch... etc.
>
> So after performing the above steps to officially "disconnect" the
> dead connection I thought the connection is clean and ready for a new
> connect()...
>
> But what happens is when I come back into range of the device and try
> to connect, I am unable to reconnect, no matter how many times I try.
> connect() throws this exception: java.io.IOException: Device or
> resource busy
>
> The only way I can then reconnect to the device is to disable
> bluetooth on my droid, then re-enable it. Then my app is able to re-
> connect just fine.
>
> BluetoothDevice -> BluetoothAdapter -> BluetoothSocket -> InputStream/
> OutputStream.
>
> I'm currently tearing down the connect from the socket to the right
> (see above). Should I be tearing it down all the way down to the
> adapter instead? or the device? By setting those to null so GC can
> clean them up? This is the best idea I can come up with but it doesn't
> feel right.
>
> Thanks
> Brad
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en