Android Question Request to turn on Bluetooth

red30

Well-Known Member
Licensed User
Longtime User
Apps that use Bluetooth require permission to start using it. How can I make such a request in B4A?
 

red30

Well-Known Member
Licensed User
Longtime User
I tried this example and got the following result.
If the bluetooth is turned off, then the application simply does not start with the following error:
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
b4xmainpage_initialize (java line: 628)
java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for AttributionSource { uid = 10294, packageName = b4a.example, attributionTag = null, token = android.os.BinderProxy@9811f0b, next = null }: enable
    at android.os.Parcel.createExceptionOrNull(Parcel.java:3023)
    at android.os.Parcel.createException(Parcel.java:3007)
    at android.os.Parcel.readException(Parcel.java:2990)
    at android.os.Parcel.readException(Parcel.java:2932)
    at android.bluetooth.IBluetoothManager$Stub$Proxy.enable(IBluetoothManager.java:724)
    at android.bluetooth.BluetoothAdapter.enable(BluetoothAdapter.java:2436)
    at anywheresoftware.b4a.objects.Serial$BluetoothAdmin.Enable(Serial.java:392)
    at b4a.example.b4xmainpage._initialize(b4xmainpage.java:628)
    at b4a.example.b4xpagesmanager._initialize(b4xpagesmanager.java:708)
    at b4a.example.main._activity_create(main.java:367)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at b4a.example.main.afterFirstLayout(main.java:105)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:83)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8757)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Caused by: android.os.RemoteException: Remote stack trace:
    at com.android.server.bluetooth.BluetoothManagerService.checkPermissionForDataDelivery(BluetoothManagerService.java:5558)
    at com.android.server.bluetooth.BluetoothManagerService.checkConnectPermissionForDataDelivery(BluetoothManagerService.java:5576)
    at com.android.server.bluetooth.BluetoothManagerService.checkBluetoothPermissions(BluetoothManagerService.java:1865)
    at com.android.server.bluetooth.BluetoothManagerService.enable(BluetoothManagerService.java:2201)
    at android.bluetooth.IBluetoothManager$Stub.onTransact(IBluetoothManager.java:275)
If the bluetooth is turned on, then the application starts and works fine.
And I would like the application to always start and when you press any button, ask for permission to turn on the bluetooth. So that the user does not have to turn it on each time. So that you can turn it on when requested by the application.
Note: test was on Galaxy Tab A8 Android 13
 
Last edited:
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I also noticed that if you set targetSdkVersion=33, when this code to enable the Bluetooth module stops working. I always get BTadmin.Enable = False.
B4X:
If BleMan.State = BleMan.STATE_POWERED_OFF Then
    BTadmin.Initialize("BTadmin")
    If BTadmin.IsEnabled = False Then
        If BTadmin.Enable = False Then
            ToastMessageShow(Main.loc.Localize("Bluetooth enable error"), True)
        Else
            ToastMessageShow(Main.loc.Localize("Enabling Bluetooth..."), False)
        End If
    End If
End If
How can I turn on bluetooth in Sdk version 33? I don't want to force the user to turn on the bluetooth manually every time.
Note: I also noticed that on version 33, if I use classic bluetooth (not BLE) to start the device search process, now, just like in BLE, geolocation is required.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I tested this version on the same Galaxy Tab A8 Android 13 (targetSdkVersion="33"). I get the error "Error starting discovery process.", even though I agree to all permission requests.
I wanted to create a new topic "Bluetooth and BLE usage in SDK 33". In the morning I tested SDK 33 and work with different versions of android and realized that now using sdk 33, even for classic bluetooth (not BLE), geolocation is now needed. I don't know why this is happening, but now it is.
Maybe it makes sense to write some example of how to make it work correctly on most devices with Bluetooth and BLE in the current conditions and make this work convenient for the end user... To provide for all options that the user can click....
I see it this way. In the worst case scenario, we do not have more than one permission, bluetooth and geolocation are turned off. When you press the button, first I think a dialog should appear to enable the bluetooth module. If the user forbids the inclusion (on purpose or accidentally), then if possible, ask again (if not the restrictions of andoid itself). Then there is a whole set of permissions depending on the version of the phone sdk. To be honest, I'm already confused about what and when to request ... I want to somehow systematize it...

I used to have this:
For classic bluetooth you need:
1) Turn on bluetooth
B4X:
If BleMan.State = BleMan.STATE_POWERED_OFF Then
    BTadmin.Initialize("BTadmin")
    If BTadmin.IsEnabled = False Then
        If BTadmin.Enable = False Then
            ToastMessageShow(Main.loc.Localize("Bluetooth enable error"), True)
        Else
            ToastMessageShow(Main.loc.Localize("Enabling Bluetooth..."), False)
        End If
    End If
End If
2) request "PERMISSION_ACCESS_COARSE_LOCATION" and that's it. If the user does not give this permission, display a message that it must be given in the settings for it to work. This worked for all my devices.

For BLE you need:
1) Also turn on bluetooth
2) Next I checked sdk 22 and requested to enable GPS (Phone1.SdkVersion>22 And GPS1.GPSEnabled = False). If the user does not turn on GPS, display a message that it must be given in the settings for it to work.
3) Further, if GPS is enabled, it was necessary to check RunPer.Check(RunPer.PERMISSION_ACCESS_FINE_LOCATION) = False. Next, I requested this permission, and also if the user does not give this permission, display a message that for work it is necessary to give it in the settings. This worked for all my devices.

But now, by changing the sdk version, everything has become much more complicated ...
 
Upvote 0
Top