Android Question BLE - Heart Rate Monitor does not send data

grafsoft

Well-Known Member
Licensed User
Longtime User
I tried B4XPages, but now I get a PROBLEM WITH PERMISSIONS:

Î got this error:

B4X:
[TABLE]
[TR]
[TD]'*** Service (Starter) Create ***
'** Service (Starter) Start **
'** Activity (Main) Create, isFirst = True **
'puls init
'Call B4XPages.GetManager.LogEvents = True To enable logging B4XPages events.
'** Activity (Main) Resume **
'puls status
'StateChanged: 12
'puls scan
'Error occurred on line: 26 (HeartRateMonitor)
'java.lang.SecurityException: Need android.permission.BLUETOOTH_SCAN permission For AttributionSource { uid = 10359, packageName = b4a.example, attributionTag = Null, token = android.os.BinderProxy@d19f2b3, Next = Null }: GattService registerScanner
'    at com.android.bluetooth.Utils.checkPermissionForDataDelivery(Utils.java:901)
'    at com.android.bluetooth.Utils.checkScanPermissionForDataDelivery(Utils.java:960)
'    at com.android.bluetooth.gatt.GattService.registerScanner(GattService.java:3677)
'    at com.android.bluetooth.gatt.GattService$BluetoothGattBinder.registerScanner(GattService.java:945)
'    at com.android.bluetooth.gatt.GattService$BluetoothGattBinder.registerScanner(GattService.java:933)
'    at android.bluetooth.IBluetoothGatt$Stub.onTransact(IBluetoothGatt.java:293)
'    at android.os.Binder.execTransactInternal(Binder.java:1316)
'    at android.os.Binder.execTransact(Binder.java:1280)
[/TD]
[/TR]
[/TABLE]

I added this to the Manifest

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

Still the same

I know code like this

B4X:
Dim rp As RuntimePermissions
rp.CheckAndRequest(rp.PERMISSION_CAMERA)
Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
Log(Permission & " : " & Result)

but the values for Bluetooth are not constants of RuntimePermissions.

I have attached the little project.

@Erel: You say "If you are not using B4XPages then you are doing it wrong."

I do not see this warning when I create a new project, choosing "standard" or when I am using your class. Why? Why even allow to use (old or new) standard projects when it is wrong - without any error message?
 

Attachments

  • test.zip
    11.5 KB · Views: 152
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
I tried B4XPages, but now I get a PROBLEM WITH PERMISSIONS:

Î got this error:

B4X:
[TABLE]
[TR]
[TD]'*** Service (Starter) Create ***
'** Service (Starter) Start **
'** Activity (Main) Create, isFirst = True **
'puls init
'Call B4XPages.GetManager.LogEvents = True To enable logging B4XPages events.
'** Activity (Main) Resume **
'puls status
'StateChanged: 12
'puls scan
'Error occurred on line: 26 (HeartRateMonitor)
'java.lang.SecurityException: Need android.permission.BLUETOOTH_SCAN permission For AttributionSource { uid = 10359, packageName = b4a.example, attributionTag = Null, token = android.os.BinderProxy@d19f2b3, Next = Null }: GattService registerScanner
'    at com.android.bluetooth.Utils.checkPermissionForDataDelivery(Utils.java:901)
'    at com.android.bluetooth.Utils.checkScanPermissionForDataDelivery(Utils.java:960)
'    at com.android.bluetooth.gatt.GattService.registerScanner(GattService.java:3677)
'    at com.android.bluetooth.gatt.GattService$BluetoothGattBinder.registerScanner(GattService.java:945)
'    at com.android.bluetooth.gatt.GattService$BluetoothGattBinder.registerScanner(GattService.java:933)
'    at android.bluetooth.IBluetoothGatt$Stub.onTransact(IBluetoothGatt.java:293)
'    at android.os.Binder.execTransactInternal(Binder.java:1316)
'    at android.os.Binder.execTransact(Binder.java:1280)
[/TD]
[/TR]
[/TABLE]

I added this to the Manifest

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

Still the same

I know code like this

B4X:
Dim rp As RuntimePermissions
rp.CheckAndRequest(rp.PERMISSION_CAMERA)
Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
Log(Permission & " : " & Result)

but the values for Bluetooth are not constants of RuntimePermissions.

I have attached the little project.

@Erel: You say "If you are not using B4XPages then you are doing it wrong."

