This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Issues with characteristic values using the pc-ble-driver-py library

Hello!

I am doing some development with pc-ble-driver-py and ran in to some unexpected behaviours when reading characteristics. I have been modifying the nus_collector.py script that was given as an example on this post. I am also using a PCA10028 to scan for devices.

Firstly, when scanning for devices, they last few characters for some devices are missing or corrupted. For example, when receiving the advertising name of a device with the name of "TestDev", the received name is captured as follows:

TesÝgSt -> [84, 101, 115, 221, 103, 83, 116]
TesÝÝÝÝ -> [84, 101, 115, 221, 221, 221, 221]
TesÝÝÝÝ -> [84, 101, 115, 221, 221, 221, 221]
TesÝÝÝ  -> [84, 101, 115, 221, 221, 221, 0]
TesÝÝÝ  -> [84, 101, 115, 221, 221, 221, 0]
TesÝwar -> [84, 101, 115, 221, 119, 97, 114]
TesÝÝÝ5 -> [84, 101, 115, 221, 221, 221, 53]
TesÝÝÝÝ -> [84, 101, 115, 221, 221, 221, 221]
TesÝÝÝI -> [84, 101, 115, 221, 221, 221, 73]
TesÝÝÝÝ -> [84, 101, 115, 221, 221, 221, 221]
TesÝÝÝÝ -> [84, 101, 115, 221, 221, 221, 221]
TesÝÝÝÝ -> [84, 101, 115, 221, 221, 221, 221]

where the expected output should be:

TestDev -> [84, 101, 115, 116, 68, 101, 118]

Upon using nRF Connect (either PC or mobile versions), the correct device name is displayed:

   

This end of device name string corruption does not just happen for this particular device - I have tested scanning other devices where I see a similar behaviour with differing amounts of corruption (e.g. some devices only have 1 character that is corrupted at the end of the string). However, not all of the devices I have scanned display the corruption; some devices have their advertising names being received correctly.



Secondly, when receiving heart rate notifications from a connected device, the data is tens of thousands of values in length. Is there any documentation on how to convert this data so it can be interpreted in beats per minute (bpm)? Note: the heart rate is correctly displayed on nRF Connect (both PC and mobile).

Here is the modified nus_collector.py script that I have been using to test:

import sys
import time
import queue
import logging
from enum import Enum
from functools import wraps

CONN_IC_ID = 'NRF51'

from pc_ble_driver_py import config
config.__conn_ic_id__ = CONN_IC_ID
from pc_ble_driver_py.ble_driver import BLEUUID, BLEUUIDBase, BLEDriverObserver, BLEDriver, BLEEnableParams, BLEAdvData, BLEGapTimeoutSrc
from pc_ble_driver_py.ble_adapter import BLEAdapterObserver, BLEAdapter


TARGET_DEV_NAME = "TestDev"
TARGET_DEV_MAC_ADDRESS = 'DEA01D552902'
CONNECTIONS = 1

def init(conn_ic_id):
    global nrf_sd_ble_api_ver
    nrf_sd_ble_api_ver = config.sd_api_ver_get()

