Android Question Bluetooth Media Prev / Next When Phone Locks

woodpecker

Member
Licensed User
Longtime User
I'm using bluetooth buttons media_next and media_prev in my app but they stop working when the phone locks, its the same whether the phone auto locks via timeout or locks via the power button.

Is there anyway to keep the keys working with the phone screen off?
 

woodpecker

Member
Licensed User
Longtime User

I've spent hours trawelling the threads but still not made much progress, I've managed to get a response from the volume keys using BroadCastReceiver but I'm getting nothing from the media prev, media next or play/pause buttons, here is what I've got, can you help me access these buttons while the phone screen is off?

Service module called Receiver:-

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

Sub Process_Globals
    Dim Broadcast As BroadCastReceiver
End Sub

Sub Service_Create
    Broadcast.Initialize("BR")
End Sub

Sub Service_Start(StartingIntent As Intent)
    Broadcast.SetPriority(2147483647)
    Broadcast.registerReceiver("android.intent.action.MEDIA_BUTTON")
    LogColor("BR registered: " & Broadcast.isRegistered,Colors.Green)
End Sub

Sub Service_Destroy
    Broadcast.unregisterReceiver
End Sub

Sub BR_OnReceive(Action As String, Extras As Object)

    ToastMessageShow("Receiver",False)
    LogColor("Receiver",Colors.Green)
    LogColor(Action,Colors.Red)
End Sub

Main:-

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
End Sub



Sub Activity_Create(FirstTime As Boolean)
    StartService("Receiver")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed And Not(IsPaused(Receiver)) Then
        StopService(Receiver)
    End If
End Sub

In Manifest

B4X:
AddReceiverText(Receiver,
<intent-filter android:priority="2147483647">
    <action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>)
 
AddReceiverText(Receiver, <intent-filter>
   <action android:name="android.media.VOLUME_CHANGED_ACTION" />
    </intent-filter>)
 
Last edited:
Upvote 0
Top