Android Question [SOLVED] Android 14 bluetooth acces

TomKluz

Active Member
Licensed User
Hi. My app don't want to scan bluetooth devices since Android 14 appears. In logs I am getting this:

*** Service (starter) Create ***
Start Starter
SDK werja: 34
FirstStart: true
Error occurred on line: 374 (Starter)
java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for android.content.AttributionSource@cf91748b: AdapterService getBondedDevices
at com.android.bluetooth.Utils.checkPermissionForDataDelivery(Utils.java:554)
at com.android.bluetooth.Utils.checkConnectPermissionForDataDelivery(Utils.java:586)
at com.android.bluetooth.btservice.AdapterService$AdapterServiceBinder.getBondedDevices(AdapterService.java:2429)
at com.android.bluetooth.btservice.AdapterService$AdapterServiceBinder.getBondedDevices(AdapterService.java:2420)
at android.bluetooth.IBluetooth$Stub.onTransact(IBluetooth.java:1064)
at android.os.Binder.execTransactInternal(Binder.java:1344)
at android.os.Binder.execTransact(Binder.java:1275)


I have add this permission (Bluetooth_Connect) in manifest editor.
I am using only Bluetooth Clasic and not using B4Xpages so far.
Is it possible to overtake this problem ?
Thank you for any help.
Tomasz
 

DonManfred

Expert
Licensed User
Longtime User
I have add this permission (Bluetooth_Connect) in manifest editor.
That´s not all you need.

Check this example

 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
I have these in manifest
B4X:
'Permissions--------------------------------------------------------------------------------
AddPermission(android.permission.BLUETOOTH_ADMIN)
AddPermission(android.permission.ACCESS_COARSE_LOCATION)
AddPermission(android.permission.ACCESS_FINE_LOCATION)
 
AddPermission(android.permission.BLUETOOTH_ADVERTISE) 'api 31
AddPermission(android.permission.BLUETOOTH_CONNECT)      'api 31
AddPermission(android.permission.BLUETOOTH_SCAN)      'api 31


I deal with bluetooth and wifi and I add these in runtime.
B4X:
    Dim ph As Phone
    If ph.SdkVersion >= 23 Then
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_COARSE_LOCATION)
    End If
    
    'Bluetooth
    If ph.SdkVersion >= 31 Then
        For Each Permission As String In Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT")
            Starter.rp.CheckAndRequest(Permission)
        Next
    End If
    
    'Wifi
    If ph.SdkVersion >= 33 Then
        For Each Permission As String In Array("android.permission.NEARBY_WIFI_DEVICES")
            Starter.rp.CheckAndRequest(Permission)
        Next
    End If
 
Upvote 0

TomKluz

Active Member
Licensed User
I deal with bluetooth and wifi and I add these in runtime.
I have placed serial connection (by Bluetooth) in Starter module.
Due to your advice I have put your code in. There is only rp.CheckAndRequest (Permission) not Starter.rp.CheckAndRequest (Permission) because it is inside Starter.
Sub Service_Create

Log("Start Starter")

Dim ph As Phone
Log ("SDK wersja: " & ph.SdkVersion)

'Bluetooth
If ph.SdkVersion >= 31 Then
For Each Permission As String In Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT")
rp.CheckAndRequest(Permission) (this is line 73)
Next
End If

but unfortunatelly I am geting foult in logs:

Logger połączony z: motorola moto g04
--------- beginning of main
*** Service (starter) Create ***
Start Starter
SDK werja: 34
Error occurred on line: 73 (Starter)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference
at anywheresoftware.b4a.objects.RuntimePermissions.CheckAndRequest(RuntimePermissions.java:93)
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 GeoLux.Wydruk_5.starter.onCreate(starter.java:56)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4765)
at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2356)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8385)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:640)

Do you use your serial connection in starter module ?
What is it 'null object reference' in my case ?

Sorry for this questions but I have got lost at all.
 
Last edited:
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
I have placed serial connection (by Bluetooth) in Starter module.
Due to your advice I have put your code in. There is only rp.CheckAndRequest (Permission) not Starter.rp.CheckAndRequest (Permission) because it is inside Starter.


but unfortunatelly I am geting foult in logs:



Do you use your serial connection in starter module ?
What is it 'null object reference' in my case ?

Sorry for this questions but I have got lost at all.
Bluetooth permission request cannot be used in Starter or service. Put it in Activity or B4xPage.

Yes, my serial is in service, but not in starter.

If you just started your project, switch to b4xpage. Service can get killed by OS when its in the background. B4xpage does not get killed.

Follow the 2nd post bluetooth example.
 
Last edited:
Upvote 0

TomKluz

