Android Code Snippet Force (Sensitivity) Based Shake Detection Code

Rajesh kannan MJ

Member
Licensed User
Hi,

I wanted to write an app that can make sound whenever a user keeps shaking the phone. When the phone is still (not moving) the sound wil pause. However, I cannot use the sensor library directly, because I had to tune it to work for humans. :) Well, I mean, when a user shakes, the shakes may not be even, it may be irregular, so the values sometimes become "0" which leads to fire the "not shaking" event. So, I tried to tune it with "shaking correcting".

The following code can help you in detecting shakes with sensitive. The sensitive parameter can be set with MIN_FORCE value. The lesser value means, highest sensitivity. For example, if you set 1 for MIN_FORCE, even a lighter shake of phone will fire shake code.

Please note that the code may not be suitable for all events, But It is great my for my app,

The code needs Core, Phone an Java Object Libraries,

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Accelerometer As PhoneAccelerometer
   
    Dim lastX As Float
    Dim lastY As Float
    Dim lastZ As Float
    Dim MIN_FORCE As Int
    Dim totalMovement As Int
    Dim jo As JavaObject
    jo.InitializeStatic("java.lang.Math")
    Dim shakingCorrection As Int
End Sub

Sub Globals
    MIN_FORCE = 2
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
End Sub

Sub Accelerometer_AccelerometerChanged (X As Float, Y As Float, Z As Float)
    'totalMovement = myMaths.Myabs( )
    totalMovement = jo.RunMethod("abs", Array As Object( X + Y + Z  - lastX - lastY - lastZ))
    If (totalMovement > MIN_FORCE) Then   
        Log("shaking")
        'do all things here when you wanted the phone to shake
        'reset shaking correction
        shakingCorrection = 0
    Else
        shakingCorrection = shakingCorrection + 1
        If shakingCorrection > 2 Then
            Log("not shaking, do not do anything")
            shakingCorrection = 0
        End If
    End If
  
    'Assign new values
    lastX = X
    lastY = Y
    lastZ = Z
End Sub
Sub Activity_Resume
    Accelerometer.StartListening("Accelerometer")
End Sub

Sub Activity_Pause (UserClosed As Boolean)
     Accelerometer.StopListening
End Sub
 

Rajesh kannan MJ

Member
Licensed User
Erel, I tried Abs first, But the output is totally different and the code did not work. Am I missing anything?

I shall try to give the values here provided by Abs and Math.abs of Java,

Thank you,
 

Rajesh kannan MJ

Member
Licensed User
Erel,

I tried both code in 2 sessions,

B4X:
    totalMovement = jo.RunMethod("abs", Array As Object( X + Y + Z  - lastX - lastY - lastZ))
    'totalMovement = Abs(X + Y + Z  - lastX - lastY - lastZ)
Only the java object method worked for me. When I commented java object method and made the Abs of b4a, it did not work.

Moreover, I found that Abs of B4a returns float value. But Math.abs returns integer. Does it make any different?

I am not a great coder, But a coder, sorry if i am missing any points :)
 
Top