Android Question Bluetooth problem

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to Everybody here
I am facing a problem with Bluetooth management. My original App, developed years ago, had no problem to connect to Bluetooth devices, and still has no problems, maintaining it "as is". As long as I am reworking it, during execution, I see the Android message informing that the App may be dangerous because developed for previous versions etc. Annoyed by this message, I checked the manifest and changed the "android:targetSdkVersion" from the original 26 to 31. The result is that the App crashes saying that I must add BLUETOOTH_SCAN and BLUETOOTH_CONNECT permissions. Adding these permissions to the manifest has no effect. Being suspicious on my code, I used the original Bluetooth example posted here by Erel, and the problem is the same. The apparent conclusion is that bluetooth works only for versions <=26 ... Of course this is rather weird and I hope to miss something...
Thanks in advance for any suggestion..
 
Solution
try adding this

B4X:
Sub btnSearchForDevices_Click
    Dim phone As Phone
    Dim Permissions As List

    If phone.SdkVersion >= 31 Then
        Permissions = Array("android.permission.BLUETOOTH_SCAN", _
                            "android.permission.BLUETOOTH_CONNECT", _
                            rp.PERMISSION_ACCESS_FINE_LOCATION)
    Else
        Permissions = Array(rp.PERMISSION_ACCESS_FINE_LOCATION)
    End If

    For Each per As String In Permissions
        rp.CheckAndRequest(per)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result = False Then
            ToastMessageShow("No permission: " & per, True)
            Return
        End If
    Next

    Dim success As Boolean =...

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Further comment: I verified now another maybe important "detail" (important to save the time of other people facing this problem): after modifying the manifest and unsuccessfully running the App, no matter if you change back the manifest specifications. In other words: initially, with version 26, works; change to version 31, crashes; putting again version 26, crashes. I "resolved" only uninstalling the App. Then with version 26 works again.
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
..The apparent conclusion is that bluetooth works only for versions <=26 ...
the "conclusion" is: In your app, bluetooth works only for versions <=26

Edit: my tests that include API 34; Too me it's just a permition issue.

I think the best way to go about this is:
1. Test the example as-is using API level 31, and if it crashes, post the log
2. Share the log, when your current app crash
 
Last edited:
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Thanks my friend. BUT I wrote "Being suspicious on my code, I used the original Bluetooth example posted here by Erel, ..." . So, maybe my english is not enough good, but it seems not a problem of my code (btw i am enough old to know that 95% of problems are in my code). About the Log: it simply says to add the BLUETOOTH related permissions. Adding BLUETOOTH_CONNECT only, asks for BLUETOOTH_SCAN. Adding this latter, curiously, continues to crash same way. I Wrote also that, even returning to 26 it continues crashing, until you uninstall the App. After reinstalling, 26 works again, and 31 not, of course. When crashing, in debug mode, it gives no useful message, but to add the abovementioned Permissions (which are both present in the manifest). Therefore, the conclusion is that it is not that simple to understand what happens... My suspect is that it is necessary a more advanced permission management, which is out of my actual skills. That's why I posted my question here ...
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
I undertand, are you using this example: ?
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
I undertand, are you using this example: ?
Of course not, this is LowEnergy Bluetooth. I did a search here to get the attched code, which is a basic chat example. BTW, to day, I have another app that works, with this manifest:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="15"  android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
AddManifestText(<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="29" />
)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
SetApplicationAttribute(android:requestLegacyExternalStorage, true)
CreateResourceFromFile(Macro, Core.NetworkClearText)
As you see, here the target version is 29... Maybe this gives more info.
 

Attachments

  • Bluetooth.zip
    12.4 KB · Views: 82
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
try adding this

B4X:
Sub btnSearchForDevices_Click
    Dim phone As Phone
    Dim Permissions As List

    If phone.SdkVersion >= 31 Then
        Permissions = Array("android.permission.BLUETOOTH_SCAN", _
                            "android.permission.BLUETOOTH_CONNECT", _
                            rp.PERMISSION_ACCESS_FINE_LOCATION)
    Else
        Permissions = Array(rp.PERMISSION_ACCESS_FINE_LOCATION)
    End If

    For Each per As String In Permissions
        rp.CheckAndRequest(per)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result = False Then
            ToastMessageShow("No permission: " & per, True)
            Return
        End If
    Next

    Dim success As Boolean = Starter.Manager.SearchForDevices
    If success = False Then
        ToastMessageShow("Error starting discovery process.", True)
    Else
        ProgressDialogShow2("Searching for devices...", False)
    End If
End Sub

Manifest:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="31"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.

AddPermission(android.permission.BLUETOOTH_SCAN)
AddPermission(android.permission.BLUETOOTH_CONNECT)
AddPermission(android.permission.ACCESS_FINE_LOCATION)

Add the library phone

working ☕ ☕ in Android 14

test OLD_b4XCode1.jpg
 
Last edited:
Upvote 0
Solution

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Yes. Your answer is exactly what I was missing. The solution is not to simply add the permission in the manifest (I tested it), but also to modify the SearchForDevices sub. Btw you added the Phone library, which is not in the original version. Now the original bluetooth example works. I guess that also my app will work too, being the problem the same. Finally I post the modified example, for the benefit of other people. Thanks a lot. Have a nice day.
 

Attachments

  • Bluetooth_31.zip
    12.7 KB · Views: 86
Upvote 0
Top