I do not see this warning when I create a new project, choosing "standard" or when I am using your class. Why? Why even allow to use (old or new) standard projects when it is wrong - without any error message?
For new projects it is better to work using B4XPages (old examples are having less support, but still will work), the current example BLEPages isn't updated, but basically you have to add:

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="31"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.
AddPermission(android.permission.ACCESS_FINE_LOCATION)

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

BLEPages: When you are connected read and post the logs here..
 
Last edited:
Upvote 0

grafsoft

Well-Known Member
Licensed User
Longtime User
Thank you! Bit since the BluetoothChat example also cannot find my device, I'll deal with that later when I succeeded in getting something out of this example. Only then I can find out which errors I am making.
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Thank you! Bit since the BluetoothChat example also cannot find my device, I'll deal with that later when I succeeded in getting something out of this example. Only then I can find out which errors I am making.
Follow those steps:
1. use nRF App from Nordic Semiconductor and post a screenshot when you are connected to your fitness band. it will help to add a filter in you B4X's App
2. use the B4Xpages example (just add those lines in the manifest) and copy as text the logs here.
 
Upvote 0

grafsoft

Well-Known Member
Licensed User
Longtime User
NRF says "no devices found" and "no bonded devices". The Xiaomi software works.

Thanks for your support :)
 
Upvote 0

emexes

Expert
Licensed User
NRF says "no devices found"

Do you have any other BLE devices you can verify the remainder of the system with?

eg these el-cheapo tags (I was buying them for like $2 before covid but I knew that price was too good to last ?)

https://www.ebay.com.au/itm/195283250116
https://www.ebay.com.au/itm/354247288548

1670330081884.png
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
NRF says "no devices found" and "no bonded devices". The Xiaomi software works.

Thanks for your support :)
Make sure your band can be "found", check the setting.

and carry out a power reset in both, smartphone and band.
 
Upvote 0

grafsoft

Well-Known Member
Licensed User
Longtime User
Thank you, I should have thought of that myself.

The logs still show that the device is not found:

Logger verbunden mit: samsung SM-A725F
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
puls init
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
puls status
StateChanged: 12
puls scan
** Activity (main) Pause event (activity is not paused). **
** Service (starter) Destroy (ignored)**



1670365420469.png
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Thank you, I should have thought of that myself.
What were the issue?

The logs still show that the device is not found:
Are you still connected to your band using nRF App or the brand App? if yes, make sure they are not running..

Note: please post small pictures, hard to read your post!

Next step:
1. add a filter:
2. Add a log
B4X:
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData) 'ignore
    If Id = "44:27:F3:25:FE:09" Then
    'If Name = "REDMI BAND PRO FE09" Then
        ConnectedName = Name
        Log ("test...: "&ConnectedName)
        manager.StopScan
        Log("connecting")
            #if B4A
        manager.Connect2(Id, False) 'disabling auto connect can make the connection quicker
        #else if B4I
        manager.Connect(Id)
        #end if
    End If
End Sub

3. post the full logs here as text.
 
Last edited:
Upvote 0

grafsoft

Well-Known Member
Licensed User
Longtime User
Everything restarted, brand app stopped, Manager_DeviceFound updated, but this event is never called.

Here are the logs:

Logger verbunden mit: samsung SM-A725F
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
puls init
*** mainpage: B4XPage_Appear
** Activity (main) Resume **
puls status
StateChanged: 12
puls scan
** Activity (main) Pause event (activity is not paused). **
*** mainpage: B4XPage_Disappear [mainpage]
** Service (starter) Destroy (ignored)**
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
puls init
*** mainpage: B4XPage_Appear
** Activity (main) Resume **
puls status
StateChanged: 12
puls scan
** Activity (main) Pause event (activity is not paused). **
*** mainpage: B4XPage_Disappear [mainpage]
** Activity (main) Pause event (activity is not paused). **
*** mainpage: B4XPage_Disappear [mainpage]
Logger verbunden mit: samsung SM-A725F
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
puls init
*** mainpage: B4XPage_Appear
** Activity (main) Resume **
puls status
StateChanged: 12
puls scan
** Activity (main) Pause event (activity is not paused). **
*** mainpage: B4XPage_Disappear [mainpage]
** Service (starter) Destroy (ignored)**
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
puls init
*** mainpage: B4XPage_Appear
** Activity (main) Resume **
puls status
StateChanged: 12
puls scan
** Activity (main) Pause event (activity is not paused). **
*** mainpage: B4XPage_Disappear [mainpage]
** Activity (main) Pause event (activity is not paused). **
*** mainpage: B4XPage_Disappear [mainpage]
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
puls init
*** mainpage: B4XPage_Appear
** Activity (main) Resume **
puls status
StateChanged: 12
puls scan
 
