Android Question Phone, speaker, call state, advice sought

Avon

Member
Licensed User
It's a few years since I wrote a B4A app, and I never got involved with telephony so I'm rusty and ignorant. Advice on the following would be welcome:

My objective is to use a single click to make a phone call, the speaker being on during the call, and only during the call.

I'm using B4A v 6.8 and my development phone is running Android v6.

The trick seems to be to acquire the call state having started a call activity, and to use the CALL_STATE_OFFHOOK state to switch the speaker on and the CALL_STATE_IDLE to exit my app. Can someone confirm, please?

Questions:

How do I set up a call state listener?

Do I need to turn the speaker off?
 

Avon

Member
Licensed User
Do you mean like this?

The connection is made but the speaker is not used.

B4X:
Sub Button1_Click
   Msgbox("connecting","something's happening!")

   StartActivity(pc.Call("07599999780"))
   'open speakerphone
   Dim rr As Reflector
   rr.Target = rr.GetContext
   rr.Target = rr.RunMethod2("getSystemService", "audio", "java.lang.String")
   rr.RunMethod2("setSpeakerphoneOn", True, "java.lang.boolean")

End Sub
 
Last edited:
Upvote 0

Avon

Member
Licensed User
Erel, I'm attempting to follow your advice re PhoneEvents.

How can I tell whether the callback to PE_PhoneStateChanged is called? I have a toast there but it doesn't show up, maybe it is "below" the dialler?

I'm starting the call like this:
B4X:
Sub Button1_Click
   Msgbox("connecting","something's happening!")

   StartActivity(pc.Call("07999999780"))
End Sub
Below is my service module. Is this the right way to do it?

B4X:
'Service module
Sub Process_globals
   Dim PE As PhoneEvents
   Dim pi As PhoneId
   Dim rr As Reflector
End Sub

Sub Service_Create

   PE.InitializeWithPhoneState("PE",pi)
    
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
   ToastMessageShow(State, 5000)
   If State = "OFFHOOK" Then
     rr.Target = rr.GetContext
     rr.Target = rr.RunMethod2("getSystemService", "audio", "java.lang.String")
     rr.RunMethod2("setMode", 2, "java.lang.int")
     rr.RunMethod2("setSpeakerphoneOn", True, "java.lang.boolean")
   End If
End Sub
 
Upvote 0

Avon

Member
Licensed User
Above code contains a schoolboy error, "ToastMessageShow(State, 5000)" should be ""ToastMessageShow(State, true)".

Also I was attempting to start the service from the wrong place, so it never was started.

Now the service starts, toasts are visible, and the speaker is in use.

However, the speaker remains on after the call terminates, and I have not destroyed the service, so there is a little work still to do.

Thanks for your advice, Erel.
 
Upvote 0
Top