8

I want to write an app for the Samsung S-View cover. To find out if the cover is closed I want to use the magnet which is in the cover and not the light sensor. I tried for my own with the deafult Android magnetic API:

SensorManager manager = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor s = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
manager.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);

tv.setText(event.values[2]);

but I do not know how to interpret the numbers. Of course I tried with the x-axis and y-axis too. They depends on the axis how I hold my phone and there is not a big difference if I have the Flip cover behind or in front of my phone. Do you have any ideas how I can find out if the cover is in front of my phone using the magnetic sensor? Do I have to use the uncalibrated sensor or did I do something else wrong?

Cilenco
  • 6,951
  • 17
  • 72
  • 152

1 Answers1

3

I do believe this feature is made by the magnetic sersor, available in standard android API. Anyway, run an experiment with the smart cover to see if you can use that data.

Dmitry Gryazin
  • 933
  • 1
  • 11
  • 24
  • Oh okay there is a magnetic sensor in the standard android api? I will look for it and try it out. Thank you – Cilenco May 17 '14 at 15:03
  • I wrote a small app which puts the values in a TextView but I do not know how to interpret this numbers. They depends on the axis how I hold my phone. Do you have an idea how I should implement the sensor? – Cilenco May 17 '14 at 17:07
  • 1
    Of course the coordinates you get changes with phone movings: the magnetic field strength is a vector and the coordinate system is connected with the phone's sensor. – Dmitry Gryazin May 21 '14 at 20:31
  • However, you only need to know the magnitude of this vector to detect the changes, corresponding to the flip cover closing. So, modify your program to show the value of x^2+y^2+z^2 and, please, post the results here – Dmitry Gryazin May 21 '14 at 20:42
  • I think you forgot a sqrt() right? I did that and if I put the cover ofer the screen the value is increased a bit (5+-2). But how can I use this now? What do you else what to know? – Cilenco May 24 '14 at 14:07
  • About sqrt: it's not meaningful, your duty is just to detect the cover closing. The next steps are pretty straightforward - you can build your own observer. See [developers guide about SensorEvent](http://developer.android.com/reference/android/hardware/SensorEvent.html) - just implement `onSensorChanged(SensorEvent event)` in the observer class, calculate the magnitude there and make a decision, if the cover is opened/closed. Do stuff you need in listeners. Just in case you're unfamiliar, check out [Observer pattern](http://en.wikipedia.org/wiki/Observer_pattern) – Dmitry Gryazin May 27 '14 at 17:08
  • 2
    Just to make things clear, Samsung uses Hall Effect sensor on the left side of the phone which detects the presence of the attached cover based on its proximity. – Purush Pawar Nov 14 '14 at 10:45
  • Okay thank you for that. Is ther an Android API with which I can read out this sensor? – Cilenco Nov 14 '14 at 12:56
  • Are you sure you are reading the Hall sensor and not the compass? – Evan Langlois Jun 27 '17 at 06:35