1

I have a Xamarin.forms application which use a Android Service. The MainPage calls a specific Class in Android part via Dependency Service and this class binds the Service. The Application Context is passed by the MainPage.

public bool BindService() {
  Intent intent = new Intent("com.plustek.service.plkscan.IPlkScanService");
  this._scanServiceConnection = new ServiceConnection(this, this._scanCallBack, this._systemCallBack);
  bool serviceBoundSuccess = this.Context.BindService(intent, this._scanServiceConnection, Bind.AutoCreate);

  if (serviceBoundSuccess) {
    return true;
  }
  return false;
}


Everything is fine, but when the method OnServiceConnected() is called all my member variable are null.

Anybody an Idea ?

     internal class ServiceConnection : Java.Lang.Object, IServiceConnection {

       public IPlkScanService PlcScanService { get; private set; }

       private PlkSystemCallBack _plkSystemCallBack;
       private PlkScanCallBack _plkScanCallBack;
       private PlustecDocumentScanner _plustecDocumentScanner;

       public ServiceConnection(PlustecDocumentScanner plustecDocumentScanner, PlkScanCallBack plkScanCallBack, PlkSystemCallBack plkSystemCallBack) : this(){
         this._plkScanCallBack = plkScanCallBack;
         this._plkSystemCallBack = plkSystemCallBack;
         this._plustecDocumentScanner = plustecDocumentScanner;
       }

       public ServiceConnection(IntPtr dfref, JniHandleOwnership asdf) {

       }

    public void OnServiceConnected(ComponentName name, IBinder service) {
         this.PlcScanService = PlkScanServiceStub.AsInterface(service);
         try {
           this.PlcScanService.InitService(this._plkScanCallBack);
           this.PlcScanService.RegisterSystemCallBack(this._plkSystemCallBack);
           this.PlcScanService.MountScannerDevice();
           this._plustecDocumentScanner.PlcScanService = this.PlcScanService;
         } catch (RemoteException e) {
           e.PrintStackTrace();
         }
       }

}
feldeOne
  • 417
  • 3
  • 18

1 Answers1

0

Found the Issue on an other Post on Stackoverflow.
Two things were wrong. The first thing was, if this ctor is required, something is very wrong:

public ServiceConnection(IntPtr dfref, JniHandleOwnership asdf) {
}

The second, I had a local copy that overrode the base version of the IntPtrHandle : public IntPtr Handle { get; }. Removing that property allowed the map to work correctly.
Further Informartions I found here.

feldeOne
  • 417
  • 3
  • 18