1

I am trying to write an Android application which uses Bluetooth but ran into some problems. I succeeded in getting things working by following the guide on Android Developers portal. But i want to organize my code a bit and move everything related to Bluetooth to a separate class/src file. I run into a problem already when trying to turn on Bluetooth. As per above mentioned guide this is done with:

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

However my class is not an activity and startActivityForResult is not defined. I can pass my main activity's context to my Bluetooth class and call startActivityForResult on that. This works. But result is then returned to my main activity and again I have to write code into it, instead of my Bluetooth dedicated class.

Alternatively I can make my Bluetooth class extend the Activity class. But then startActivityForResult returns a nullPointer exception (I take it because my "Activity" is not initialized).

I am completely new in Android app development so I am hoping for some pointers about what to look at to help me solve my problem. I would really like to have everything related to Bluetooth in one class/src file so that I can reuse it in any future apps.

All ideas in how to achieve that will be greatly appreciated.

Thank you.

EEALNT
  • 145
  • 1
  • 13

1 Answers1

0

Composition, not inheritance.

Pass the Activity to your BluetoothGodClass as a contructor, store it in an instance variable, and use that to call the respective method. BUT.

What do you really need to do? Does the UI need to control bluetooth? Create a method turnOnBluetooth(Activity activity) and use that. Do you really need to receive the specific result of the activity you called? If not, use a Context or a BroadcastReceiver.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
  • I already tried your first suggestion. With that I can display the dialog to prompt the user if Bluetooth can be turned on. But I then need to get the user's choice and I can only get it in my main activity (which I passed to my BluetoothGodClass through its constructor). Which means I again have to do a bit Bluetooth related in that activity as well. My purpose of having everything in one class is then defeated. Could you perhaps elaborate a bit more on your second suggestion about Context and BroadcastReceiver? Thank you. – EEALNT Sep 19 '13 at 18:54
  • See [this answer](http://stackoverflow.com/questions/9693755/detecting-state-changes-made-to-the-bluetoothadapter#answer-9694138) for an example. However, for best results that will require you to redesign your class into a fully-fledged Android Service. Oh - activities can give results only to other activities, not services. – Tassos Bassoukos Sep 19 '13 at 19:09
  • 1
    Why not create a BaseBluetoothActivity, that has all necessary code nicely packaged, which you can extend from your proper Activities? – Tassos Bassoukos Sep 19 '13 at 19:10
  • Thanks for the link. I will definitely have a look. Having my class extend Activity already crossed my mind, as I indicated in my question. The reason I have been avoiding it so far is because I thought turning on Bluetooth and using it should not require a full activity. I thought it should be simpler. Also that requires additional modification of the project (manifest, another layout ...). I really wanted to only have one file for my class. But I guess in the end that will probably be the way to go - extend Activity. – EEALNT Sep 19 '13 at 19:30