Android Question [RESOLVED] Android 5.0 - StartBluetoothSco not working.

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hi All

Has anyone any issues with StartBluetoothSco not working on Android 5.0, specifically 5.1.1

I have built a PTT server using B4J, and have a prototype client on B4A. Our customers want to use Bluetooth PTT mic's and it works fine except on this version of android. I have seen other threads on the forum about how to use StartBluetoothSco, but no mention of anyone having issues with it working.

These are the start and stop functions:

B4X:
Sub StartBluetoothSco()
   
   Dim r As Reflector   
   r.Target = r.GetContext
   r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")   
   r.RunMethod("startBluetoothSco")
   Log("startBluetoothSco")
   
End Sub

Sub stopBluetoothSco()
   
   Dim r As Reflector
   r.Target = r.GetContext
   r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
   r.RunMethod("stopBluetoothSco")
   Log("stopBluetoothSco")

End Sub

Any help, guidance would be appreciated.

Regards

John.
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
Update:
Got the MIC working:

B4X:
Sub init_stream

   Dim p As Phone
   
   ' // https://developer.android.com/reference/android/media/MediaRecorder.AudioSource
   Try
       If p.SdkVersion = 21 Or p.SdkVersion = 22 Then
           ' // VOICE_COMMUNICATION         
           Log("connector:init_stream() audioStream.Initialize2")
           audioStream.Initialize2(7,"AudioStream", 11025, True, 16, audioStream.VOLUME_MUSIC)
       Else
           ' // MIC       
           Log("connector:init_stream() audioStream.Initialize2")
           audioStream.Initialize2(1,"AudioStream", 11025, True, 16, audioStream.VOLUME_MUSIC)
       End If

       Log("connector:init_stream() BEFORE StartPlaying")
       audioStream.StartPlaying
       Log("connector:init_stream() AFTER StartPlaying")
   Catch
       Log("connector:init_stream() error - " & LastException.Message)
   End Try       
   
End Sub

Speaker is still not coming out of the PTT device, but comes oout of the phone.

Regards

John.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
UPDATE:
Both are working:

B4X:
Sub init_stream

   Dim p As Phone
   
   ' // https://developer.android.com/reference/android/media/MediaRecorder.AudioSource
   Try
       If p.SdkVersion = 21 Or p.SdkVersion = 22 Then
           ' // VOICE_COMMUNICATION
           Log("connector:init_stream() audioStream.Initialize2")
           audioStream.Initialize2(7,"AudioStream", 11025, True, 16, audioStream.VOLUME_VOICE_CALL )
       Else
           ' // MIC       
           Log("connector:init_stream() audioStream.Initialize2")
           audioStream.Initialize2(1,"AudioStream", 11025, True, 16, audioStream.VOLUME_MUSIC)
       End If

       Log("connector:init_stream() BEFORE StartPlaying")
       audioStream.StartPlaying
       Log("connector:init_stream() AFTER StartPlaying")
   Catch
       Log("connector:init_stream() error - " & LastException.Message)
   End Try       
   
End Sub

Regards

John.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
the BluethoothSCO bit

B4X:
Sub StartBluetoothSco()
   
   Dim r As Reflector
   Dim ph As Phone
   
   r.Target = r.GetContext
   r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")   
   
   Log("StartBluetoothSco:isBluetoothScoOn " & r.RunMethod("isBluetoothScoOn"))
   Log("StartBluetoothSco:isWiredHeadsetOn " & r.RunMethod("isWiredHeadsetOn"))
   
   If (ph.SdkVersion >= 21) And  (ph.SdkVersion <=22) Then
       Log("Lollipop Detected, API Level " &    ph.SdkVersion)
       r.RunMethod2("setSpeakerphoneOn", False,"java.lang.boolean")
       r.RunMethod2("setBluetoothScoOn", True,"java.lang.boolean")
   End If
       
   r.RunMethod("startBluetoothSco")
   Log("StartBluetoothSco:startBluetoothSco")
   
   
End Sub


Sub stopBluetoothSco()
   
   Dim r As Reflector
   r.Target = r.GetContext
   r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
   
   Log("stopBluetoothSco:isBluetoothScoOn " & r.RunMethod("isBluetoothScoOn"))
   
   If r.RunMethod("isBluetoothScoOn") Then
       r.RunMethod("stopBluetoothSco")
       Log("stopBluetoothSco:stopBluetoothSco")
   End If
   
   
   
   
End Sub
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
[UPDATE]

It's a bit hit and miss on samsung devices (Lollipop), does not always work, I will do so more work on this, I suspect I need to put a delay in somewhere.
 
Last edited:
Upvote 0
Top