Active Member
Licensed User
If you just started your project, switch to b4xpage. Service can get killed by OS when its in the background. B4xpage does not get killed.
Thank's for reply. Unfortunatelly my app is an older, quite big (for me) project. Has been working well till now (means Android 14).
Serial connection in starter was a smart idea in my opinion. I don't know the explanation why bluetooth permission cannot be used there.
On the other hand I am not interested to act in background.
Seems to be that I have to remake everything, using B4Xpages or move serial back to activity . Big work.
Thank you again.
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Seems to be that I have to remake everything, using B4Xpages or move serial back to activity . Big work.
I don't think you have to do it now, especially if it is a big task.

I have many different Bluetooth devices. Tell me which one you are using, and if I have the same module, I can give it a try.

It would be better if you could create a basic demo, just for the Bluetooth connection, and we can test it for you.
 
Upvote 0

TomKluz

Active Member
Licensed User
I have many different Bluetooth devices. Tell me which one you are using, and if I have the same module, I can give it a try.
Hi. Everthing I am using I am trying to make it simple. I am using basic module HC 05 which is well known. It is working well. The problem is in Android, not with Bluetooth module. Your offer is really nice. I was looking for another BT modules but mainly to have better range like 40-50 m. Unfortunately I didn't found enything really better.
I will stop changing my 'big' app for now and start a simple demo to learn and understand what is going on.
Anyway it is interesting what do you think about possibility to repleace HC 05.
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Hi. Everthing I am using I am trying to make it simple. I am using basic module HC 05 which is well known. It is working well. The problem is in Android, not with Bluetooth module. Your offer is really nice. I was looking for another BT modules but mainly to have better range like 40-50 m. Unfortunately I didn't found enything really better.
I will stop changing my 'big' app for now and start a simple demo to learn and understand what is going on.
Anyway it is interesting what do you think about possibility to repleace HC 05.
I have an HC05 and will give it a try tomorrow. The range depends on your application. I use los chip of NordicSemiconductor nRF52840 and can achieve long distances—500+ meters between nRF modules, and in some cases up to 700 meters; then I change nRF52840 from long range to standard BLE and connect it to my app, In standard mode without an external antenna, the range is 10+ meters, and in some cases up to 25 meters..

edit:
The problem is in Android, not with Bluetooth module.
Agree, permissions - Android!
I simply use the BLE example from the forum, so it should work for you too. If you can create your own basic example, upload it, we can test it.
 
Last edited:
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
I can confirm this results for HC05. I simply estimate a range as 10 m and in most cases it is ok for me.


HC05 is a clasic bluetooth 2. Do you think that BLE example will work ?
I tested my HC06 using the example https://www.b4x.com/android/forum/t...hreads/hc-05-classic-bluetooth.66677/#content and this

and getting this error
(IOException) java.io.IOException: read failed, socket might closed or timeout, read ret: -1

I coudn't connect to my HC06 - later will do some debugging
 
Last edited:
Upvote 0

TomKluz

Active Member
Licensed User
and getting this error
(IOException) java.io.IOException: read failed, socket might closed or timeout, read ret: -1
Thank you for answer. Your foult is not related to Android permissions. I am getting it always then Android cannot connect with Bluetooth module.

I have also took a simple app based on the example using HC05.

After making a correction in manifest have add a code:

'Bluetooth
If ph.SdkVersion >= 31 Then
For Each Permission As String In Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT")
rp.CheckAndRequest(Permission)
Next
End If

before Bluetooth starting and in debug mode app is running.

Unfortunatelly after compiling in Release mode I am getting this foult in logs and phone screen:

java.lang.Exception: Sub activity_permissionresult was not found.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:227)
at anywheresoftware.b4a.BA$2.run(BA.java:395)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8385)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:640)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:982)
java.lang.Exception: Sub activity_permissionresult was not found.

Strange thing (in my opinion) is that app is working after confirming this error on the screen.

Do you have an idea what is the reason of this foult ?
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
Thank you for answer. Your foult is not related to Android permissions. I am getting it always then Android cannot connect with Bluetooth module.

I have also took a simple app based on the example using HC05.

After making a correction in manifest have add a code:

'Bluetooth
If ph.SdkVersion >= 31 Then
For Each Permission As String In Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT")
rp.CheckAndRequest(Permission)
Next
End If

before Bluetooth starting and in debug mode app is running.

Unfortunatelly after compiling in Release mode I am getting this foult in logs and phone screen:

java.lang.Exception: Sub activity_permissionresult was not found.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:227)
at anywheresoftware.b4a.BA$2.run(BA.java:395)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8385)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:640)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:982)
java.lang.Exception: Sub activity_permissionresult was not found.

Strange thing (in my opinion) is that app is working after confirming this error on the screen.

Do you have an idea what is the reason of this foult ?
Add this in your activity

B4X:
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    Log(Permission & " " & Result)
End Sub
 
Upvote 0

TomKluz

Active Member
Licensed User
Sub Activity_PermissionResult (Permission As String, Result As Boolean) Log(Permission & " " & Result) End Sub
Hi.
This advice was final. My small app starts to work, even in Release mode. Thank you very much.
Now I have to find a solution for a big app with Starter.
Your advice was to remove it, so I will go in this direction.
 
Upvote 0
Top