Search code examples
androidandroid-intentandroid-activitybluetooth-lowenergy

How to pass any object from activity to service?


I want to pass object of "BluetoothDevice" class from an activity to service. I am getting syntaxes for passing Strings or any primitive types but not passing objects. Please Help. Thanx in advance.


Solution

  • you should create a class containing your data to be passed and extend that from Serializable or Parcelable and then you can put an object of that class as an Extra to your Intent and use it in your Service

    EDIT : as rom4ek mentioned, BluetoothDevice is already implements Parcelable and all you have to do is :

    Bundle b = new Bundle();
    b.putParcelable("data", yourBluetoothDeviceObject);
    yourIntent.putExtra(b);
    

    and for retrieving data :

    Bundle b = yourIntent.getExtra();
    BluetoothDevice device = (BluetoothDevice) b.getParcelable("data");