Android Question Connect a bluetooth speaker with B4A App.

Nikan99

Member
Hello, I am trying to connect a bluetooth speaker with my app but even if it is connected, the sound does not transfer. Actually nothing can be heard. I use the BluetoothAdmin and Ble2.
I have succeeded to read all the devices and I can connect to that device (every time the bluetooth speaker is connected succeesfully, I hear from the device something like "the device has been connected"). But as I mentioned, the sound does not work. I will give some of the code to understand. If anyone can suggest a solution, please help.

B4X:
Private Sub btnBluetooth_Click
    BluetoothPnl.Visible=True
   
    If admin.IsEnabled = False Then
        Wait For (EnableBluetooth) Complete (Success As Boolean)
        If Success = False Then
            ToastMessageShow("1. Bluetooth error...", True)
        End If
    End If
   
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False And rp.Check(rp.PERMISSION_ACCESS_COARSE_LOCATION) = False Then
        ToastMessageShow("2. Bluetooth error...", False)
        Return
    End If
    If phone.SdkVersion >= 31 Then
        For Each Permission As String In Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT")
            rp.CheckAndRequest(Permission)
            Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
            If Result = False Then
                ToastMessageShow("3. Bluetooth error...", False)
                Return
            End If
        Next
    End If
   
    Dim Success As Boolean = admin.StartDiscovery
    If Success = False Then
        ToastMessageShow("4. Bluetooth error...", True)      
    End If
End Sub

Private Sub Admin_DiscoveryStarted
    BleRefresh.Enabled=False
End Sub
Private Sub Admin_DiscoveryFinished
    BleRefresh.Enabled=True
End Sub

Private Sub Admin_DeviceFound (Name As String, MacAddress As String)
    Dim BluetoothDevice As Label, backgrounddevice As ColorDrawable, tf As Typeface
    backgrounddevice.Initialize2(Colors.ARGB(100,0,0,0),0,10,Colors.Black)
    tf = tf.CreateNew(Typeface.DEFAULT_BOLD,Typeface.STYLE_NORMAL)
   
    BluetoothDevice.Initialize(""):    BluetoothDevice.Text=Name: BluetoothDevice.Typeface=tf
    BluetoothDevice.TextColor=Colors.White:    BluetoothDevice.TextSize=18: BluetoothDevice.Gravity=Gravity.CENTER
    BluetoothDevice.Background=backgrounddevice
    BluetoothDevice.Padding=Array As Int(4dip,4dip,4dip,4dip)
    BluetoothDevice.Height=0.15*BLEClv.GetBase.Height
   
    BLEClv.Add(BluetoothDevice,MacAddress)
   
End Sub

Private Sub BLEbackBtn_Click
    BluetoothPnl.Visible=False
    BLEClv.Clear
    admin.CancelDiscovery
End Sub

Private Sub BleRefresh_Click
    BleRefresh.As(B4XView).Rotation=0
    BleRefresh.As(B4XView).SetRotationAnimated(500,180)
   
    BLEClv.Clear
    btnBluetooth_Click  
End Sub

Private Sub DisconectLbl_Click
    Starter.mSerial.Disconnect
End Sub

Private Sub BLEClv_ItemClick (Index As Int, Value As Object)
    Starter.mSerial.Connect(Value)
   
    Wait For mSerial_Connected(Success As Boolean)
   
    If Success Then
        ToastMessageShow("Success.",False)
       
        SetEarPhone(False)
       
    Else
        ToastMessageShow("Failure.",False)
    End If
End Sub

Sub SetEarPhone(Value As Boolean)
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
    Dim mode As Int
    If Value Then mode = 2 Else mode = 0
    r.RunMethod2("setMode", mode, "java.lang.int")
End Sub

Private Sub EnableBluetooth As ResumableSub
    ToastMessageShow("Enabling Bluetooth adapter...", False)
    Dim p As Phone
    If p.SdkVersion >= 31 Then
        rp.CheckAndRequest("android.permission.BLUETOOTH_CONNECT")
        Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
        If Result = False Then Return False
        If p.SdkVersion >= 33 Then
            Dim in As Intent
            in.Initialize("android.bluetooth.adapter.action.REQUEST_ENABLE", "")
            StartActivityForResult(in)
            Wait For ion_Event (MethodName As String, Args() As Object)
            Return admin.IsEnabled
        End If
    End If
    Return admin.Enable
End Sub

Private Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub GetBA As Object
    Dim jo As JavaObject = Me
    Return jo.RunMethod("getBA", Null)
End Sub

As you can see every time I click in customlistview I want to connect to the device, unfortunately the sub: SetEarPhone does not work either boolean is true or false.
 
Top