Android Question WearOS

ddk1

Member
Licensed User
I run a small B4A app on my Samsung Galaxy Watch 6, which runs WearOS.
I have created a watchface that launches the app when the user taps a number onscreen. The app shows an image (a barcode).
I then wave that image at a scanner which reads the barcode.

When I tilt my hand down to the scanner, the watch screen dims which causes problems. To solve this I added PhoneWakeState.KeepAlive.
The problem is that, although I release the KeepAlive when the image app is closed, the watch seems to still be held awake.
Normally, I raise my wrist, the screen wakes up, then after 5 secs the screen dims again (to save battery). But after running this app, the screen just stays awake. It still dims if I turn my wrist downwards, but raising wrist again wakes the screen, and then it stays awake when it should dim after 5 secs.

In 138960, teddybear suggested adding the SetActivityAttribute(Main, android:keepScreenOn, "true") to manifest. I tried this but it does not keep the screen awake.
Its a standard Activity. I tried it using B4XPages, same.

Any ideas?

Code:
Sub Globals
    Dim pws As PhoneWakeState
    Private B4XImageView1 As B4XImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("myimage")
    B4XImageView1.Load(File.DirAssets, "myimage.jpg")
    pws.KeepAlive(True)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        pws.ReleaseKeepAlive
    End If
End Sub
Layout "myimage" just has a B4XImageView on it.
 
Last edited:

Sagenut

Expert
Licensed User
Longtime User
Try with
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    pws.ReleaseKeepAlive
End Sub
 
Upvote 0

ddk1

Member
Licensed User
I tried without 'If UserClosed' but that caused the screen to go back to dimming when wrist rotated down even though barcode image still showing. ie. stops it doing what I need. I'm guessing that rotating wrist down is causing app to invoke Activity_Pause(UserClosed=False) and if we then release KeepAlive, it undoes what I needed KeepAlive for in the first place.
So I have to have 'If UserClosed' to stop the dimming.
 
Upvote 0

ddk1

Member
Licensed User
Sorted, for anyone who comes across this issue.
Using B4X IDE to load the app onto watch (running wireless debugging) does support logging output back to the IDE. From this I see that when the app first starts on the watch, it immediately calls Activity_Pause(UserClosed=False). Further, when I exit the app on the watch, again it calls Activity_Pause and critically, again UserClosed=False.
So pws.ReleaseKeepAlive never gets called. Modified code and now it works as expected.
B4X:
Sub Globals
    Private pws As PhoneWakeState
    Private B4XImageView1 As B4XImageView
    Private mblnFirstPause As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)  
    mblnFirstPause = True
    Activity.LoadLayout("myimage")
    B4XImageView1.Load(File.DirAssets, "mycard.jpg")      
End Sub

Sub Activity_Resume
    pws.KeepAlive(True)
End Sub

Sub Activity_Pause (UserClosed As Boolean)  
    If mblnFirstPause Then
        Log("Activity_Pause, mblnFirstPause=True")
        mblnFirstPause = False
    Else
        Log("Activity_Pause, mblnFirstPause=False, calling ReleaseKeepAlive")
        pws.ReleaseKeepAlive
    End If
End Sub

Again, in case its useful to someone: when you turn wireless debugging on on watch, on the watch first you must 'Pair new device'. On subsequent dev sessions, you just need to reconnect to the watch wireless debugging port, which changes every time. To help I wrote a script, attached.
 

Attachments

  • Connect Watch Wifi Debugging.zip
    4.2 KB · Views: 143
Last edited:
Upvote 0

ddk1

Member
Licensed User
The code above worked at that time, but something changed and I had to tweak code. Here is newest.
B4X:
Sub Globals
    Private const MYPIC As String = "mycard.jpg"
    Private pws As PhoneWakeState
    Private B4XImageView1 As B4XImageView
    Private mblnKeepAlive As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("myimage")
    B4XImageView1.Load(File.DirAssets, MYPIC)       
End Sub

Sub Activity_Resume   
    If Not(mblnKeepAlive) Then
        pws.KeepAlive(True)
        mblnKeepAlive = True
    Else
        'Log("Activity_Resume, mblnKeepAlive=True")
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If mblnKeepAlive Then
        pws.ReleaseKeepAlive
        mblnKeepAlive = False
    End If
End Sub
 
Upvote 0
Top