3

I have a project that involves using an Android device to send commands to and receive messages from a remote embedded controller (similar to an Arduino) via Bluetooth SPP.

I have successfully created a single activity app based on a series of tutorials on YouTube. Much of the code in the tutorial is taken from the official BluetoothChat example but it is not done as a service.

Now for my problem... I need to set up a "Home Page" activity that will start other activities when a corresponding button is clicked.

Each activity spawned off the Home Page will have button controls to send commands via Bluetooth to the embedded controller to perform different functions.

The initial Bluetooth connection is started in my Home Page activity. When a new activity is started though, my Home Page's onStop() method is called and the Bluetooth connection is lost.

Short of duplicating the Bluetooth Code in each activity, it would seem a service is the way to go? I found this post on Stack Overflow link that is very good, but I'm new enough to Android programming that the missing details have made me hit a brick wall.

Also, the example does not address multiple activities using the service. What is the best way for me to approach multiple activities using a Bluetooth connection?

Community
  • 1
  • 1
john8791
  • 161
  • 1
  • 2
  • 10

2 Answers2

3

I see two options for you to achieve what you seek:

  • Manage your Bluetooth connection in the Service, and use the Android doc to write a working service. And you can bind each of your activities to your running service using bindService(). The service runs in background and won't stop when you go from one activity to the other. But for that you need to move most of the code you have into a service. Again, follow the doc.

  • Use fragments: Android Doc. And if you know nothing about fragments: Android doc. Basically, a fragment is very similar to an activity but it is recommended by Android to use fragments. You will have one activity that manages you bluetooth connection and hosts your fragments: you will replace all the activities you wanted to have in your app by fragments. So when you navigate from a fragment to the other, your activity won't be stopped and so your bluetooth connection will be safe.

I don't know exactly what you want to do, but the first option is the only right option if your bluetooth connection has to run even when the user has their phone in their pocket. The second option is better if the user doesn't need to keep a bluetooth connection when they leave the app.

Hope it's clear.

Footjy
  • 259
  • 1
  • 14
  • 2
    The fragment idea was the way to go for me. I knew very little about fragments but have spent most of the day learning and my app now works the way I want it. For those with a similar situation, in addition to the Android Docs, I HIGHLY recommend the series of tutorials on fragments by "Slidenerd" at Youtube. – john8791 Oct 01 '14 at 23:03
  • 1
    As far as I understand from the docs service stops when the bounded activities stop. Am I right? So if the second activity starts after some action in the first, (say, button click), how do you bind the second activity to the service? (so that the connection made in the first activity doesn't die) – Sndn Mar 11 '15 at 11:11
  • @Sndn You should create an Activity which is like a master activity for all others. You can call it AbstractActivity or something and extend Activity with that, and then all other activities should exend that AbstractActivity. That's how it won't stop working. -edit- I just saw how old is the question. But the answer might help out someone, who knows. :) – zed Dec 10 '16 at 13:24
-1

You will probably want to run this off of the UI thread... independent of your activity's lifecycle on the stack. Look into providers as well. Providers will really help if you are dealing with communication/access to your app or communicating outside of it (especially across activities and other android apps).

dev-null
  • 27
  • 2