Android Question BlePeripheral & android 13

Pesciolina

Active Member
Licensed User
Longtime User
HI,

after updating Ide to 12.2 the app that uses BlePeripheral release 1.01 on android 13 crashes.
The previous version compiled with Ide 11.8 and sdk 30 works fine.
In the code I have inserted the following function with which it appears to have the permissions

unfortunately it was communicated to me by a friend who uses the app, I don't have a cell with android 13

Thanks for your help

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")
            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)
                'LogColor("Permiso Bluetooth aprobado",Colors.Blue)
                Return True
            End If
        Next
    Else If phone.SdkVersion <> 31 Then
        Log("Es diferente SdkVersion 31")
        Return True
    End If
End Sub

file Manifest
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.BLUETOOTH_ADVERTISE)
AddPermission(android.permission.BLUETOOTH_CONNECT)
AddPermission(android.permission.BLUETOOTH_SCAN)



B4X:
Sub BleManager_StateChanged (State As Int)
    Select State
        Case BleManager.STATE_POWERED_OFF
            CurrentStateTextBle = "POWERED OFF"
        Case BleManager.STATE_POWERED_ON
            CurrentStateTextBle = "POWERED ON"
        Case BleManager.STATE_UNSUPPORTED
            CurrentStateTextBle = "UNSUPPORTED"
            
    End Select
    
    If State <> BleManager.STATE_POWERED_ON Then
        ToastMessageShow("Please enable Bluetooth", True)
        NotAvailableBle = True
    Else
        BleReiPeripheral.Initialize("BleReiPeripheral", BleManager )

        If BleReiPeripheral.IsPeripheralSupported = False Then
            ToastMessageShow("Peripheral mode not supported.", True)
            NotAvailableBle = True
        Else
                
            BleReiPeripheral.Start(GetBluetoothName)
            
            Wait For BleReiPeripheral_Start (Success As Boolean)
            Log("Peripheral started successfully? " & Success)
            
        
        End If
    End If
    
    
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Pesciolina

Active Member
Licensed User
Longtime User
Hi Erel,
in the manifest file there are all the permissions,
the difference with the example is that I ask for the
authorization when starting the app and not when I activate the search
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetServiceAttribute(Starter, android:foregroundServiceType, "location")
'End of default text.

SetApplicationAttribute(android:theme, "@style/MyAppTheme")
'SetApplicationAttribute(android:requestLegacyExternalStorage, true)

CreateResource(values, theme.xml,
<resources>
    <style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#0098FF</item>
        <item name="colorPrimaryDark">#007CF5</item>
        <item name="colorAccent">#FF0000</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>
    <style name="ToolbarMenu" parent="Base.ThemeOverlay.AppCompat.ActionBar">
        <item name="android:textColorPrimary">#fff</item>
        <item name="android:colorBackground">#0098FF</item>
    </style>
</resources>
)

CreateResourceFromFile(Macro, Core.NetworkClearText)

AddPermission(android.permission.GET_ACCOUNTS)
AddPermission(android.permission.ACCESS_WIFI_STATE)
AddPermission(android.permission.ACCESS_FINE_LOCATION)

AddPermission("android.permission.ACCESS_COARSE_LOCATION")
AddPermission("android.permission.ACCESS_NETWORK_STATE")
AddPermission("android.permission.CHANGE_NETWORK_STATE")
AddPermission("android.permission.ACCESS_COARSE_UPDATES")
AddPermission("android.permission.READ_PHONE_STATE")

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



AddActivityText(main, <intent-filter>
    <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
 <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
</intent-filter>
    <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
        android:resource="@xml/device_filter" />
        )   
AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(SmsService,
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)

AddManifestText(
<uses-permission
  android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  android:maxSdkVersion="18" />
)
 
Upvote 0

Pesciolina

Active Member
Licensed User
Longtime User
It doesn't matter when you request the permissions as long as it happens before you try to discover the peripheral. The BLE example works on Android 13.
Good morning,

I finally recovered a device with Android 12 and was able to debug.
The authorization request works, as Erel wrote, but I have two other problems.
I use the BlePeripheral library and pass the name to this function which is deprecated, what can I use?
B4X:
Sub GetBluetoothName As String
    Dim jo As JavaObject
    Return jo.InitializeStatic("android.bluetooth.BluetoothAdapter").RunMethodJO("getDefaultAdapter", Null) _
     .RunMethod("getName", Null)
End Sub
B4X:
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at B4A.SendTime.starter._getbluetoothname(starter.java:12088)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    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)

For testing I tried to pass it a test name and I get this authorization error, what am I missing?

B4X:
Caused by: java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for android.content.AttributionSource@a8910bf1: getName
    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.IBluetoothManager$Stub$Proxy.getName(IBluetoothManager.java:818)
    at android.bluetooth.BluetoothAdapter.getName(BluetoothAdapter.java:1303)

Thanks for your collaboration
 
Upvote 0
Top