5

When using the light sensor, I have an issue where the onSensorChanged() event is never triggered. The reason is that when the listener is registered, if the lux value remains constant, there's no change thus no event. However, I still want to know the lux value! There's a difference between a constant lux value of 0, and a constant of 60,000!

After some research, I realised that there's no way to directly read the lux value of the sensor. So how do I get the lux value if it's constant, and thus will never trigger an event? Is there a way to set a false value initially (such as -1) so an event will always occur? Or am I thinking about this wrong?

Any assistance would be greatly appreciated!

Glitch
  • 2,785
  • 1
  • 27
  • 50
  • 1
    I've noted, that Android 2.3.4 notifies listener with current value as soon as its registered. While Android 2.2 doesn't do this. – inazaruk May 30 '11 at 20:17
  • Interesting, my phone is 2.3.1. Will have to find someone with 2.3.4 to test. – Glitch May 31 '11 at 04:20
  • I'm using a Meizu M9. However I've replicated this problem on a Motorola Defy. – Glitch Jun 02 '11 at 04:17

2 Answers2

2

Check following example, It explained how to use Sensors

Android Light Sensor

shanethehat
  • 15,460
  • 11
  • 57
  • 87
Ketan Parmar
  • 2,962
  • 19
  • 27
  • That's what I've been doing. The problem is, if the lux is constant, an event will not be triggered. Using that code you linked, put something over the light sensor to maintain a constant lux of 0. Now start the app. You will never get a value, not even a value of 0. It just never reports anything. I want it to report the value once, as you'd expect, but it doesn't. – Glitch May 23 '11 at 11:49
  • I know it's the the best solution but maybe you can check the constant within a timer? – Fredrik May 23 '11 at 11:58
  • @KPBird It's pretty much exactly like yours. In fact, I just compiled your code, and it has the same issue. Did you try the test I outlined? Perhaps it's hardware dependant? – Glitch May 23 '11 at 12:01
  • @Fredrik I'm not sure what you mean? – Glitch May 23 '11 at 12:02
  • I mean that you can set up a timer to check for the current lux value every ten seconds or so. Then it doesn't matter if it's not triggering the event. But I can't tell you if it's a good solution or not because I don't know why, when and how often you want to check the value.. – Fredrik May 23 '11 at 14:22
  • @Fredrik That's the solution I was considering, but it doesn't work if the lux is constant from the start of the apps execution, and there's no data at all. – Glitch May 24 '11 at 10:34
  • The lux is not constant from the start. Haven't you tried usb debug your device? It always sends Logcat message with the lux value. – Fredrik May 24 '11 at 10:57
  • Yes, I have been using USB debug. If I cover the light sensor and run the app, I get no results via Logcat at all. Once I uncover it, I get values. I'm starting to think this is hardware dependant. Perhaps I'll have to add this to the growing list of Android fragmentation issues. >_ – Glitch May 24 '11 at 11:50
  • This example in tablet andromeda 2, not work, bad example!!, when you try run a code, you need know all divices to post this result ! –  Jul 13 '15 at 16:39
0

I'm pretty sure there is no solution to this problem. You just need to hope that the lux changes within a set amount of time, before asking the user directly. Ahh well.

Glitch
  • 2,785
  • 1
  • 27
  • 50