Reading BLE service in Appinventor

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

Wow, I didn't know this extension existed. But I also haven't seen any updates to this extension since I've been active here.

1 Like

Well I am looking for such an extension. There are many people wanting to do HR belt reading from what I am reading here and on net.

Many apps written for such but, in my opinion, are not any good at all.

Its all about reading bytes and mathematic deciphering it appears.