Page 1 of 1

Multiple BLE Services Example

Posted: Sun Jan 17, 2021 9:58 pm
by Simpler1
When creating a Bluetooth Low Energy server, what is the best way to create it with multiple services?

I want to have the following services:
  • Files (to check the number of files and possibly transfer files)
    Time (so that I can read and write)
    Voltage
    Proximity (rssi)
    Temperature
All of the examples in the examples/bluetooth directory create a class for the service, but none of the examples use multiple services.
I think it would be best to create a class for each service, but the problem is that each time BLE().gatts_register_services is called, it overwrites the previous values.

Does anyone have an example or template to follow?

Thanks

Re: Multiple BLE Services Example

Posted: Mon Jan 18, 2021 4:45 am
by jimmo
Simpler1 wrote:
Sun Jan 17, 2021 9:58 pm
Does anyone have an example or template to follow?
More than the register services, the main issue was sharing the IRQ handler. So I found it easier to have a single class manage the service registration and irq, which would dispatch to individual handler classes.

So something like:

Code: Select all

ble = ...
files = FileService()
time = TimeService()
voltage = VoltageService()
proximity = ProximityService()
temperature = TemperatureService()
manager = BleManager(ble, (files, time, voltage, proximity, temperature,))
then BleManager can ask each service for its tuple (and pass them all together to gatts_register_services) and tell them what their value handles are.

Re: Multiple BLE Services Example

Posted: Mon Jan 18, 2021 4:51 am
by jimmo
FWIW, I'm working on a much easier to use BLE API based on asyncio that I will try and get merged upstream...eventually.

See demo here:
https://github.com/jimmo/micropython/co ... 33dc238a4b

We need to get some other things fixed in asyncio first, which is why I haven't gotten around to finishing this off.

Re: Multiple BLE Services Example

Posted: Mon Jan 18, 2021 1:49 pm
by Simpler1
Thanks for your feedback jimmo. It helps a lot.