class NUSCollector(BLEDriverObserver, BLEAdapterObserver):

    def __init__(self, adapter):
        super(NUSCollector, self).__init__()
        self.adapter    = adapter
        self.conn_q     = queue.Queue()
        self.adapter.observer_register(self)
        self.adapter.driver.observer_register(self)
        global GFIT_CS_BASE_UUID, HRS_UUID, DEVICE_NAME_UUID, MANUFACTURER_NAME_UUID
        global FIRMWARE_REVISION_UUID, SOFTWARE_REVISION_UUID, FITNESS_MACHINE_FEATURES_UUID
        global FITNESS_MACHINE_STATUS_UUID, TREADMILL_DATA_UUID, CROSS_TRAINER_DATA_UUID
        global STEP_CLIMBER_DATA_UUID, ROWER_DATA_UUID, INDOOR_BIKE_DATA_UUID
        global SUPPORTED_INCLINATION_RANGE_UUID, SUPPORTED_RESISTANCE_LEVEL_RANGE_UUID
        global SUPPORTED_HEART_RATE_RANGE_UUID, SUPPORTED_POWER_RANGE_UUID
        global GFIT_CS_CONTROL_POINT_UUID, GFIT_CS_CUSTOM_DATA_UUID


        HRS_UUID = BLEUUID(BLEUUID.Standard.heart_rate)
        DEVICE_NAME_UUID = BLEUUID(0x2A00)
        MANUFACTURER_NAME_UUID = BLEUUID(0x2A29)
        FIRMWARE_REVISION_UUID = BLEUUID(0x2A26)
        SOFTWARE_REVISION_UUID = BLEUUID(0x2A28)

        FITNESS_MACHINE_FEATURES_UUID = BLEUUID(0x2ACC)
        FITNESS_MACHINE_STATUS_UUID = BLEUUID(0x2ADA)
        TREADMILL_DATA_UUID = BLEUUID(0x2ACD)
        CROSS_TRAINER_DATA_UUID = BLEUUID(0x2ACE)
        STEP_CLIMBER_DATA_UUID = BLEUUID(0x2ACF)
        ROWER_DATA_UUID = BLEUUID(0x2AD1)
        INDOOR_BIKE_DATA_UUID = BLEUUID(0x2AD2)

        SUPPORTED_INCLINATION_RANGE_UUID = BLEUUID(0x2AD5)
        SUPPORTED_RESISTANCE_LEVEL_RANGE_UUID = BLEUUID(0x2AD6)
        SUPPORTED_HEART_RATE_RANGE_UUID = BLEUUID(0x2AD7)
        SUPPORTED_POWER_RANGE_UUID = BLEUUID(0x2AD8)

    def open(self):
        self.adapter.driver.open()



        ble_enable_params = BLEEnableParams(vs_uuid_count      = 1,
                                            service_changed    = False,
                                            periph_conn_count  = 0,
                                            central_conn_count = CONNECTIONS,
                                            central_sec_count  = CONNECTIONS)
        if nrf_sd_ble_api_ver >= 3:
            print("Enabling larger ATT MTUs")
            ble_enable_params.att_mtu = 50

        self.adapter.driver.ble_enable(ble_enable_params)


    def close(self):
        self.adapter.driver.close()


    def connect_and_discover(self):
        self.adapter.driver.ble_gap_scan_start()
        new_conn = self.conn_q.get(timeout = 60)

        if nrf_sd_ble_api_ver >= 3:
            att_mtu = self.adapter.att_mtu_exchange(new_conn)

        self.adapter.service_discovery(new_conn)

        status, data = self.adapter.read_req(new_conn, DEVICE_NAME_UUID)
        data = ''.join([chr(c) for c in data])
        print('Device Name = {} status = {}'.format(data, status))

        self.adapter.enable_notification(new_conn, HRS_UUID)

        return new_conn


    def on_gap_evt_connected(self, ble_driver, conn_handle, peer_addr, role, conn_params):
        print('New connection: {}'.format(conn_handle))
        self.conn_q.put(conn_handle)


    def on_gap_evt_disconnected(self, ble_driver, conn_handle, reason):
        print('Disconnected: {} {}'.format(conn_handle, reason))


    def on_gap_evt_timeout(self, ble_driver, conn_handle, src):
        if src == BLEGapTimeoutSrc.scan:
            ble_driver.ble_gap_scan_start()


    def on_gap_evt_adv_report(self, ble_driver, conn_handle, peer_addr, rssi, adv_type, adv_data):
        dev_name_list = None
        if BLEAdvData.Types.complete_local_name in adv_data.records:
            dev_name_list = adv_data.records[BLEAdvData.Types.complete_local_name]
        elif BLEAdvData.Types.short_local_name in adv_data.records:
            dev_name_list = adv_data.records[BLEAdvData.Types.short_local_name]
        else:
            return

        dev_name = ''.join(chr(e) for e in dev_name_list)
        print('{} -> {}'.format(dev_name, dev_name_list))

        address_string  = ''.join("{0:02X}".format(b) for b in peer_addr.addr)
        # print('Received advertisement report, address: 0x{}, device_name: {}'.format(address_string,
                                                                                    # dev_name))
        if (dev_name in TARGET_DEV_NAME):
            self.adapter.connect(peer_addr)


    def on_notification(self, ble_adapter, conn_handle, uuid, data):
        if (uuid.value.value == BLEUUID.Standard.heart_rate.value):
            if (len(data) > 32):
                print('Heart Rate Received: {}...'.format(data[:10]))
                print('Data Length: {}'.format(len(data)))
            else:
                print('Heart Rate Received: {}'.format(data))
        else:
            print('Connection: {}, {} = {}'.format(conn_handle, uuid, data))

    def on_gattc_evt_read_rsp(self, ble_driver, conn_handle, status, error_handle, attr_handle, offset, data):
        print('Read Response event - Connection: {}, {} = {}'.format(conn_handle, attr_handle, data))


    def on_att_mtu_exchanged(self, ble_driver, conn_handle, att_mtu):
        print('ATT MTU exchanged: conn_handle={} att_mtu={}'.format(conn_handle, att_mtu))


    def on_gattc_evt_exchange_mtu_rsp(self, ble_driver, conn_handle, **kwargs):
        print('ATT MTU exchange response: conn_handle={}'.format(conn_handle))


