java - How do I close any previous Bluetooth sockets after my app starts? -
in app, open , close bluetooth socket on same device each session , listen data. when close one, make sure close input , output streams , socket in cancel
method. still, people app keeps trying read device. know because read logs run()
in listening thread, there flag listening that's set false in cancel
, , listening thread end when socket closed due ioexception
, never happens, socket must still opened. see logs of attempted reads every second of every day though person isn't using app. might because app crashes , cancel
method isn't called. either way can't guarantee cancel method called. how close bluetooth sockets opened when start app, if these opened in new threads created independently?
this guy had same problem didn't see solution: android bluetooth connection doesn't close after application crash
the accepted answer no because:
the current users haven't had
uncaughtexceptionhandler
run code yet , need have previous connections closed when new version releasedthe
uncaughtexceptionhandler
must have reference sockets, doesn't have. want able close bluetooth sockets when app starts.the guy made question asked how information socket store when app starts , want close them, , got no response.
edit:
how open socket (removed logging code):
try { tmp.connect();; } catch (ioexception e) { isconnected = false; try { tmp = (bluetoothsocket) device.getclass().getmethod("createrfcommsocket", new class[] {int.class}).invoke(device, 1); } catch (exception e2) { e2.printstacktrace(); } try { tmp.connect(); setconnected(); } catch (ioexception e1) { e1.printstacktrace(); isconnected = false; cancel(); }
how close socket:
public void cancel() { isconnected = false; listening = false; try { if (manageconnection.mminstream != null) { manageconnection.mminstream.close(); manageconnection.mminstream = null; } } catch (ioexception e) { e.printstacktrace(); } try { if (manageconnection.mmoutstream != null) { manageconnection.mmoutstream.close(); manageconnection.mmoutstream = null; } } catch (ioexception e) { e.printstacktrace(); } try { mmsocket.close(); mmsocket = null; manageconnection = null; } catch (ioexception e) { // logging code } } }
listening:
while (listening == true) { try { synchronized (listeningactivity.lock) { buffer = new byte[mminstream.available()]; mminstream.read(buffer); .... } catch (ioexception e) { // code calls cancel()
Comments
Post a Comment