Android Question Permissions to work with Bluetooth and BLE depending on the target Sdk version

red30

Well-Known Member
Licensed User
Longtime User
Right now you can't upload an app with targetSdkversion<31 on Google play, so I had to install target Sdk=31 version to keep my apps updated. As I understand it, now it is desirable to immediately set targetSdkversion=33, since in the future there will still be a mandatory transition to targetSdkversion=33.
With the transition to Sdk=31, I finally got confused with the necessary permissions for Bluetooth to work. At Sdk=33, things get even more confusing.
Does anyone know how to properly work with Bluetooth and BLE on Sdk=33? How to correctly control the necessary permissions depending on the Sdk version of the android device?
Now it seems to me that I figured out the classic Bluetooth. Here is how I do it:
B4X:
Sub InitBT2
    admin.Initialize("admin")
    bt2serial.Initialize("bt2serial")
    If admin.IsEnabled = True Then
        BT2SearchForDevicesPer
    Else
        BT2IsEnabledFalse
    End If
End Sub

Sub BT2IsEnabledFalse
    Wait For (EnableBluetooth) Complete (Success As Boolean)
    If Success = False Then
        ToastMessageShow("Failed to enable bluetooth", True)
    Else
        BT2SearchForDevicesPer
    End If
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 Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result = False Then
            Return False
        End If
        If p.SdkVersion >= 33 Then
            Dim in2 As Intent
            in2.Initialize("android.bluetooth.adapter.action.REQUEST_ENABLE", "")
            StartActivityForResult(in2)
            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
    Dim cls As String = Me
    cls = cls.SubString("class ".Length)
    jo.InitializeStatic(cls)
    Return jo.GetField("processBA")
End Sub

Sub BT2SearchForDevicesPer
    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("No permission...", False)
        Return
    End If
    If Starter.Phone1.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("No permission...", False)
                Return   
            End If
        Next
    End If
    BT2begin
End Sub

Sub BT2begin
    foundDevices.Initialize
    admin.StartDiscovery
    Dim success As Boolean = admin.StartDiscovery
    If success = False Then
        ToastMessageShow(Main.loc.Localize("Ошибка запуска процесса поиска."), True)
    End If
End Sub
But at the same time, I noticed that some devices require GPS to be turned on in order to correctly start the process of searching for Bluetooth devices. Can I do this using an Intent? I do not understand when it is better to make a request to turn on GPS, but I know for sure that from version Sdk>=29, without turning on GPS, the process of finding Bluetooth devices will not work.
 

red30

Well-Known Member
Licensed User
Longtime User
I wrote that if the Sdk of the device is >=29, then without turning on the GPS, the process of searching for Bluetooth devices will not work in this example. I always get the error "Error starting discovery process".
I think you need to check Sdk>=29 and GPS off, then you can send a request to turn on GPS using Intent, or at least notify that without GPS on, device search will not work ...
 
Upvote 0
Top