0

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?

Robert Lewis
  • 1,847
  • 2
  • 18
  • 43

1 Answers1

0

As @avinash correctly saw, the problem was simply neglecting to declare the Service in the Manifest.

Robert Lewis
  • 1,847
  • 2
  • 18
  • 43