Android Code Snippet "Raise left hand" - tracking with Smartwatch (WearOS)

Hi
Anyone who wants to track whether the user touches their nose, mouth, ear or face can use this code.
Used here to remind the user that he eats, smokes or bites his nails.

Start und Stop:
Sub btnStart_Click
    CallSubDelayed(Player, "Play")
End Sub

Sub btnStop_Click
    CallSubDelayed(Player, "Stop")
Sub
beenden

Code:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    Private nid As Int = 1
    Private lock As PhoneWakeState
    'Private pl As SimpleExoPlayer
    Dim ps As PhoneSensors
    Dim X,Y,Z As Float
    Private counter As Long
    'Dim v As PhoneVibrate
    Dim b As Beeper
    Public Provider As FileProvider
End Sub

Sub Service_Create
    lock.PartialLock
    ps.Initialize(9)
    b.Initialize(100,400)
End Sub


Sub Service_Start (StartingIntent As Intent)
    Public Provider As FileProvider
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
    Service.StopAutomaticForeground
    Log(ps.MaxValue)
    If ps.StartListening("Sensor") = False Then
        'lbl.Text = sd.Name & " is not supported."
        Log(ps.Timestamp & " is not supported.")
    End If
End Sub

Public Sub Play
    Service.StartForeground(nid, CreateNotification("Fettwarner An"))
    ps.StartListening("Sensor")
End Sub

Sub pl_Error (Message As String)
    Log(Message)
End Sub

Sub pl_Complete
    Log("complete")
End Sub

Public Sub Stop
    
    Service.StopForeground(nid)
    ps.StopListening
    'pl.Pause
    StopService("Player")
End Sub


Sub CreateNotification (Body As String) As Notification
    Dim notification As Notification
    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "icon"
    notification.SetInfo("Tracking Essen", Body, Main)
    Return notification
End Sub

Sub Service_Destroy
    'pl.Pause
    lock.ReleasePartialLock
End Sub

Sub Sensor_SensorChanged (Values() As Float)
    
    counter = counter + 1
    If counter Mod 100 = 0 Then
        X = Values(0)*1000
        Y = Values(1)*1000
        Z = Values(2)*1000
        'X = Round(X)
        'y = Round(y)
        'z = Round(Z)

        
        Log("                "&X & "," & Y & "," & Z)
        
        If  x <10000 And x >5000 Then
            If y > -8000 And y < 6000 Then
                If z < 9300 And z > -2000 Then
                    Dim v As PhoneVibrate
                    Log("Treffer bei ,"& X& ","&Y&","&Z)
                    v.Vibrate(600)
'                    If LEDCorona.KVS2.Get("Beepsound") Then
'                        b.Beep
'                    End If
                End If
            End If
        End If
    End If
End Sub
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Very good.

It could be be used as a warning during a time when a pandemic demands extra precaution in transferring
pathogens to the eyes (or mouth nose when mask is absent/poor fitting).

It also could be used to reduce repetitive scratching in response to an itch or as a nervous gesture.

This makes me want to buy a smart watch and start experimenting. I think I will.
 

kimstudio

Active Member
Licensed User
Longtime User
Nice.

This is actually a posture detection algorithm, instead of a continuous movement from hand down to up.

It could be a little bit more stable if not only use one sample to determine the hand raise but use several continuous samples to confirm the posture to avoid false alarms.
 
Last edited:
Top