Android Question Does Turning Bluetooth need a permission?

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I thought turning Bluetooth On need a permission, but on my app running on Android 15, it did not ask for permission at all. Run it via Android Studio emulator, caused don't have device with Android 15.

Here are sample of codes
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private Tooth As Toggle
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("lyInp")
    Tooth.Initialize
End Sub

Sub B4XPage_Appear
    If Tooth.Bluetooth = False Then
        ToastMessageShow("Turn Blutooth on", True)
        Tooth.TurnBluetoothOn
    End If
End Sub

And here is the manifest
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
AddPermission(android.permission.READ_EXTERNAL_STORAGE)
SetApplicationAttribute(android:usesCleartextTraffic, "true")

AddApplicationText(<meta-data
    android:name="com.google.android.gms.vision.DEPENDENCIES"
    android:value="barcode,,face" />
)

AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)
CreateResource(xml, provider_paths,
   <files-path name="name" path="shared" />
)
AddPermission(android.permission.REQUEST_INSTALL_PACKAGES)

If Bluetooth is off, it will turn on automatically. I have device with Andro 10, app will asked permission to Turn On Bluetooth.
So, it is true, that on Android 15 and may be up or low, no need for a permission to turn on Bluetooth?
 

incendio

Well-Known Member
Licensed User
Longtime User
You cannot change the Bluetooth state on Android 13+ with targetSdkVersion >= 33.

As you are using a low targetSdkVersion (which will not be accepted to Google Play) you can still use this API and the library probably adds the BLUETOOTH_ADMIN which isn't a runtime permission.
Thanks for your clarification 👍
 
Upvote 0

ema01

Active Member
Licensed User
Longtime User
You cannot change the Bluetooth state on Android 13+ with targetSdkVersion >= 33.

As you are using a low targetSdkVersion (which will not be accepted to Google Play) you can still use this API and the library probably adds the BLUETOOTH_ADMIN which isn't a runtime permission.

Actually you can.
Well, you can't automatically as you could until sdk 32, but you can ask the user to turn it on with

B4X:
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

and
B4X:
Dim i As Intent
i.Initialize("android.bluetooth.adapter.action.REQUEST_ENABLE", "")
StartActivityForResult(i)

Anyway. As long as something changed, the android emulator doesn't have bluetooth. I remember it would even crash when calling the API
 
Upvote 0
Top