• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Difficulties with Bluetooth Socket on Different Activity

Hello Android forums people.

To make it quick, I have been trying to make an app that displays nearby bluetooth devices so I can then connect to one of them and begin interaction, you know, sending simple data, like text. So far, that has been done using one Activity, a bluetooth item adapter, a class to manage the connection and data transmission with different threads, a custom listview with onItemClick events, and some of that other fun stuff, and yes, it works as is.

The thing is, I want to preserve the bluetooth socket and use it in another Activity, which has different buttons to send different kinds of data to the connected device (an electronic device with bluetooth connectivity and a microcontroller....not an arduino).

I have seen so many posts with different suggestions, but nothing really pinpointing in the right direction. "Use threads!, Use Services!, Use the Application class!, Use AsyncTask!". Again, just suggestions, but not a way of explaining how to do it.

I never thought it would be so difficult to pass the active bluetooth socket to another activity. You can certainly pass primitive data types with different methods, but not something like a bluetooth socket; and that is all I really need so I can send data.

So.... any ideas on how I can overcome this?
 
Last edited:
It seems like there is no standard way to do it.

So many views though. I bet it is almost 600 views of people looking for an answer to the same question.
 
Last edited:
In the end, I ended up using Services in order to overcome this issue.

I created a service class with a couple of inner threads that manage the Bluetooth connection. So, when I change activities, the main activity gets unbounded from the service, the Bluetooth connection persists, and finally, on the newer activity, I call a reference to that Service class I am using, binding later on to it and resuming communication tasks to the external Bluetooth device using the previously created socket.
 
In the end, I ended up using Services in order to overcome this issue.

I created a service class with a couple of inner threads that manage the Bluetooth connection. So, when I change activities, the main activity gets unbounded from the service, the Bluetooth connection persists, and finally, on the newer activity, I call a reference to that Service class I am using, binding later on to it and resuming communication tasks to the external Bluetooth device using the previously created socket.

By any change, can you please share the code? I've been looking for the same solution but no luck so far :'(
New to coding here, would really appreciate your help
 
Back
Top Bottom