I'm working on an Android application interacting with Arduino. I'm trying to manage multiple activities, each one being responsible for controlling a specific part of the Arduino board.
Unfortunately, tutorials on the web use only one activity. So I don't know how to proceed for keeping the connection with Arduino alive while navigating to a new Activity or for closing it properly and then re-open it on the new Activity.
Any idea?
You could use a Service to connect to Arduino and to keep that connection open. Your Activities can then talk to the Service, being started, stopped, switched, etc.
EDIT: How this can be done is already handled by another question/answer:
Long running ADK Accessory (Service?)
I haven't used Arduino with Java before, but you might want to follow a Singleton pattern. Using a Singleton pattern, whatever objects you are using to connect to it can be accessed from multiple activities and multiple threads.
Using a Singleton will only alleviate your problem though. You still need to figure out how to close/reopen it.
Also, it is important you know how the Activity Lifecycle works. If a user receives a phone call, the screen turns off, or the orientation of the screen changes it will destroy your activity and recreate it. You'll have to handle how you're going to save and restore where you were. This will most likely mean you have to close/reopen the connection.
Related
When I started working on a mobile application using Android SDK, I wanted to make a BluetoothService class, which would allow me to connect to a paired device and communicate with it by injecting the service whenever I need to.
However, when I tried to implement the Service, I realised that Android BluetoothAdapter depends on an Activity (aka. an active view). So I gave up the idea of having a Service and implemented the Bluetooth directly in the view.
Although it worked, I still had a problem.
Basically, my application was a remote that could be used to control a specific joint/stepper motor of an articulated arm at a time.
Whenever the user wanted to switch to another joint, they either could use the upper right menu, or press the picture at the top (depicting a picture of the arm) to get to another view, where they could press on the part they wanted to control (directly on the picture). It would then return them to the main activity with the arrow buttons.
However, the fact that the application is switching to another activity means that the previous one was destroyed. As such, it happened that the application would temporarily lose the Bluetooth connection with the other device, whereas using an injectable BluetoothService would have allowed my application to carry it over different activities.
Why do I need an Activity to use BluetoothAdapter?
Permissions and discovery require an activity because they require user input. The rest of the Bluetooth APIs should, on the whole, not require an activity. In particular, once you get a BluetoothDevice, the actual Bluetooth I/O should be able to be done without an activity.
In your case, it is not clear that you really need to be doing this work in the background, so a service may not be required. Either:
Use a single activity for all of this work, using fragments or something to handle the work being triggered by the action bar item; or
Have the Bluetooth communications be managed by some singleton, perhaps using a variant on the repository pattern, that lives outside of either activity
My goal is to connect my app to a Bluno module. I used and tweaked the codes from this tutorial. Basically my version's launcher activity has a "SCAN" button that lets me scan and connect to the Bluno module. Once connected, it redirects to another activity (which is kind of like the "main" one in a sense that the features are here i.e. chatting, contacts, calls). Problem is, once I've successfully connected and redirected to the other activity, the Bluetooth connection disconnects. How do I fix this? Thank you.
Looking at the library example, the problem is probably that if you just copied it straight up then going to the different activity will pause as the BlunoLibrary is written to be an activity class that you extend. Frankly it is a little bizarre - and as an example it is not very helpful for practical use if you intend to have multiple activities or for some reason can't subclass from that, it's also straight up designed to not run in background.
Specifically if you look at
public void onPauseProcess() {
System.out.println("BLUNOActivity onPause");
scanLeDevice(false);
mainContext.unregisterReceiver(mGattUpdateReceiver);
mLeDeviceListAdapter.clear();
mConnectionState=connectionStateEnum.isToScan;
onConectionStateChange(mConnectionState);
mScanDeviceDialog.dismiss();
if(mBluetoothLeService!=null)
{
mBluetoothLeService.disconnect();
mHandler.postDelayed(mDisonnectingOverTimeRunnable, 10000);
// mBluetoothLeService.close();
}
mSCharacteristic=null;
}
in BlunoLibrary(or the onDestroy, or the onStop) you'll see that it disconnects the service when you switch activity. you could just not call this(from mainactivity), but then you would need to pass that old activity to the new activity object or at least somehow inform the old one where to send incoming messages. Frankly you would be better off just looking from the BlunoLibrary.java what it actually does(to make the bluetooth connection) and making your own based on that information and then you can make it into a service or however you want so that it survives switching of the activity.
I am trying to use multiple activities for my remote app, say, Activity A, Activity B.
But each time I try to create an intent(in Activity A) and start a new activity(Activity B), the Pairing Activity keeps on popping up, but I only want the pairing-activity used in the first time I start the app, and I want the pairing communication to keep on alive (untill I kill the whole app).
Is there a way to implement this? or should I keep all of my app into one Activity? like Activity AB?
I'm not quite sure but I think it might have to do something with AnymoteClientService and
AnymoteSender
If your Anymote logic is kept in a service, then multiple activities can use that to send commands to Google TV. Pairing would only occur once per app.
If you are using the Anymote Library that Google open sourced (https://code.google.com/p/googletv-android-samples/source/browse/#git%2FAnymoteLibrary), then that library requires that the pairing step be done with every connection.
The Anymote-for-Java library that I open-sourced (https://code.google.com/p/googletv-android-samples/source/browse/#git%2FAnymoteLibrary) will remember the pairing information and only requires the pairing step to be done once per device.
I have IP client application which has the following objects:
Socket instance connected to IP server.
Thread instance, executing ThreadFunction. This function continuously reads the socket, when it is connected.
Now I am trying to understand, what should I do, when activity is recreated (for example, after changing screen orientation). Is Socket instance lost? What happens with ThreadFunction? Looking at the other side behavior (IP server), I see that the client, Android application, is not disconnected. So, what should be my strategy in this case: should I create these resources again, or try to restore them by some way?
The answer to your question related to what happens to the resources when the application is destroyed, is that everything goes away, including sockets and runnables.
Even from the server side, the socket will be gone as soon as the server try to communicate over it.
Your best solution, as said already is to manage it using a service. It's quite similar to use an activity, and I encorage you to see a couple of examples.
If you realy want to make what you have now consistent without using a service, you may disable the screen rotation adding android:configChanges="orientation" to AndroidManifest.xml
I recommend using a Service. It is designed to do what you are asking.
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