def main(serial_port):
    print('Serial port used: {}'.format(serial_port))
    driver    = BLEDriver(serial_port=serial_port, auto_flash=True)
    adapter   = BLEAdapter(driver)
    collector = NUSCollector(adapter)
    collector.open()
    global name
    for i in range(CONNECTIONS):
        conn_handle = collector.connect_and_discover()

    time.sleep(10)
    print('Closing')
    collector.close()


def item_choose(item_list):
    for i, it in enumerate(item_list):
        print('\t{} : {}'.format(i, it))
    print(' ')

    while True:
        try:
            choice = int(input('Enter your choice: '))
            if ((choice >= 0) and (choice < len(item_list))):
                break
        except Exception:
            pass
        print ('\tTry again...')
    return choice


if __name__ == "__main__":
    serial_port = None
    if len(sys.argv) < 2:
        print("Please specify connectivity IC identifier (NRF51, NRF52)")
        exit(1)
    init(sys.argv[1])
    if len(sys.argv) == 3:
        serial_port = sys.argv[2]
    else:
        descs       = list(BLEDriver.enum_serial_ports())
        choices     = ['{}: {}'.format(d.port, d.serial_number) for d in descs]
        choice      = item_choose(choices)
        serial_port = descs[choice].port
    main(serial_port)
    quit()

Any help with either of these issues would be greatly appreciated!

Regards,

Scott

Parents
  • Hi,

    Which device are you testing this against? Are you running the HRS example from the SDK? Are you seeing this name issue with other peripherals as well, or only this device?

    Could you give an example of the HRS data issue? The data should only contain the bpm value.

    Best regards,
    Jørgen

  • I am testing against a custom device that implements both FTMS and HRS. The device receives HR data from a heart rate monitor and re-transmits it over BLE along with its own FTMS data.

    I am seeing the name corruption issue on other peripherals as well, not just the custom device.


    Here is an example of the HRS data issue:

    TesÝÝÝÝ -> [84, 101, 115, 221, 221, 221, 221]
    New connection: 0
    Read Response event - Connection: 0, 29 = [119, 44, 84, 143, 17, 144, 53, 19, 186, 102, 66, 192, 0, 0, 170, 12]
    Device Name = Tes   ] status = BLEGattStatusCode.success
    Read Response event - Connection: 0, 3 = [84, 101, 115, 0, 0, 0, 93]
    Heart Rate Received: [23, 9, 48, 12, 0, 128, 248, 79, 72, 119]...
    Data Length: 36664
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    
    Heart Rate Received: [20, 9, 96, 13, 0, 128, 192, 53, 83, 3]...
    Data Length: 36667
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    
    Heart Rate Received: [23, 9, 48, 12, 0, 128, 57, 0, 16, 0]...
    Data Length: 36664
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    
    Heart Rate Received: [23, 9, 48, 12, 0, 128, 57, 0, 16, 0]...
    Data Length: 36664
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    
    Heart Rate Received: [23, 9, 48, 12, 0, 128, 57, 0, 16, 0]...
    Data Length: 36664
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    
    Heart Rate Received: [23, 9, 48, 12, 0, 128, 57, 0, 16, 0]...
    Data Length: 36664
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    
    Heart Rate Received: [23, 9, 48, 12, 0, 128, 57, 0, 16, 0]...
    Data Length: 36664
    
    Heart Rate Received: [17, 9, 208, 14, 0, 136, 8, 105, 85, 3]...
    Data Length: 36806
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    
    Heart Rate Received: [23, 9, 48, 12, 0, 128, 57, 0, 16, 0]...
    Data Length: 36664
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    Closing

    The above results should be displaying a heart rate of 73 bpm. Upon using nRF Connect, the correct value can be seen.

    I also tried testing the HR data with a standard BLE heart rate monitor and the expected data can be seen (in the format specified by org.bluetooth.characteristic.heart_rate_measurement.xml).

    Note: the device name corruption can also be seen in this device.

    MYZONE-0001586252 -> [77, 89, 90, 79, 78, 69, 45, 48, 48, 48, 49, 53, 56, 54, 50, 53, 50]
    New connection: 0
    Read Response event - Connection: 0, 22 = [144, 90, 227, 175, 48, 234, 141, 232, 246, 150, 100, 70, 92, 0, 2, 27]
    Read Response event - Connection: 0, 3 = [77, 89, 90, 79, 78, 69, 45, 48, 48, 48, 49, 53, 56, 54, 50, 221, 221, 221, 221]
    Device Name = MYZONE-00015862ÝÝÝÝ status = BLEGattStatusCode.success
    Heart Rate Received: [22, 74, 27, 3, 107, 3, 148, 3, 169, 3, 112, 3, 3, 3, 31, 3, 221, 221, 221, 221]
    Heart Rate Received: [22, 75, 61, 3, 45, 3, 1, 3, 12, 3, 12, 3, 54, 3, 78, 3, 221, 221, 221, 221]
    Heart Rate Received: [22, 74, 26, 3, 2, 3, 149, 245, 111, 116]
    Heart Rate Received: [1, 0, 28, 0]
    Heart Rate Received: [22, 75, 26, 3, 2, 3]
    Heart Rate Received: [110, 0, 1, 1]
    Heart Rate Received: [22, 75, 26, 3]
    Heart Rate Received: [1, 0, 255, 255]
    Heart Rate Received: [22, 75, 59, 0, 67, 0]
    Heart Rate Received: [22, 75, 26, 3]
    Closing

