Android Question closing asyncstream

mterveen

Member
Licensed User
Longtime User
need simple clarifications on a bluetooth connection.

(serial and asyncstream defined and initialized in starter service)

1) should program always close asyncstream when exiting program, or will it still be valid when program is restarted?

2) should program always disconnect serial when exiting program, or will it still be valid when program is restarted?

3) will closing the asyncstream attached to my bluetooth serial connection close the serial streams and require "reconnecting" the serial connection again or can i simply define another asyncstream and reattach?

purpose of question is to determine best/quickest way to continue bluetooth communications with a device once user has initially connected after starting program, user then exits the program, and then a short time later restarts the program.
 

mterveen

Member
Licensed User
Longtime User
I have gone through the tutorial a few times but do not see the answers to my questions, in particular #3.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
It's a good style to close any ressource you don't need anymore (e.g. close the connection and switch off BT when the app/activity pauses). The rest depends on your app and your needs. So there's no simple yes or no.
 
Upvote 0

mterveen

Member
Licensed User
Longtime User
i understand alot of it is preference. my app has been running for a number of years accessing an arduino based bluetooth solution. it does have a few sporadic quirks though when users exit the app and then restart. i.e. the bluetooth connection is fubared. i assumed by just closing the asyncstream on app exit and leaving the serial connection "active" in the starter service would allow users to quickly reconnect when restarting the app (or selecting it from the background running apps list). basically they would not have to search again for the bluetooth device and recreate a serial connection. just simply have the program create a new asyncstream attached to the existing serial connection. so that is the basis for my questions. looking for a best practice as well as some clarification on what closing the asyncstream actually does. worse case(?) i guess i can close everything on app exit and just go through the whole connection process again if need be.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Closing AsyncStreams does two things:
1. Close the underlying stream.
2. Stop the two threads that are responsible for reading and writing.

You should call Close in two cases:
1. When there is a new connection. If AStream is already initialized then close it.
2. When you no longer want the connection to be open.

No need to close it when the app "exits". There is no such thing in Android. Apps never exit. At some point the OS kills the process and closes everything for you.
 
Upvote 0
Top