1

I struggle with the Binder from Android.

I developed an App, which should load different "driver" modules for some devices as plguins. In my Main App, I build an extra module (driverinterface), which contains a "DriverBinder"-Class and an Interface "IDriver". The IDriver Interface contains all functions to interact with the module. When the App starts, it discover all installed plugins and the user can choose, which one should be used.

here is my simple DriverBinder

public class DriverBinder extends Binder {
    public boolean getDriver(){ return true; }
}

and my Interface is a classic one:

   public interface IDriver {

        public enum TriggerButtonMode {
            NONE,
            BARCODE,
            RFID,
            TRACE
        }

        public enum ReadRange {
            AUTO, FAR, NEAR, PROXIMITY
        }

        //properties
        String getManufacturer();
        String getModelType();
        String getDeviceDescription();
        boolean isConnected();
        boolean isReading();
}

Now, in a different App, I wrote a Tiny Device Driver, which implements my "driverinterface" module and use my IDriver Interface (implementation is shorted for posting).

public class TestDriver extends Binder implements IDriver  {
    @Override
    public String getManufacturer() {
        return null;
    }

    @Override
    public String getModelType() {
        return null;
    }

    @Override
    public String getDeviceDescription() {
        return null;
    }

    @Override
    public boolean isConnected() {
        return false;
    }

    @Override
    public boolean isReading() {
        return false;
    }
}

So everything works together, in my Main App I can find all installed plugins and can access them.

Now I connect to my service and try to cast to my DriverBinder Class, but I get an ClassCastException, its not pssobile to cast android.os.BinderProxy to DriverBinder. Here is my Connect:

private ServiceConnection mDriverConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "Driver connected!");

            try {
                DriverBinder connDriver = (DriverBinder) service;
                boolean isConn = connDriver.getDriver();
                String s = "";
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "Driver disconnected...");
        }
    };

I figured out thats a BinderProxy, because it comes from a different process. But I how can I interact with these service and use my functionallity? How can I write external plugins and consume them in my app?

Thanks for Help!

edit:

I forgot the code for the service, who return the binder. Hiere is my TestDriverService:

public class TestDriverService extends Service {

    private final IBinder binder = new DriverBinder();
    private final IDriver mDriver = new TestDriver();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        String status = String.valueOf(((DriverBinder)binder).getDriver());
        Log.i("TestDriverService", "Service started! whoop whoop! Binder Val: " + status);
        return binder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("TestDriverService", "OnStartCommand called!");
        return super.onStartCommand(intent, flags, startId);
    }
}
Marco Rehmer
  • 1,033
  • 2
  • 12
  • 32

0 Answers0