I’m adding Bluetooth functionality to my free Android game “forerunner” already on the market. I have the socket correctly set up in a thread embedded in the Main activity. I’ve created another java file for my ConnectThread(extending thread) which will send coordinates of the player between the phones. I’ve taken a look at Service, AysncTask, and Handler. I don’t think any will help me much more besides handler.
Any of the following will allow this to work.
How can I use the Bluetooth socket I connected in the parent activity (created in an embedded Thread) in the child activity?
How can I send data to the child activity from ConnectThread if it wasn’t initialized in the child activity? Basically how can I call public methods in the child activity from the thread?
Is there a way to pass the socket i connected in the parent activity to the child activity?
Put a static (class) variable either in the parent activity or (better) in the application class (to do that create your own application class which is derived from the Android Application class and remember to put that in your manifest in
<application android:name="...">). Assign the socket to this static variable. The child activity can pick it up from there. Because it is static it won’t get removed by the garbage collector when your activity gets destoyed, so make sure that you close it and assign the variable to null when you are done with it.