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.
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 .
We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With