Reading BLE service in Appinventor

Maybe some of these blocks (with the new BLE ext) help:

and these are from your (working) HeartRateTest.aia:

I haven't worked on these BLE HeartRate apps for a long time, so I'm something out of the topic.

Hello again
Well, since I had to receive three data on the mobile, PPG (2 data) and ECG (1 data), I have not known how to use the GATT Service.
I have been recommended to do it through UART. I have managed to make a program that sends those three data. I receive them in a string of the form:
498, 496, 245,
I read them well from Serial Bluetooth Terminal, and in nRF Connect I see this:

I have seen many examples, of applications in App Inventor, to read this data on mobile. But there is a lot of information.
Can you help me choose a good example for this please?

With the UART the program from before works perfectly:

Now I need to separate the three data to process them separately. Any help please?

Hello again
The latter is also fixed. I put it here in case someone helps:

@ Anibal_de_la_Torre Did you use the new BLE and Heart-Rate extensions? Did you use "shorts" as data type? Would you mind posting the final "aia" file for your last two posts?

hi there @ABG - sorry I'm late responding, I've just posted some hopefully useful stuff here - I got involved with decoding live data from a Stryd power meter and a Polar heart rate monitor - the project got massive and since I got a running watch not looked at the code for months and months now. Anyway have a look at this and see if it helps : BLE GATT characteristic parsing
Cheers,
Martin

Hurrah, success. It's interesting to see that I can now follow a lot of the previous posts and the code now I've been through the loop myself.

There were lots of concepts I had to discover and figure out in going from BLE noob to getting it working.

First was trying to hack old programs was a real struggle as they didn't work. Watch out for lack of backwards compatibility with old code and the bluetooth extension. I got all sorts of weird errors about advertisements and failures that I couldn't figure out - old extensions.

Second was in trying to understand how bluetooth LE works. The basic are quite straightforward - once you know!!!

Essentially you have to start scanning for BT devices

component_method

Then you have an event handler for whatever it finds. Stick whatever it finds into a listpicker (or another suitable ui element) - a list is returned (that was a gotcha!)

Then select a device from the list. - You have to display the list of course (well it's of course now I figured it out!)

blocks (3)

![blocks

Okay now that you have a device (and don't forget that it may take a few moments for all sensors/devices to be recognised, then you can start interrogating the sensor (in my case a Garmin chest heart rate monitor)

You need two magic numbers aka
The service UUID


and the Characteristic UUID

These are used to get the data for the device.
Those numbers should work, but using a bluetooth sniffer such as nRF Connect from the Play Store (or similar e.g. BLE UUID Explorer or BLE Checker will be useful). There is an xml spec at bluetooth.com - heart rate service xml which may provide some help.

So from code, we now need to respond to the connected event and register a callback (event listener) for any data and also to enable reading the data

1 Like

Oh how annoying and frustrating that I cannot post more images or create more replies - you really don't help do you.

I've shown in these blocks how to get the information on what services and characteristics the sensor supports - but lot of info is sent so I used a text box to show it.

So there is the register a service and the readShorts - and that was another gotcha. You'd have thought it would send an integer value. But no, it sends a short (2 byte value 16 bits) (and it's unsigned because it's not a positive or negative number) and the big gotcha is that this data is in a list object and that the index for a list object starts at 1, not zero like just about every other programming language I've used (apart from dear old BASIC). It seems to me that getting just the first element will do the job - but I maybe wrong, I couldn't see any other elements in the list when I debugged it.

Anyway, when you've done these steps then you need an actual listener to respond to the data that is sent (arbitrarily/asynchronously?) from the sensor.

So what is the format of this short:

Well if I just output the short to a label I was getting a number like 23312 and similar.

In binary this is

0 1 0 1 1 0 1 1 0 0 0 1 0 0 0 0

Split this into a most significant byte and a least significant byte
0 1 0 1 1 0 1 1 and 0 0 0 1 0 0 0 0

Now look at the least significant bit at the far right. If this is a 0 then the heart rate is in the most significant byte (8 bits) and if a 1 then the data is 16 bits (and I don't know what you look at!

So 0 1 0 1 1 0 1 1 is 91 in decimal - a bit fast, but I was excited!!!

How get that most significant byte? In assembly language and up you would shift the short 2 bytes to the right by 8 bits, which is the same as dividing by 256 and losing any fraction - hence the format to 0 decimal places.

Anyhow the main code looks like the following and hacked from some code by Jose Luis Núñez (I think his app is on the Play Store but needs the newest BLE extension and some refactoring. It's also somewhere else as an aia but I can't remember where.)

Anyhow I hope this helps others who are taking their first steps. It took me four days of effort to get to this and it was really frustrating. But now it's done and I can move on to my speed and cadence sensors.

HeartRateMonitorGM.aia (314.5 KB)

1 Like

could you provide the app ?

Gordon - thank you for the code and explanations. I started playing with it, and it works fine when run in companion mode, but when I create an .apk and install it, the BLE scanning does not see any devices. Is this a known issue? Thanks for any insights. Dino

Found the problem - need a newer version of the BLE extension. Thanks to EW Patton, for uploading:
BLE-with-extensions-api.aix

I wish to pipe in and give a HUGE thank you to you Sir. Your extension takes all the work out of seeing and reading an HRM device. Although I could have spent a few weeks studying and experimenting, it is so much better (no not easier) to have a plug in !

Much controversy about whether it should be left to the developer to research and devise his own methods and thereby learn coding vs extensions to make (plugins) to perform same but I believe that is why MIT AI (and others) use block form creation. By offering methods to do such tasks and then creatively use devices and apps in new and better ways is future.

THANK YOU so much

1 Like

Just came across this Anke. Back in the days when I did long distance running (40 years ago), a regular, slow heart beat was thought to be an advantage :grin:

(added to FAQ)


The same is still true today.

HRV (heart rate variability) has been scientifically studied intensively for decades and has been confirmed as a health parameter.

I have used this extension nearly 2 years. With the new BLE for Android 12+ it does not seem to work :roll_eyes: Do you have an update?

I am looking for an updated version of HeartRateMonitor.aix that will work with the latest version of BLE for Android 12+

Evidently I was tired from coding and replied incorrectly .. sorry

What is HeartRateMonitor.aix? This topic is about the BLE extension.

hmmm I seem to be replying wrong ??

HeartRateMonitor.aix written by E.W.Patton in 2020