Android Question [B4X_Pages] Runtime Permission inside Classes (Wait For)

Mike1970

Well-Known Member
Licensed User
Longtime User
Hi everyone,
I'm moving part of my code done in a B4XPage into a Standard Class.

In this code i've a Runtime Permission CheckAndRequest that requires the "Wait For" block

B4X:
Private Sub BeginBLEscan
    rp_ble.CheckAndRequest(rp_ble.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow("Permesso Bluetooth non fornito", True)
        Return
    End If
End Sub

Well... in this manner this does not work, the "Wait For" will never fire because it calls the "B4XPage_PermissionResult" in the module where I declared the class.

Example:
1. I initialized the "ClassA" in the B4XPage called "Home"
2. In the "ClassA" there is the Runtime Permission WaitFor
3. When it should be fired, insted, this appears in the Logs -> *** home: B4XPage_PermissionResult [home]

It is delegated to the B4XPage instead of the Class...

How can i solve this issue (beside of moving the check outside the class)?

Thanks in advance
 

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Move your sub BeginBLEscan back to your (any) VISIBLE module, then call it from your INVISIBLE standard class such as:
B4X:
CallSubDelayed(Main,"BeginBLEscan")
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Move your sub BeginBLEscan back to your (any) VISIBLE module, then call it from your INVISIBLE standard class such as:
B4X:
CallSubDelayed(Main,"BeginBLEscan")
Uhm actually i was trying to create a class that is self-contained.. so it can be used in different project without have to create specific functions outside... sparse in the code. If what I want to do is impossibile.. i will do like you say. Thanks :D
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
There is no simple way to do this.
However, one way is to provide an event which the parent calls, such as ClassPermissionResult.

So in the parent you need

priva
B4X:
private sub B4xpage_permissionresult(Permission As String, Result As Boolean)

if (xui.subexists(mysubclass,"Class_PermissionResult",2) then  ' Call child
      Callsubdelayed3(mysubclass,"Class_PermissionResult",Permissions,result)
end if
end sub

in the class you no have

B4X:
Private Sub BeginBLEscan
    rp_ble.CheckAndRequest(rp_ble.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For Class_permissionResult (Permission As String, Result As Boolean) ' Wait for call from parent
    If Result = False Then
        ToastMessageShow("Permesso Bluetooth non fornito", True)
        Return
    End If
End Sub
 
Upvote 1

Mike1970

Well-Known Member
Licensed User
Longtime User
There is no simple way to do this.
However, one way is to provide an event which the parent calls, such as ClassPermissionResult.

So in the parent you need

priva
B4X:
private sub B4xpage_permissionresult(Permission As String, Result As Boolean)

if (xui.subexists(mysubclass,"Class_PermissionResult",2) then  ' Call child
      Callsubdelayed3(mysubclass,"Class_PermissionResult",Permissions,result)
end if
end sub

in the class you no have

B4X:
Private Sub BeginBLEscan
    rp_ble.CheckAndRequest(rp_ble.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For Class_permissionResult (Permission As String, Result As Boolean) ' Wait for call from parent
    If Result = False Then
        ToastMessageShow("Permesso Bluetooth non fornito", True)
        Return
    End If
End Sub
Hi , thanks for the suggestion.

but in this case, if the sub does not exists, the xui.subexists will fail.

i don’t have to create a sub in the class, because I need it only in the “wait for” construct… I need to intercept it inside a function… so it must be not declared as a function (I think)
I don’t know if I explained it very well
 
Upvote 0
Top