Reply
  • I am testing against a custom device that implements both FTMS and HRS. The device receives HR data from a heart rate monitor and re-transmits it over BLE along with its own FTMS data.

    I am seeing the name corruption issue on other peripherals as well, not just the custom device.


    Here is an example of the HRS data issue:

    TesÝÝÝÝ -> [84, 101, 115, 221, 221, 221, 221]
    New connection: 0
    Read Response event - Connection: 0, 29 = [119, 44, 84, 143, 17, 144, 53, 19, 186, 102, 66, 192, 0, 0, 170, 12]
    Device Name = Tes   ] status = BLEGattStatusCode.success
    Read Response event - Connection: 0, 3 = [84, 101, 115, 0, 0, 0, 93]
    Heart Rate Received: [23, 9, 48, 12, 0, 128, 248, 79, 72, 119]...
    Data Length: 36664
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    
    Heart Rate Received: [20, 9, 96, 13, 0, 128, 192, 53, 83, 3]...
    Data Length: 36667
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    
    Heart Rate Received: [23, 9, 48, 12, 0, 128, 57, 0, 16, 0]...
    Data Length: 36664
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    
    Heart Rate Received: [23, 9, 48, 12, 0, 128, 57, 0, 16, 0]...
    Data Length: 36664
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    
    Heart Rate Received: [23, 9, 48, 12, 0, 128, 57, 0, 16, 0]...
    Data Length: 36664
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    
    Heart Rate Received: [23, 9, 48, 12, 0, 128, 57, 0, 16, 0]...
    Data Length: 36664
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    
    Heart Rate Received: [23, 9, 48, 12, 0, 128, 57, 0, 16, 0]...
    Data Length: 36664
    
    Heart Rate Received: [17, 9, 208, 14, 0, 136, 8, 105, 85, 3]...
    Data Length: 36806
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    
    Heart Rate Received: [23, 9, 48, 12, 0, 128, 57, 0, 16, 0]...
    Data Length: 36664
    
    Heart Rate Received: [32, 9, 125, 9, 0, 136, 84, 0, 114, 0]...
    Data Length: 36663
    Closing

    The above results should be displaying a heart rate of 73 bpm. Upon using nRF Connect, the correct value can be seen.

    I also tried testing the HR data with a standard BLE heart rate monitor and the expected data can be seen (in the format specified by org.bluetooth.characteristic.heart_rate_measurement.xml).

    Note: the device name corruption can also be seen in this device.

    MYZONE-0001586252 -> [77, 89, 90, 79, 78, 69, 45, 48, 48, 48, 49, 53, 56, 54, 50, 53, 50]
    New connection: 0
    Read Response event - Connection: 0, 22 = [144, 90, 227, 175, 48, 234, 141, 232, 246, 150, 100, 70, 92, 0, 2, 27]
    Read Response event - Connection: 0, 3 = [77, 89, 90, 79, 78, 69, 45, 48, 48, 48, 49, 53, 56, 54, 50, 221, 221, 221, 221]
    Device Name = MYZONE-00015862ÝÝÝÝ status = BLEGattStatusCode.success
    Heart Rate Received: [22, 74, 27, 3, 107, 3, 148, 3, 169, 3, 112, 3, 3, 3, 31, 3, 221, 221, 221, 221]
    Heart Rate Received: [22, 75, 61, 3, 45, 3, 1, 3, 12, 3, 12, 3, 54, 3, 78, 3, 221, 221, 221, 221]
    Heart Rate Received: [22, 74, 26, 3, 2, 3, 149, 245, 111, 116]
    Heart Rate Received: [1, 0, 28, 0]
    Heart Rate Received: [22, 75, 26, 3, 2, 3]
    Heart Rate Received: [110, 0, 1, 1]
    Heart Rate Received: [22, 75, 26, 3]
    Heart Rate Received: [1, 0, 255, 255]
    Heart Rate Received: [22, 75, 59, 0, 67, 0]
    Heart Rate Received: [22, 75, 26, 3]
    Closing

Children
No Data
Related