Bug 37560 - SensorManager RegisterListener fails with valid Listener and Sensor
Summary: SensorManager RegisterListener fails with valid Listener and Sensor
Status: CLOSED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2016-01-10 19:22 UTC by Andrew Page
Modified: 2016-01-15 01:24 UTC (History)
5 users (show)

Tags: BZCU
Is this bug a regression?: ---
Last known good build:


Attachments
Xamarin Sample Project (34.49 KB, application/x-bzip2)
2016-01-10 19:22 UTC, Andrew Page
Details
Android Studio Java version of same procedure (95.94 KB, application/x-bzip2)
2016-01-10 19:23 UTC, Andrew Page
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
CLOSED ANSWERED

Description Andrew Page 2016-01-10 19:22:22 UTC
Created attachment 14520 [details]
Xamarin Sample Project

The sensor manager will not register a sensor event listener with 'RegisterListener(ISensorEventListener, Sensor, SensorDelay, Handler)' or RegisterListener(ISensorListener, Int32, SensorDelay)

When attempting a register a sensor event listener with a valid sensor and listener, this appears on the console:
[SensorManager] sensor or listener is null

and the listener is never invoked.  

However, using the same sequence of operations with an android studio project in Java, the listener is registered successfully and the listener is invoked.   

This was tested against an Samsung Galaxy Edge6(SM-G925F API 21) as well as other devices.

Attached are sample projects for Xamarin Android and Android Studio
Comment 1 Andrew Page 2016-01-10 19:23:06 UTC
Created attachment 14521 [details]
Android Studio Java version of same procedure
Comment 2 Andrew Page 2016-01-10 19:32:03 UTC
sensorMgr = (SensorManager)ctx.GetSystemService(Activity.SensorService);
rotation = sensorMgr.GetDefaultSensor(SensorType.RotationVector);
if(rotation == null)
  throw new Exception("Null sensor");

register_success = sensorMgr.RegisterListener(this, rotation, SensorDelay.Normal, 100000, mainHandler);

register_sucess comes back null and 
[SensorManager] sensor or listener is null

appears in the 'Application Output'

If I had to hazard a guess, I think there's a local 'Sensor' variable eclipsing an instance variable in whatever is serving as a wrapper around the Android sensor manager object.
Comment 3 Jonathan Pryor 2016-01-14 20:33:26 UTC
When you build your project, the following warning is generated:

> warning : Type 'tiltSensor.Droid.AndroidTilter' implements Android.Runtime.IJavaObject but does not inherit from Java.Lang.Object. It is not supported.

wherein "not supported" means "if you pass instances of AndroidTilter to Java code, Java code will receive a null reference," which is *precisely* what you're reporting.

See also the documentation:

http://developer.xamarin.com/api/type/Android.Runtime.IJavaObject/

> NEVER IMPLEMENT THIS INTERFACE YOURSELF.

Edit your project and change this line:

> public class AndroidTilter : ISensorEventListener, rotateUpdate {

to:

> public class AndroidTilter : Java.Lang.Object, ISensorEventListener, rotateUpdate {

and remove the Dispose() method and Handle property.
Comment 4 Andrew Page 2016-01-15 01:24:34 UTC
So, yet another thing marked as a 'warning' that should be an 'error' and one more obscure message emitted in a sea of other messages(and in a different space entirely  (compile/link vs run).   Not so much failing 'silently' as failing 'quietly' and getting drowned out in the noise.

   And yet, it would be just too darn easy to throw an exception when you pass something that in that is known will not work.   And so much more fun to watch people thrash around and ask for help and have you coming to the rescue,

Heck... why give that up (snirk)