B4A Library Sensor Extender Library

MikieK

Member
Licensed User
I have created a library that offers access to many of Androids SensorManager "Helper" functions. (I'm only using API 8, so Ive missed out a few of the newer functions and a large chunk of gravity constants.)
This library includes the facilities needed to get around the fact that TYPE_ORIENTATION is now depreciated.
I've also added crude low pass and high pass filters (both easily done in B4a, but this keeps them in one place)

I have implement Sensor.GetType() thanks to Erel, and added "_accuracychanged" event. I have included all of the PhoneSensors library in this library (mainly out of neccessity)

I have also attached an example.
Have a go and, let me know what you think.
 

Attachments

Last edited:

MikieK

Member
Licensed User
V0.01

Version 0.01 Released
Including:
  • Entire PhoneSensors functionality
  • _accuracychanged event
  • getType()
  • fixed highpassfilter (I think)
  • changed other methods to be more in line with Android methodology
 

stevel05

Expert
Licensed User
That's excellent, thank you. I've been working to use the orientation / accelerometer to control a game, I have just started to understand most of what it's all doing, the next step was to implement it and now you've already done that.

It will be invaluable.
 

latcc

Banned
Great stuff.

One thing. Users must rem the message box at the end of the code before running as this stops the program from exiting and you will need a task manager to kill this program.

There doesn't seem to be any working code to display non-compass sensors. This would be useful.
 
Last edited:

latcc

Banned
I have noticed that once I run the compass code in my app (and then exit the app) on further starting of the app the compass is fixed at 001 degrees. Only rebooting my phone makes the compass work again.

The code

pa.Initialize2(pa.TYPE_ACCELEROMETER,3)
pm.Initialize2(pa.TYPE_MAGNETIC_FIELD,3)

must NOT be placed here:

if FIRSTTIME then
pa.Initialize2(pa.TYPE_ACCELEROMETER,3)
pm.Initialize2(pa.TYPE_MAGNETIC_FIELD,3)
end if

Within the Activity_Create module or the compass will not work on second run.
 
Last edited:

MikieK

Member
Licensed User
Hi,I'm not sure what you mean, however, I have noticed that if you use this library or the phonesensors library, and stop the app with basic for android (ie. install an update when the app is running) the sensors are not released until you reboot the phone. It is a minor anoyance, that I have no idea how to fix, however, it should have little affect the final product.
 

latcc

Banned
Hi,I'm not sure what you mean, however, I have noticed that if you use this library or the phonesensors library, and stop the app with basic for android (ie. install an update when the app is running) the sensors are not released until you reboot the phone. It is a minor anoyance, that I have no idea how to fix, however, it should have little affect the final product.
To avoid sensor freeze afer a pause I found that the init code must NOT be placed within the IF FIRSTTIME THEN test of the Activity_Create code.

Putting it in this test will freeze the compass sensor on program pause. I placed this initialization code outside this clause and it works fine now.

So this is WRONG...

if FIRSTTIME then
pa.Initialize2(pa.TYPE_ACCELEROMETER,3)
pm.Initialize2(pa.TYPE_MAGNETIC_FIELD,3)
end if

The 'firsttime' test seems is not actioned after a program pause so the sensors are not reinitialized and don't work again (until you reboot). This is why the init code must be placed outside this test clause.
 

hmotiwala

Member
Licensed User
Thanks for the very useful library.
It seems that even after calling method "StopListening" the activity keeps receiving callback to SensorChanged() event. Shouldn't the callbacks stop? Does it mean once started it never stops listening ?
 
Top