Android Question BLE volume control device causes activity to stop and re-start (Activity Create, but not first time)

Charlie Burnham

Member
Licensed User
Hi, I'm attempting to use a BLE "Media Button" device (Satechi) to control a video recorder in my App. I've used Erel's example here https://www.b4x.com/android/forum/t...-app-is-in-the-background.124899/#post-787779 It does detect the BLE media button volume_up and volume_down very well. Unfortunately, if the media button is either waking up from sleep or going to sleep (because of no usage for about 15 minutes), it cause the Activity to stop and immediately restart. You get something like this in the log:

** Activity (main) Pause event (activity is not paused). **
** Activity (main) Create, isFirst = false **
Activity Start [this my log]
** Activity (main) Resume **


This would normally not be too much of an issue, however we are capturing mp4 video using Cam2_Exe at this point, and the Activity Create ruins the capture file. I am trying to figure out if this behavior is just the way things have to work, or if the BLE "wakeup and reconnect" commands, whatever they are, could be consumed in a background service without disturbing the focused activity.
 

Charlie Burnham

Member
Licensed User
The activity where this occurs is called "Capture" and is called from the main activity with StartActivity(Capture) and Activity.Finish (we don't need the main activity any more). In Capture there is a reflector instantiated, which is set up like this in Activity_Create:
B4X:
re.Target = Activity
    re.SetOnKeyListener("ActivityOnKey")
    re.RunMethod2("setFocusable", "True",  "java.lang.boolean")
    re.RunMethod2("setFocusableInTouchMode", "True", "java.lang.boolean")
    Activity.RequestFocus

Then, there is a sub to detect the buttons:

B4X:
Private Sub ActivityOnKey(vTag As Object, keyCode As Int, KeyEvent As Object) As Boolean
    
    re.Target = KeyEvent
    Dim Action As Int = re.RunMethod("getAction")     
    Select Action....

    ...Return(True)

The problem is not that the media button clicks are not detected at all. It is that the very first click (intended to start video capture to mp4) is not detected by the handler and, after a significant delay, causes the Capture Activity to restart. Then the clicks are detected properly by the handler. After about 15 minutes, we believe the media button goes to sleep or disconnects itself, or whatever. At this point, the OS apparently causes the Capture Activity to restart again, causing the video capture to stop. If you are recording for more than the sleep timeout of the media button, you get the same result if you click "Stop Record". We tried all of this using your B4A pages example, which uses a media session approach to detect volume clicks, however the result is the same. Android is causing this Activity_Create event whenever the button connects or disconnects itself.
 
Upvote 0

Charlie Burnham

Member
Licensed User
Hi, I solved this. The problem is that BT Media Button (and almost any of these kinds of paired BT devices) all look like keyboards to the OS. When they connect/disconnect, they force a configuration_change event, which re-starts the activity. I just put this in the manifest to inhibit the event:
B4X:
SetActivityAttribute(Main, android:configChanges, "keyboard|keyboardHidden")

I am locked to landscape in my app so I don't care about the rotation event. I do have several other activity modules so I made a manifest line for each one, since this keyboard change event might occur when any one of them is active. Since I need the virtual keyboard as well, I also did this setting on the phone:
[Android 10]
Settings>System>Languages and Input>>Physical Keyboard>>Show virtual Keyboard = On

This is an example of how a person who has not had prior experience or interest in Android can get quite far into using B4A successfully, and still not know the OS itself well enough to avoid spending 2 days trying to solve a simple problem. In Windows, there would be 100 posts about registry hacks for something like this--and most of them would be out of date. Thank you, as always, Erel, for creating B4X.
 
Upvote 0
Top