Keeping a Bluetooth connection across activities on Android [duplicate] - android

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android: How to pass a Bluetooth connection to another Activity?
I have an application in which I am going to be transferring data via a Bluetooth connection.
In my proof of concept app, I was able to put the Bluetooth communication in a different thread and keep all of my other work on the same activity (using ViewFlipper).
However, for my next iteration, I will need to use several different "screens" (activities). The flow would go something like this:
My initial activity will connect up to the Bluetooth device I will be getting the data from.
One activity off of the "main" activity will be to get a list of data from the BT device and display it in a list view. Then as I click on an item I will need to get a more detailed view of the data (which is shown in yet ANOTHER activity but also has to access the BT connection).
There will be additional, specific activities off of the "main" activity.
So my question is how is the best way to manage that, where I initiate the Bluetooth connection ONCE and don't have to initiate it again for each activity?

As Alex said store a controller for the connection as a member of a subclass of a custom application class.
You have to make your own application class that extends the android application class and register this class in your manifest. Now you can get the Application with getApplication and cast it to your subclass. Now you can access the member variables of your Application class.
Some other possibilities are described in the android documentations.
I would not use a static field in your application class. There is only one application class anyway and you can be sure that the whole application object won't be destroyed while your app is running. Some authors of Android books state that this is not that certain for static variables.

Store it as a static field in Application subclass (and instantiate with application context if required).

Related

Nearby Connections advertise with application context

I'm using Nearby Connections framework for Quiz Game application. Also, I'm using MVVM design pattern along with live data and room. When I call Nearby.getConnectionsClient() in activity, I can pass this as argument or applicationContext as argument and everything works. But I don't want handle networking in activity, I want do this for example in view model (or maybe some singleton). But when I use AndroidViewModel(application) and as argument pass application.applicationContext, which is same application context like in activity, it doesn't work. When I'm trying to advertise, I get com.google.android.gms.common.api.ApiException: 13: ERROR. I have no idea what is difference and why this is not working. Plus when I am using activity context and rotate a phone, it got destroyed. So I need start advertising again?
I'm working on quiz app and I want to create lobby activity, where you can find nearby HOSTS and join. Then I want to have access to this connection in next activities, where game is playing. So also I don't have idea how to work with this connection in multiple activities.
Thanks for help.
Using the application context definitely works. Try using the application object itself instead of Application.applicationContext

Same state in two different activities in Android

I'm currently developing an Android application with BLE (Bluetooth Low Energy) API basead on app distributed by Google Developers.
At this moment i already find devices, connect and receive notifications by slave (CC2540 TI).
My general propose is: I have an activity "A" that connects and shows at every 1second the received notifications (data string) and I want to move to activity "B" and continue receiving and visualizing at every 1 second the same data of activity "A", in other words, I want visualize and receive data in real time in more than one activity.
What is the best way/solution for this problem ? I have read about put "extend Application" in my activity B so that access the whole state of activity "A" but i can't.
With intent I only pass to activity "B" one string and not various strings at 1second...
Can you help me ? I appreciate the attention.
Thanks a lot.
Best Regards
You can create a class that manages the BLE connection that belongs to a class that extends Application. Create a get() method in the Application class so you can retrieve the class that manages the BLE connection in any Activity.
I have an example project that does this with a Service (that I run a BLE connection within), but it is the same concept. It is intended to allow you to share BLE connections between Activities.
Project Example

How to work with Bluetooth with Many Activities in Android

i am creating an app in which from many activity we send and receive data through Bluetooth ,
so if i release socket in 1 activity ,so in other activity i have to connect that socket again
,so some time it is connect and sometime it disconnect , so how can i create only 1 socket and share to all over app?i have already tried to make 1 conman class to share but that also did't worked out.
the right way to maintain Bluetooth connection between different activities, and in general - to share any Object across the entire application, is by using a Service class.
android Service derived class is the facility for your app to perform operations, and hold references to objects that needs to maintain it state regardless if any Activity changed, or even when there is no any Activity at all (when your app is in background)
you can bind the current foreground activity to a Service, and by that - to communicate with it from the current activity.
for more info - read: http://developer.android.com/guide/components/services.html
you'll find tones of tutorials and samples by searching on android Service, and binding to a Service...
I guess that for someone who not familiar with android Service - it would look a little complex and hard to understand ant implement, but eventually it's one of the most important component of the API's, so it worth the effort.

