Auro
Auro

Reputation: 1638

How to pass a BluetoothAdapter from one activity to another in Android?

I have a class / activity called MainActivity.java where I have used a BluetoothAdapter BA to check if the Bluetooth is on or not. I have another class called Search.java that searches for nearby Bluetooth devices, but it has to do so using the same BluetoothAdapter created in MainActivity.java. How do I do that?

And also I have a button in my MainAactivity.java whose onClick has to be defined in another class, how do I do that?

I'm sorry if such questions are stupid, but I'm new to Android & have very little experience working with Java.

Thanks for your time!

Upvotes: 0

Views: 1436

Answers (2)

khebrati
khebrati

Reputation: 68

BluetoothAdapter.getDefaultAdapter() is depreciated. instead you should use this as stated in this post

BluetoothManager bluetoothManager = applicationContect.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager.getAdapter()

Upvotes: 0

Alan Wink
Alan Wink

Reputation: 113

I have a class / activity called MainActivity.java where I have used a BluetoothAdapter BA to check if the Bluetooth is on or not. I have another class called Search.java that searches for nearby Bluetooth devices, but it has to do so using the same BluetoothAdapter created in MainActivity.java. How do I do that?

(Edit: please see my edit below, you don't need to implement this in your case) You can implement a Singleton to store and get the BluetoothAdapter, so you can access it anywhere in your project. It would be something like this:

import android.bluetooth.BluetoothAdapter;

public class BluetoothSingleton {
    private static BluetoothSingleton mInstance = new BluetoothSingleton();
    private BluetoothAdapter mBluetoothAdapter;

    private BluetoothSingleton(){
        // Private constructor to avoid new instances
    }

    public static BluetoothSingleton getInstance(){
        return mInstance;
    }

    public void setBluetoothAdapter(BluetoothAdapter adapter){
        mBluetoothAdapter = adapter;
    }

    public BluetoothAdapter getBluetoothAdapter(){
        return mBluetoothAdapter;
    }
}

And also I have a button in my MainActivity.java whose onClick has to be defined in another class, how do I do that?

You can simply create a method to set that button listener, passing a OnClickListener like this:

AnotherClass.java:

...
public void setButtonListener(View.OnClickListener listener){
    mButton.setOnClickListener(listener);
}
...

Then you can set it in your MainActivity.java:

...
AnotherClass another = new AnotherClass();
another.setButtonListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Do what you want here
        }
    });
...

EDIT 1:

Actually BluetoothAdpater is a singleton, so you can just call:

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

And you will get the same adapter.

Upvotes: 2

Related Questions