Multiple BLE Services Example

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Simpler1
Posts: 14
Joined: Wed Jan 06, 2021 6:30 pm

Multiple BLE Services Example

Post by Simpler1 » Sun Jan 17, 2021 9:58 pm

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

User avatar
jimmo
Posts: 2236
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Multiple BLE Services Example

Post by jimmo » Mon Jan 18, 2021 4:45 am

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.

User avatar
jimmo
Posts: 2236
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Multiple BLE Services Example

Post by jimmo » Mon Jan 18, 2021 4:51 am

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.

Simpler1
Posts: 14
Joined: Wed Jan 06, 2021 6:30 pm

Re: Multiple BLE Services Example

Post by Simpler1 » Mon Jan 18, 2021 1:49 pm

Thanks for your feedback jimmo. It helps a lot.

Post Reply