Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing BluetoothDevice Object to Another Activity though Intent

I'm writing bluetooth client and I have a problem. My first activity display enabled devices in ListView. When click on some item on this list, it should start new activity and pass a BluetoothDevice object there. I write something like that:

public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub
    if(btAdapter.isDiscovering()) {
        btAdapter.cancelDiscovery();
    }
    if(listAdapter.getItem(position).contains("Paired")) {

        BluetoothDevice selectedDevice = devices.get(position);
        Intent intent = new Intent (this, BTActivity.class);
        intent.putExtra("btdevice", selectedDevice);
        startActivity(intent);

Is it possible to pass BluetoothDevice object to another activity? How to extract this object in new activity?

Sorry for my English. If something isn't clear, I will try explain better.

like image 697
user2374593 Avatar asked Apr 12 '14 01:04

user2374593


People also ask

How do you pass an object in an intent?

One way to pass objects in Intents is for the object's class to implement Serializable. This interface doesn't require you to implement any methods; simply adding implements Serializable should be enough. To get the object back from the Intent, just call intent. getSerializableExtra .

How could you pass data between activities without intent?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How can we get data from one Android activity to another using intent?

Using Intents This example demonstrate about How to send data from one activity to another in Android using intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

Yes . It is possible since BluetoothDevice class implements Parcelable

You can get the object in other Activity like this

BluetoothDevice bluetoothDevice = getIntent().getExtras().getParcelable("btdevice");

Make sure getIntent().getExtras() is not null

like image 175
Libin Avatar answered Oct 19 '22 10:10

Libin