Application vs Service vs Intent

I am creating a project which connects to an embedded Bluetooth chip. Currently I have it set up with a separate application class which controls all the bluetooth functionality.
My program initialized with a main menu that has 9 buttons. In the main screen I create the connection to the Bluetooth device. Each button brings me to a separate Activity. Each activity needs to receive different pieces of data from the Bluetooth chip.
My question to you all is, would it make sense for me to use a service instead of an application? From what I understand of a service, it is used because there is always something running in the background. However in this case nothing needs to be running in the background ( unless keeping the connection with the Bluetooth device counts), data is only sent/received when an Activity asks for it.
Or, am I completely off track and shouldn't use either? Just a simple class to act as my data container which can be passed through intents? I know this will work, but am very new to Android and intents seem to be a bit messy. I would rather not use intents if I didn't have to.
I'm also building up a bluetooth connection, and i put the whole communication stuff in a service and bind to this service with every activity that needs to use the connection. This works pretty well for me. You might want to choose this way too.
Actually i earlier realised a way holding the connection in the application, but now i prefer the service way, because i'm using the application for global states.
Using service also reduces the need of intents to a very small amount :)
In your case a static property for the bluetooth connection would be the most pragmatic solution

Using a Handler in multiple Activities

I am developing an application which has around 8 Activities, and a class which is used to connect/receive data to/from an embedded Bluetooth chip. When I started, a Bluetooth object was initialized in my initial Activity, where there was a Handler which received messages from the Bluetooth object.
After poking around on the internet for a while, it seems like the best idea for me is to turn my class into an Application subclass. However, doing this removes the need for me to initialize an object in the MainMenu, which removes my ability to pass it the Handler used.
Does anyone know of a way to eliminate the need for a Handler, so that every time the Bluetooth Application changes it state or receives data, the current Activity can access it?
My main problem with this approach is that the Activity doesn't know when the Bluetooth Application will be sending it messages, the Application waits and listens, and then notifies the Activity when it happens.
OR
Is it bad practice for me to write the Handler into the MainMenu, have it handle messages for ALL the different activities, and then pass the Handler from Activity to Activity?
I'm going to assume that you're trying to achieve the following as it's a little unclear from your question your ultimate aim (sorry!):
Your application has several activities but only one Activity receives the data from the bluetooth device.
The other activities in in your application require the data from the bluetooth device but are not receiving it directly from the bluetooth device. Currently you're providing the data via the one activity mentioned above.
You want to NOT use a Handler to achieve this.
If my above assumptions are correct then you are going along the correct lines but you probably do not want to use a Handler.
You are quite correct in having one Activity handle all the interactions with the Bluetooth device. It simplifies things and provides a much better, cleaner way of handling the Bluetooth device. However you need to get the data from this one Activity to all the others and to achieve this you would probably want to use Broadcasts, BroadcastReceivers and Intents. See here for an overview.
However if you can you might want to take a look at using LocalBroadcastManager as this keeps any broadcasts within your own app's space. Broadcasts are global and should be avoided if you do not need to pass the data outside of your own app due to security implications.
Finally, have you considered using Fragments for your other Activities? Another disadvantage with Broadcasts is there is extra overhead associated with them. If you're keeping data within your app then you can create an interface to be implemented by each of your Fragments and your main activity just calls that interface on the Fragment that is currently selected.
You can use BroadcastReceiver class to send broadcast messages to your activities. see here http://developer.android.com/reference/android/content/BroadcastReceiver.html
When you get the data you need into the application class, you can send it to the activity you want.. just make sure that the activity has registered to receive that broadcast message..

Categories

Resources