In the Service class:
class AeroBluetoothService extends Service { ...
private final IBinder asBleBinder = new LocalBinder();
public class LocalBinder extends Binder {
AeroBluetoothService getService() {
return AeroBluetoothService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return asBleBinder;
}
In the client Activity:
Intent bindAsBleIntent;
@Override
public void onServiceConnected( ComponentName className, IBinder service ) {
AeroBluetoothService.LocalBinder asBleBinder = (AeroBluetoothService.LocalBinder) service;
asBleServiceRef = asBleBinder.getService();
}
In Client's onCreate():
bindAsBleIntent = new Intent( getApplicationContext(), AeroBluetoothService.class );
bindService( bindAsBleIntent, /*ServiceConnection*/ this, Context.BIND_AUTO_CREATE );
The problem is that when I try to call a Service method from the Client:
asBleServiceRef.scanForAero();
the reference to the Service instance, asBleServiceRef
, is null
. It is as though the onServiceConnected()
callback is not being called (or is passing a null argument).
I copied this code quite carefully from an Android example. I just noticed that the example calls bindService()
from its onStart()
method, whereas I'm calling from onCreate()
. Could that make any difference? What's the problem?