Problem in onServiceConected in an Android Service

edited March 2017 in Android
I'm having a problem trying to use the board in an Android Service.

I've created a service, which is running in another process, not in the app's process, but I have an error in the onServiceConnected which says:
"java.lang.ClassCastException: android.os.BinderProxy cannot be cast to$LocalBinder"

That error is in this line:

public void onServiceConnected(ComponentName name, IBinder service) {
----> serviceBinder = (BtleService.LocalBinder) service; <----
        mwBoard = serviceBinder.getMetaWearBoard(btDevice);

I don't know how to solve this,
any idea? Thanks.


  • edited March 2017
    The error message tells you the service variable is a BinderProxy type so you have to cast it to the BinderProxy class, not the BtleService.LocalBinder class.
  • I've done some research, and the problem is that when you create a service in another process, in the "onServiceConnected" you dont receive the same service variable as in the "onServiceConnected" from your main activity, you will get a generic variable, in this case a BinderProxy type. 

    So in my service now i can't use the board because i can´t initialice the MetaWearBoard variable, because i don't have the serviceBinder.
    I have not found the solution to get the same service variable in my service yet.

    Can I pass the MetaWearBoard variable between activities? (In my case will be between an activity and a service, but i think that is the same scenario).

    Thanks for your time.
  • This post from SO might be useful:

    You can through the use of a global variable.  You can also pass the BluetoothDevice object with the Intent object instead as it is parcelable.
  • That post is very usefull, thank you very much Eric.
This discussion has been closed.