adafruit_ble library - Characteristics and Services info

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
frangerhawer
 
Posts: 4
Joined: Thu Jun 30, 2022 6:24 am

adafruit_ble library - Characteristics and Services info

Post by frangerhawer »

Hello, I read through multiple adafruit tutorials, but I cannot find a list of possible GATT Services and how to import them using Circuitpython.
Background:
I am am using Adafruit Feather nRF52840 Sense and I want to create perypethial BLE device - cycling power sensor in particular. I want to advertise Cycling Power Service with its mandatory characteristics.
I found CPS (Cycling Power Service) Service here: https://www.bluetooth.com/specifications/specs/
I know which characteristics are requied, but I cannot find Docs on how to implement them using Circuitpython. I found this: https://docs.circuitpython.org/projects/ble/en/latest/ but there is not much info.
I foun how it can be done using Arduino C:

Code: Select all

BLEService CyclePowerService("1818");
BLECharacteristic CyclePowerFeature("2A65", BLERead, 4);
BLECharacteristic CyclePowerMeasurement("2A63", BLERead | BLENotify, 8);
BLECharacteristic CyclePowerSensorLocation("2A5D", BLERead, 1);
BLE.setDeviceName(BLE_DEVICE_NAME);
BLE.setLocalName(BLE_LOCAL_NAME);
BLE.setAdvertisedService(CyclePowerService);
CyclePowerService.addCharacteristic(CyclePowerFeature);
CyclePowerService.addCharacteristic(CyclePowerMeasurement);
CyclePowerService.addCharacteristic(CyclePowerSensorLocation);

BLE.addService(CyclePowerService);
Code taken from: https://teaandtechtime.com/arduino-ble- ... r-service/
I want to replicate this tutorial using Adafruit feather and Circuitpython

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: adafruit_ble library - Characteristics and Services info

Post by tannewt »

This is definitely a place where we're missing good documentation. That's my fault because I didn't take the time to do it.

The CircuitPython model of BLE Services and Characteristics is very declarative. That means you define a Service class with Characteristic members. It isn't explicitly adding characteristics to a service using a function. The reason we did this style was so that the class could be used both in the client and server roles. In CP you also provide the service class to add to the ProvideServicesAdvertisement.

The best place to see examples of this is in the BLE libraries examples: https://github.com/adafruit/Adafruit_Ci ... n/examples

In particular the `ble_json_` and `ble_packet_buffer` files show defining a custom service and then using it.

User avatar
frangerhawer
 
Posts: 4
Joined: Thu Jun 30, 2022 6:24 am

Re: adafruit_ble library - Characteristics and Services info

Post by frangerhawer »

Thank you for a fast reply! So there is no way to import the CPS service required by me from Nordic like it is done with UART:

Code: Select all

from adafruit_ble.services.nordic import UARTService  <-- change this to i.e. CyclePowerService
Thank you for directing me to the right example. The ble_packet_buffer_service.py looks promising, and I overlooked it when I was reading through the list myself.

On the other hand, I was thinking about switching from Circuitpython to Arduino on nRF52840 Sense, and using this Arduino library: https://www.arduino.cc/reference/en/lib ... rduinoble/
what do you think about this approach?

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: adafruit_ble library - Characteristics and Services info

Post by tannewt »

frangerhawer wrote:Thank you for a fast reply! So there is no way to import the CPS service required by me from Nordic like it is done with UART:

Code: Select all

from adafruit_ble.services.nordic import UARTService  <-- change this to i.e. CyclePowerService
I don't think this will work. I don't know of anywhere that has already defined the CyclePowerService.
frangerhawer wrote: On the other hand, I was thinking about switching from Circuitpython to Arduino on nRF52840 Sense, and using this Arduino library: https://www.arduino.cc/reference/en/lib ... rduinoble/
what do you think about this approach?
I don't use Arduino so I don't know.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”