Upvote 0

emexes

Expert
Licensed User
B4X:
If Id = "44:27:F3:25:FE:09" Then
'If Name = "REDMI BAND PRO FE09" Then

This tip is more pragmatic than professional, but...

when I'm in the early days of hooking up to a BLE device, .Contains gets a bit of a workout, eg:

B4X:
'''If Id.Contains("FE:09") Then
If Name.ToLowerCase.Contains("redmi") Then

because I've been stung too many times by unexpected tabs or space or Chr(0) padding.
 
Upvote 0

emexes

Expert
Licensed User
And I'm starting to wonder if they have implemented some anti-hacker roadblock (or at least: speedbump) by not emitting advertising packets, ie same as you can switch off WiFi SSID advertising, so that only people or apps that specifically know the device can hook up to it.
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
And I'm starting to wonder if they have implemented some anti-hacker roadblock (or at least: speedbump) by not emitting advertising packets, ie same as you can switch off WiFi SSID advertising, so that only people or apps that specifically know the device can hook up to it.
We have here a band Xiaomi, If I remember correctly, before to connect to it you have to permit it in settings. Will have a check today later..

Edit:
@emexes ahah don't call that "anti-hacker roadblock" ahaha;) it is just the settings!!
 
Last edited:
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
yes, tested. I can connect the BLEPages example to that Xiaomi's Band.

B4X:
Connected
** Activity (main) Pause event (activity is not paused). **
Disconnected
** Activity (main) Resume **
** Activity (main) Pause event (activity is not paused). **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Found: Mi Smart Band, EE:32:9C:09:D4:D1, RSSI = -53, (MyMap) {1=[B@da0995d, -1=[B@709d2d2, 9=[B@dd6efa3, 2=[B@c99e2a0, 22=[B@8526e59, 0=[B@a8e4a1e}
connecting
Discovering services.
Connected
00001530-0000-3512-2118-0009af100700
(MyMap) {00001531-0000-3512-2118-0009af100700=[B@9eb7ef6, 00001532-0000-3512-2118-0009af100700=[B@23832f7}
00001530-0000-3512-2118-0009af100700
(MyMap) {00001531-0000-3512-2118-0009af100700=[B@9eb7ef6, 00001532-0000-3512-2118-0009af100700=[B@23832f7}
00001802-0000-1000-8000-00805f9b34fb
(MyMap) {00002a06-0000-1000-8000-00805f9b34fb=[B@37f2564}
00001800-0000-1000-8000-00805f9b34fb
(MyMap) {00002a00-0000-1000-8000-00805f9b34fb=[B@c89482, 00002a01-0000-1000-8000-00805f9b34fb=[B@1330793, 00002a04-0000-1000-8000-00805f9b34fb=[B@dbb33d0}
00001800-0000-1000-8000-00805f9b34fb
(MyMap) {00002a00-0000-1000-8000-00805f9b34fb=[B@c89482, 00002a01-0000-1000-8000-00805f9b34fb=[B@1330793, 00002a04-0000-1000-8000-00805f9b34fb=[B@dbb33d0}
00001800-0000-1000-8000-00805f9b34fb
(MyMap) {00002a00-0000-1000-8000-00805f9b34fb=[B@c89482, 00002a01-0000-1000-8000-00805f9b34fb=[B@1330793, 00002a04-0000-1000-8000-00805f9b34fb=[B@dbb33d0}
0000180a-0000-1000-8000-00805f9b34fb
(MyMap) {00002a25-0000-1000-8000-00805f9b34fb=[B@55e9fc9, 00002a27-0000-1000-8000-00805f9b34fb=[B@c6f76ce, 00002a28-0000-1000-8000-00805f9b34fb=[B@2fc89ef, 00002a23-0000-1000-8000-00805f9b34fb=[B@567bcfc, 00002a50-0000-1000-8000-00805f9b34fb=[B@fa86f85, 00000014-0000-3512-2118-0009af100700=[B@29031da}
0000180a-0000-1000-8000-00805f9b34fb

@grafsoft Have you done this ?
you should follow those step. It will work.
 
Last edited:
Upvote 0

grafsoft

Well-Known Member
Licensed User
Longtime User
"Make sure your band can be "found", check the setting."

I checked settings - found nothing. In "Settings - System" I can only restart the device or reset to factory settings. I did both - noch change in the logs.

The other menu items are irrelevant.
 
Upvote 0
Top