Same state in two different activities in Android - 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

Related

Update existing PayloadCallback in Nearby Connections without reconnecting

I have my MainActivity connecting by Nearby Connections 2.0 to Rpi3 with Android Things installed. And I need to start SecondActivity or ThirdActivity depending on payload I've received in MainActivity. Either second and third activities have their own PayloadCallbacks. So in order to get Payload there, I have to disconnect in MainActivity and connect again in the new activity with new PayloadCallback set in acceptConnection() method.
Is there a way to save existing connection, but change PayloadCallback?
Code can be found on github.com/Mkryglikov/BestCafe. I'm talking about ConnectActivity and ActiveOrderActivity
The typical pattern for what you're trying to do is to keep all your Nearby Connections code in one place (a class named something like NearbyConnectionsManager that all the Activities have access to), and to have all your different Activities register handlers with that class for the different BYTES Payloads you're expecting.
That way, NearbyConnectionsManager's onPayloadReceived() checks the value of the payloadString that it receives, and has a switch statement that invokes the handlePayloadString() callback method of the relevant Activity, thus keeping each Activity's logic contained within itself, while avoiding the need to disconnect and reconnect from your peers.
You may use a Service to encapsulate your Nearby Connections stuff (connection, events, errors, payloads), and allow your components like Activities and Fragments to bind and query the service (via an IBinder). Whether this service will be more persistent or short lived, it is up to your needs.

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

Android: General Direction for Bluetooth SPP App with Multiple UI Activities (Multithreading/Handlers)

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
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).

Keeping a Bluetooth connection across activities on Android [duplicate]

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).

Categories

Resources