Android Question [B4X] BLE 2 - Bluetooth Low Energy

Nicolás Cieri

Active Member
Licensed User
Hi, I have an app that works fine on many devices, but not on some.

To test, on one of the devices that doesn't work, try the Erel demo, BLEExample - B4a.

It's a Redmi A1.

It does the process as if it were scanning devices, but it never finds anything, that is, it never enters this part. "Manager_DeviceFound"



In the log, it doesn't show me any error.
 

Nicolás Cieri

Active Member
Licensed User
The first difference between other smartphones, I get this error on Log...

(SecurityException) java.lang.SecurityException: Need android.permission.BLUETOOTH_SCAN permission for android.content.AttributionSource@edf38225: GattService registerScanner

So, I add the permission on manifiest and by rutine..

rp.CheckAndRequest("android.permission.BLUETOOTH_SCAN")
Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
If Result = False Then
ToastMessageShow("No permission", True)
Return
End If

So, now I have the permission, no error.. but I can't see any device found in the sub "Manager_DeviceFound".


On high-end devices it seems to work fine, but on many it doesn't, it never goes into this "Manager_DeviceFound".


Thanks.
 
Upvote 0

Nicolás Cieri

Active Member
Licensed User
Hello Erel,

it stays like scanning, it's the same problem, it's not a permissions problem. The sub 'Manager_DeviceFound' never fires.

No error is shown, it's like it didn't find any Bluetooth devices, although there are many around and it works perfectly on other devices.
 
Upvote 0

Nicolás Cieri

Active Member
Licensed User
Maybe the device is not compatible because it is low-end?

But it's new, I bought it for testing because it has happened to me with various devices.

It's a Redmi A1.
 
Upvote 0

Nicolás Cieri

Active Member
Licensed User
The bluetooth of the phone works, because if I search for bluetooth devices from the phone settings if I can see all the ones it finds.

Only from the B4A apps, I can't see any, as if it didn't find any device.
 
Upvote 0

Nicolás Cieri

Active Member
Licensed User
I have solved it, now it Works.

https://source.android.com/docs/core/connect/bluetooth/ble

Is there a way to detect when location is off??????, turning this on Works.

Location scanning​

A device's location services can use Bluetooth to detect Bluetooth beacons and provide a more accurate location. This is how users enable the feature:

  1. Go to Settings > Security & Location > Location.
  2. Check that the Location On/Off toggle is in the On position.
  3. Go to Mode.
  4. Select either High accuracy or Battery saving mode.
 
Upvote 0

Nicolás Cieri

Active Member
Licensed User
No, it's not covered in the "Demo", because even if it says if I allow it, it doesn't really change this permission.

I had to change this permission manually in the phone settings.
 
Upvote 0

emexes

Expert
Licensed User
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
it's always good idea start any BLE project using nRF App. That app will tell you a lot information needed to create a B4X App.

For example: if you start nRF App it will tell you to turn ON location, Bluetooth on. etc. also it's good to make sure the hardware is working properly.
 
Upvote 0

emexes

Expert
Licensed User
It might be useful to check how other apps, eg nRF Conect, handle the same situation

And the answer to that is:

1672888845467.png
 
Upvote 0

emexes

Expert
Licensed User
or ask the user to activate it, from the application?

This tutorial post:

https://www.b4x.com/android/forum/threads/gps-tutorial.6592/

contains example code:

B4X:
StartActivity(Starter.GPS1.LocationSettingsIntent)     'Will open the relevant settings screen.

which sounds like it's in the right area as we're looking for too.

Also this thread https://www.b4x.com/android/forum/threads/gps-and-quick-settings-screen.38106/ albeit that is 8 years ago from the days of steam-powered smart phones 🤔 and plenty may have changed since then.
 
Upvote 0
Top