1. Are you ready for the Galaxy S20? Here is everything we know so far!

Difficulties with Bluetooth Socket on Different Activity

Discussion in 'Android Development' started by Alvaro Vargas, Oct 24, 2019.

  1. Alvaro Vargas

    Alvaro Vargas Lurker
    Thread Starter

    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?

    #1 Alvaro Vargas, Oct 24, 2019
    Last edited: Oct 24, 2019

    1. Download the Forums for Android™ app!


  2. Alvaro Vargas

    Alvaro Vargas Lurker
    Thread Starter

    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.
    #2 Alvaro Vargas, Nov 4, 2019
    Last edited: Nov 18, 2019
  3. Alvaro Vargas

    Alvaro Vargas Lurker
    Thread Starter

    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.

Share This Page