Android Question [Solved] [B4X] Bluetooth enable in a class

Gfy

Member
Licensed User
Longtime User
Hi
I try to do Bluetooth communication in a own class for future use. As example i use the BluetoothChat
When i export all the enable and permission stuff in a class, it dont work.
I have created a small example only with Bluetooth turn on. In the B4XMainPage, the code work, but in the BTenable class it dont. (same code)
What i do wrong?
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
   
    BTen.StartBluetooth        ' NOT WORK
   
'    StartBluetooth            ' WORK
   
End Sub
Choice between BTen.StartBluetooth or StartBluetooth.
 

Attachments

  • BT enable.zip
    15.4 KB · Views: 10
Last edited:

walt61

Well-Known Member
Licensed User
Longtime User
I'm pretty sure it's because of the runtimepermissions event that isn't firing in the class (and have no idea if it's possible to make it do so). If you change the code like this, it works on my phone:

Sub B4XPage_Created in B4XMainPage:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")

    ' Added this here instead of in the class
    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
            ToastMessageShow("Runtimepermission not granted", False)
            Return
        End If
    End If

    BTen.StartBluetooth
    
'    StartBluetooth
    
End Sub

Sub EnableBlueooth in the class:
B4X:
Private Sub EnableBluetooth As ResumableSub
    Log( "Enable BT")
    ToastMessageShow("Enabling Bluetooth adapter...", False)
        
    Dim p As Phone
' Commented this
'    If p.SdkVersion >= 31 Then
'        mRP.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 mAdmin.IsEnabled
        End If
'    End If
    Return mAdmin.Enable

End Sub
 
Upvote 0

Gfy

Member
Licensed User
Longtime User
Hi
I have solved my problem.

Add code to 'Main':
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
  
    ' Add code for all instance of class used.
    If B4XPages.MainPage.BTen.Wait_PermissionResult Then
        B4XPages.MainPage.BTen.Wait_PermissionResult = False
        If B4XPages.MainPage.xui.subexists(B4XPages.MainPage.BTen,"BTenable_PermissionResult",2) Then  ' Call child
            CallSubDelayed3(B4XPages.MainPage.BTen,"BTenable_PermissionResult",Permission,Result)
        End If
    End If
    
    If B4XPages.MainPage.BTen2.Wait_PermissionResult Then
        B4XPages.MainPage.BTen2.Wait_PermissionResult = False
        If B4XPages.MainPage.xui.subexists(B4XPages.MainPage.BTen2,"BTenable_PermissionResult",2) Then  ' Call child
            CallSubDelayed3(B4XPages.MainPage.BTen2,"BTenable_PermissionResult",Permission,Result)
        End If
    End If


    ' Must be the last.
    B4XPages.Delegate.Activity_PermissionResult(Permission, Result)
End Sub

Add Wait_permission and change Wait For:
Private Sub EnableBluetooth As ResumableSub
    Log( "Enable BT")
    ToastMessageShow("Enabling Bluetooth adapter...", False)
  
    Wait_PermissionResult = True    ' Marker for Maim - Sub Activity_PermissionResult (...)

    Dim p As Phone
    If p.SdkVersion >= 31 Then
    mRP.CheckAndRequest("android.permission.BLUETOOTH_CONNECT")
'        Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    Wait For BTenable_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 mAdmin.IsEnabled
        End If
    End If
    Return mAdmin.Enable

End Sub

This declarations must be Public:
    Public xui As XUI
  
    Public BTen As BTenable
    Public BTen2 As BTenable

Now all Wait For commands worked as expected.

Edit: Correct file
 

Attachments

  • BT enable.zip
    15.8 KB · Views: 13
Last edited:
Upvote 0
Top