Android Question error BlePeripheral e Android 13

Pesciolina

Active Member
Licensed User
HI,

I am again asking for info on the BlePeripheral library as despite giving all the permissions I get this error

B4X:
Sub GetPermission_Bluetooth As ResumableSub
    Dim phone As Phone
    If phone.SdkVersion >= 31 Then
        For Each Permission As String In Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT", "android.permission.BLUETOOTH_ADVERTISE", "android.permission.BLUETOOTH_PRIVILEGED", rp.PERMISSION_ACCESS_FINE_LOCATION)
            Sleep(0)
            Starter.rp.CheckAndRequest(Permission)
            Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
            If Result = False Then
                ToastMessageShow("No permission...", True )
                Return False
            Else
                ToastMessageShow("OK permission...", True)
               
                Return True
            End If
        Next
    Else If phone.SdkVersion <> 31 Then
        Log("Es diferente SdkVersion 31")
        Return True
    End If
End Sub

B4X:
java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for android.content.AttributionSource@f21252: AdapterService setName
    at android.os.Parcel.createExceptionOrNull(Parcel.java:2456)
    at android.os.Parcel.createException(Parcel.java:2440)
    at android.os.Parcel.readException(Parcel.java:2423)
    at android.os.Parcel.readException(Parcel.java:2365)
    at android.bluetooth.IBluetooth$Stub$Proxy.setName(IBluetooth.java:2709)
    at android.bluetooth.BluetoothAdapter.setName(BluetoothAdapter.java:1403)
    at anywheresoftware.b4a.objects.BlePeripheralRei.Start2(BlePeripheralRei.java:123)
    at anywheresoftware.b4a.objects.BlePeripheralRei.Start(BlePeripheralRei.java:116)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.objects.BleManager2$1$1.run(BleManager2.java:105)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7900)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:568)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1045)
in the manifest file i added these commands
B4X:
AddManifestText(
<uses-permission
  android:name="android.permission.BLUETOOTH"
  android:maxSdkVersion="30" />
<uses-feature
 android:name="android.hardware.bluetooth" android:required="false"/>  
)  

AddManifestText(
<uses-permission
  android:name="android.permission.BLUETOOTH_ADMIN"
  android:maxSdkVersion="30" />
 )  


AddPermission("android.permission.BLUETOOTH_CONNECT")
AddPermission("android.permission.BLUETOOTH_ADVERTISE")
AddPermission(android.permission.BLUETOOTH_SCAN)
AddPermission(android.permission.BLUETOOTH_PRIVILEGED)

thanks for the precious collaboration
 

Pesciolina

Active Member
Licensed User
I understood the mistake, thanks Erel that you opened my eyes.
Basically the permissions routine was wrong

B4X:
Sub GetPermission_Bluetooth As ResumableSub

Dim Permissions As List
Dim phone As Phone
If phone.SdkVersion >= 31 Then
        Permissions = Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT", "android.permission.BLUETOOTH_ADVERTISE", 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)
        Log(Permission &  ": " & Result)
    If Result = False Then
        ToastMessageShow("No permission: " & Permission, True)
        Return False
    Else
       Return True
    End If
Next

End Sub

B4X:
android.permission.BLUETOOTH_SCAN: true
android.permission.BLUETOOTH_CONNECT: true
android.permission.BLUETOOTH_ADVERTISE: true
android.permission.ACCESS_FINE_LOCATION: true
 
Upvote 0
Top