Search code examples
androidcode-organization

Android, where to put my own Bluetooth class


Well, I'm at a dilemma here. I made my own class that uses the Bluetooth class from android but I'm not sure where to put it. Extending the android Bluetooth class seems like a good idea but I need to override the onActivityResult() which is only available to an activity class. So, where would I put my class so that I have access to onActivityResult() (keeping in mind the idea here is to use as few dependencies as possible)?

In other words, I want to move the Bluetooth code from the main activity to a separate class.


Solution

  • You should to use separate file for each class. You can create a folder "engine". For example: com.mycorp.myapp.engine. You can get access to onActivityResult() very simple. For example: MainActivity.onActivityResult(). Note: function should be public. Or you can pass your activity to your CustomBluetooth's constructor.

    public class CustomBluetooth {
    
        private Activity mActivity;
    
        /* Constructor */
        public CustomBluetooth (Activity pActivity ) {
            super();
            this.mActivity = pActivity;
        }
    
        /* Your functions */
        public int getResult() {
            return this.mActivity.onActivityResult();
        }
    }
    

    Alex. P.S. Sorry for my English:)