views:

106

answers:

1

I am relatively new to Android but have some experience with Java. This may be more of a Multithreading/handler issue as apposed to Bluetooth.

I am writing an Application that has multiple layouts with buttons. I have set up onClickListners for the buttons. Some buttons will change the activity and bring up a new layout with new buttons, and others will send outgoing SPP strings over Bluetooth. The strings will be defined in an XML file and will not change (serial commands).I can hard code the MAC address of the Bluetooth Server and only need to send data, not receive.

I am looking for some general guidance on the direction to go as far as setting up my Bluetooth connection and outgoing transmissions. I have looked at the BluetoothChat example extensively but do not have any good resources in the case of multiple activities.

-Do I use a separate activity to manage all Bluetooth transmissions and connections and create handlers for every case where I would send a Bluetooth message? -Should I add Bluetooth connection+transmission threads in every activity (seems like i would run into issues losing the connection when changing activities)? -Can I use handlers that are not class specific where the BluetoothService Activity could send whatever was prompted by the active/current activity? -Could I just hard code all strings to be sent in the BluetoothService Activity and the UI activity could prompt the BluetoothService Activity to send the requested string based on the button click?

Any information would be greatly appreciated. Just a general direction, small sample, or class suggestions would get me going in the right direction.

Thanks,

Jonathan

A: 

I would manage Bluetooth communications by means of a separate thread. This thread could be launched and be accesible by Application itself, so all activities could access to it. You could use a blocking queue (inside the thread) to enqueue messages from all the activities and report the responses back by means of messages (intents or messages captured by a handler).

Fernando Miguélez