title: Using AltBeacon in Xamarin
body: <p>I hope someone can guide me with this issue. I'm attempting to use AltBeaconOrg, an open-source Android iBeacon wrapper created for Xamarin.</p>
  
  <p>The issue I'm facing is when the listener is implemented in a service as follows, it works fine.</p>
  
  <pre><code>    [Service]
  [IntentFilter(new [] {"com.testapp.SensorService"})]
  public class DroidSensorService : Service, IRangeNotifier, IBeaconConsumer
  {
      static readonly string Tag = typeof(DroidSensorService).Name;
  
      private IBinder binder;
  
      private Region _region;
      private BeaconManager _beaconManager;
  
      public DroidSensorService()
      {
  
      }
  
      public override void OnCreate()
      {
          base.OnCreate();
          Log.Debug(Tag, "OnCreate called in the Service");
      }
  
      public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
      {
          Log.Debug(Tag, "Service started");
          _beaconManager = BeaconManager.GetInstanceForApplication(ApplicationContext);
          _beaconManager.Bind(this);
          return StartCommandResult.Sticky;
      }
  
      public override IBinder OnBind(Intent intent)
      {
          binder = new DroidSensorServiceBinder(this);
          return binder;
      }
  
      public override void OnDestroy()
      {
          base.OnDestroy();
          Log.Debug(Tag, "Service has been terminated");
      }
  
      public void OnBeaconServiceConnect()
      {
          _beaconManager.SetForegroundBetweenScanPeriod(5000);
          _beaconManager.SetRangeNotifier(this);
          _region = new AltBeaconOrg.BoundBeacon.Region("Region", null, null, null);
          _beaconManager.StartRangingBeaconsInRegion(_region);
  
      }
  
      public void DidRangeBeaconsInRegion(ICollection&lt;Beacon&gt; beacons, Region region)
      {
  
      }
  }
  </code></pre>
  
  <p>The DidRangeBeacons event is raised when new beacons are found.</p>
  
  <p>If I implement the solution using a class to wrap the beacon code such as:</p>
  
  <pre><code>[Service]
  [IntentFilter(new [] {"com.testapp.SensorService"})]
  public class DroidSensorService : Service, ISensorService
  {
      static readonly string Tag = typeof(DroidSensorService).Name;
  
      private IBinder binder;
  
      private MyBeaconClass _myBeaconClass;
  
      public DroidSensorService()
      {
          _myBeaconClass = new MyBeaconClass(this);
      }
  
      public override void OnCreate()
      {
          base.OnCreate();
          Log.Debug(Tag, "OnCreate called in the Service");
      }
  
      public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
      {
          Log.Debug(Tag, "Service started");
  
          _myBeaconClass.Start();
  
          return StartCommandResult.Sticky;
      }
  
      public override IBinder OnBind(Intent intent)
      {
          binder = new DroidSensorServiceBinder(this);
          return binder;
      }
  
      public override void OnDestroy()
      {
          base.OnDestroy();
          Log.Debug(Tag, "Sensor Service has been terminated");
      }
  }
  
  public class MyBeaconClass : IRangeNotifier, IBeaconConsumer
  {
      private Context _context;
      private Region _region;
      private BeaconManager _beaconManager;
  
      public MyBeaconClass(Context context)
      {
          _context = context;
      }
  
      public Context ApplicationContext
      {
          get
          {
              return _context.ApplicationContext;
          }
      }
  
      public IntPtr Handle
      {
          get
          {
              return _context.Handle;
          }
      }
  
      public void Start()
      {
          _beaconManager = BeaconManager.GetInstanceForApplication(_context.ApplicationContext);
          _beaconManager.Bind(this);
      }
  
      public void OnBeaconServiceConnect()
      {
          _beaconManager.SetForegroundBetweenScanPeriod(5000);
          _beaconManager.SetRangeNotifier(this);
          _region = new AltBeaconOrg.BoundBeacon.Region("Region", null, null, null);
          _beaconManager.StartRangingBeaconsInRegion(_region);
  
      }
  
      public void DidRangeBeaconsInRegion(ICollection&lt;Beacon&gt; beacons, Region region)
      {
  
      }
  
      public bool BindService(Intent intent, IServiceConnection serviceConnection, [GeneratedEnum] Bind flags)
      {
          return _context.BindService(intent, serviceConnection, flags);
      }
  
      public void UnbindService(IServiceConnection serviceConnection)
      {
          _context.UnbindService(serviceConnection);
      }
  
      public void Dispose()
      {
  
      }
  }
  </code></pre>
  
  <p>I get the following error produced:</p>
  
  <p>Could not activate JNI Handle 0x7fefabe700 (key_handle 0x11c1ff5) of Java type 'md53c8887b34fc5ad8a1fb3519f652d3fea/DroidSensorService' as managed type 'Sensors.Droid.Classes.DroidSensorService'.</p>
  
  <p>at Java.Interop.TypeManager.n_Activate (IntPtr jnienv, IntPtr jclass, IntPtr typename_ptr, IntPtr signature_ptr, IntPtr jobject, IntPtr parameters_ptr) [0x00180] in /Users/builder/data/lanes/3540/1cf254db/source/monodroid/src/Mono.Android/src/Java.Interop/TypeManager.cs:176 
    at (wrapper dynamic-method) System.Object:ac4a9174-05aa-4926-9a80-8a8455916649 (intptr,intptr,intptr,intptr,intptr,intptr)</p>
  
  <p>The inner exception is something along the lines of "Attempt to invoke virtual method 'android.content.Context..." (Xamarin Studio cuts it off).</p>
  
  <p>If I move the line instantiating the MyBeaconClass to above the Start() call, the application on the phone crashes with no exception raised in Xamarin Studio.</p>
  
  <p>I appreciate that this code was intended to work in an Activity or a Service class directly, but it would be much neater if I were to be able to achieve this, as I would share this service with other functionality.</p>
  
  <p>Of note: I've tried using the context injected directly and the Android.App.Application.Context directly too.</p>
  
  <p>Thanks very much for reading,
  Richard</p>