1

This is my first question in Stack Overflow, hope I find your expertise in solving a problem I am facing.

I was trying to implement a Parcelable Code. Let me tell you -

  1. I have a ConnectDevice Class which when called establishes a connection to any device nearby over bluetooth.
  2. I am calling ConnectDevice in a Service - I mean - when I call this service - it establishes a connection between android and other device.
  3. I need this connection to available to my other application to do some operations with it.
  4. ConnectDevice object has a static variable called "bluetoothDeviceHandler" ( of type BluetoothDeviceHandler Class) which is needed for all other application to drive the operation.

So, to Make it available to other application -

  1. I have made ConnectDevice class as Parcelable.

  2. I am writing ConnectDevice object called "cd" (after instantiating in Service) to parcel so that I can read it back in a same state and can access its variable cd.bluetoothDeviceHandler etc

  3. There is no way I could write object itself in parcel except converting it to json object such.

    gson = new Gson();
    System.out.println("Connect Device : " + deviceHandler);  
    String handler = gson.toJson(deviceHandler);  
    dest.writeString(handler);  
    

Later I unmarshaled it using same policy

    handler = in.readString();
    System.out.println("Handler String " + handler);
    deviceHandler = gson.fromJson(handler, ConnectDevice.class);
    System.out.println("Handler " + deviceHandler);

But no matter what I try - I end up in having cd as null... ( after binding to the service ) Because of that I can't access any of the methods or variables of ConnectDevice in other application.

Below is my Service Code

public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    cd = ConnectDevice.getConnection(ConnectService.this);
    cd.deviceHandler = cd;
    // cd.connectDevice("00:06:66:47:57:80");
    cd.connectDevice("00:06:66:47:E4:42");

}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub

    return new IHandler.Stub() {

        @Override
        public ConnectDevice getHandler() throws RemoteException {
            // TODO Auto-generated method stub
            System.out.println("I am here client connected");

            return cd;
        }
    };
}

This is my sample ConnectDevice Class

public class ConnectDevice implements Parcelable                                        
{   
    public static BluetoothDeviceHandler bluetoothDeviceHandler = null;  
    public static ConnectDevice deviceHandler = null;   

    /* there are lot of codes which sets the bluetoothdeviceHandler when   
      the device is connected over bluetooth.(In service, after calling  
      cd.connect) deviceHandler will be set once the service initializes  
      this class as  */ 


    /*So, I am parcelling this deviceHandler object so that I can access all
      of its method and variable in other applications...( I did not want to
      connect in each application rather centralizing and accessing the same
      connection across the application)*/ 

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            // TODO Auto-generated method stub

            gson = new Gson();
            System.out.println("Connect Device : " + deviceHandler);
            String handler = gson.toJson(deviceHandler);
            dest.writeString(handler);

        }

        void readFromParcel(Parcel in) {

                 handler = in.readString();
             System.out.println("Handler String " + handler);
             deviceHandler = gson.fromJson(handler, ConnectDevice.class);
             System.out.println("Handler " + deviceHandler);
        }

        public static final Parcelable.Creator<ConnectDevice> CREATOR =   
                                new Parcelable.Creator<ConnectDevice>() {
            public ConnectDevice createFromParcel(Parcel in) {

                return new ConnectDevice(in);
            }

            public ConnectDevice[] newArray(int size) {
                return new ConnectDevice[size];
            }
        };

My Another applications binds properly. But I don't get the object, it is always null. And I'm not sure why.

sZinc
  • 11
  • 2
  • cd = ConnectDevice.getConnection(ConnectService.this); cd.deviceHandler = cd; is the part of ConnectService class not ConnectDevice – sZinc Aug 09 '12 at 05:57

0 Answers0