Example #1
0
def send_heart_rate_measurement():
    hvx_params = ble_driver.ble_gatts_hvx_params_t()
    encoded_hrm = ble_driver.uint8_array(MAX_HRM_LEN)

    generate_heart_rate()

    length = encode_heart_rate_measurement(encoded_hrm, heart_rate)

    hvx_length = ble_driver.new_uint16()
    ble_driver.uint16_assign(hvx_length, length)

    hvx_params.handle = heart_rate_measurement_handle.value_handle
    hvx_params.type = ble_driver.BLE_GATT_HVX_NOTIFICATION
    hvx_params.offset = 0
    hvx_params.p_len = hvx_length
    hvx_params.p_data = encoded_hrm.cast()

    error_code = ble_driver.sd_ble_gatts_hvx(connection_handle, hvx_params)

    actual_hvx_length = ble_driver.uint16_value(hvx_length)
    ble_driver.delete_uint16(hvx_length)

    if error_code == ble_driver.NRF_SUCCESS and length != actual_hvx_length:
        error_code = ble_driver.NRF_ERROR_DATA_SIZE
        print "Failed to send heart rate measurement. Error code: 0x{0:02X}".format(error_code)
        return error_code

    if error_code != ble_driver.NRF_SUCCESS:
        print "Failed to send heart rate measurement. Error code: 0x{0:02X}".format(error_code)
        return error_code

    return ble_driver.NRF_SUCCESS
Example #2
0
def send_heart_rate_measurement():
    hvx_params = ble_driver.ble_gatts_hvx_params_t()
    encoded_hrm = ble_driver.uint8_array(MAX_HRM_LEN)

    generate_heart_rate()

    length = encode_heart_rate_measurement(encoded_hrm, heart_rate)

    hvx_length = ble_driver.new_uint16()
    ble_driver.uint16_assign(hvx_length, length)

    hvx_params.handle = heart_rate_measurement_handle.value_handle
    hvx_params.type = ble_driver.BLE_GATT_HVX_NOTIFICATION
    hvx_params.offset = 0
    hvx_params.p_len = hvx_length
    hvx_params.p_data = encoded_hrm.cast()

    error_code = ble_driver.sd_ble_gatts_hvx(connection_handle, hvx_params)

    actual_hvx_length = ble_driver.uint16_value(hvx_length)
    ble_driver.delete_uint16(hvx_length)

    if error_code == ble_driver.NRF_SUCCESS and length != actual_hvx_length:
        error_code = ble_driver.NRF_ERROR_DATA_SIZE
        print "Failed to send heart rate measurement. Error code: 0x{0:02X}".format(error_code)
        return error_code

    if error_code != ble_driver.NRF_SUCCESS:
        print "Failed to send heart rate measurement. Error code: 0x{0:02X}".format(error_code)
        return error_code

    return ble_driver.NRF_SUCCESS
Example #3
0
def init_characteristics():
    char_md = ble_driver.ble_gatts_char_md_t()
    cccd_md = ble_driver.ble_gatts_attr_md_t()
    attr_char_value = ble_driver.ble_gatts_attr_t()
    ble_uuid = ble_driver.ble_uuid_t()
    attr_md = ble_driver.ble_gatts_attr_md_t()
    encoded_initial_hrm = ble_driver.uint8_array(MAX_HRM_LEN)

    cccd_md.read_perm.sm = 1
    cccd_md.read_perm.lv = 1
    cccd_md.write_perm.sm = 1
    cccd_md.write_perm.lv = 1
    cccd_md.vloc = ble_driver.BLE_GATTS_VLOC_STACK

    char_md.char_props.notify = 1
    char_md.p_char_user_desc = None
    char_md.p_char_pf = None
    char_md.p_user_desc_md = None
    char_md.p_cccd_md = cccd_md
    char_md.p_sccd_md = None

    ble_uuid.type = ble_driver.BLE_UUID_TYPE_BLE
    ble_uuid.uuid = 0x2A37

    attr_md.read_perm.sm = 1
    attr_md.read_perm.lv = 1
    attr_md.write_perm.sm = 1
    attr_md.write_perm.lv = 1
    attr_md.vloc = ble_driver.BLE_GATTS_VLOC_STACK
    attr_md.rd_auth = 0
    attr_md.wr_auth = 0
    attr_md.vlen = 1

    attr_char_value_init_len = encode_heart_rate_measurement(
        encoded_initial_hrm, 10)

    attr_char_value.p_uuid = ble_uuid
    attr_char_value.p_attr_md = attr_md
    attr_char_value.init_len = attr_char_value_init_len
    attr_char_value.init_offs = 0
    attr_char_value.max_len = MAX_HRM_LEN
    attr_char_value.p_value = encoded_initial_hrm.cast()

    error_code = ble_driver.sd_ble_gatts_characteristic_add(
        heart_rate_service_handle, char_md, attr_char_value,
        heart_rate_measurement_handle)

    if error_code != ble_driver.NRF_SUCCESS:
        print "Failed to initialize characteristics. Error code: 0x{0:02X}".format(
            error_code)
        return error_code

    print "Characteristics initiated"

    return ble_driver.NRF_SUCCESS
Example #4
0
def init_characteristics():
    char_md = ble_driver.ble_gatts_char_md_t()
    cccd_md = ble_driver.ble_gatts_attr_md_t()
    attr_char_value = ble_driver.ble_gatts_attr_t()
    ble_uuid = ble_driver.ble_uuid_t()
    attr_md = ble_driver.ble_gatts_attr_md_t()
    encoded_initial_hrm = ble_driver.uint8_array(MAX_HRM_LEN)

    cccd_md.read_perm.sm = 1
    cccd_md.read_perm.lv = 1
    cccd_md.write_perm.sm = 1
    cccd_md.write_perm.lv = 1
    cccd_md.vloc = ble_driver.BLE_GATTS_VLOC_STACK

    char_md.char_props.notify = 1
    char_md.p_char_user_desc = None
    char_md.p_char_pf = None
    char_md.p_user_desc_md = None
    char_md.p_cccd_md = cccd_md
    char_md.p_sccd_md = None

    ble_uuid.type = ble_driver.BLE_UUID_TYPE_BLE
    ble_uuid.uuid = 0x2A37

    attr_md.read_perm.sm = 1
    attr_md.read_perm.lv = 1
    attr_md.write_perm.sm = 1
    attr_md.write_perm.lv = 1
    attr_md.vloc = ble_driver.BLE_GATTS_VLOC_STACK
    attr_md.rd_auth = 0
    attr_md.wr_auth = 0
    attr_md.vlen = 1

    attr_char_value_init_len = encode_heart_rate_measurement(encoded_initial_hrm, 10)

    attr_char_value.p_uuid = ble_uuid
    attr_char_value.p_attr_md = attr_md
    attr_char_value.init_len = attr_char_value_init_len
    attr_char_value.init_offs = 0
    attr_char_value.max_len = MAX_HRM_LEN
    attr_char_value.p_value = encoded_initial_hrm.cast()

    error_code = ble_driver.sd_ble_gatts_characteristic_add(heart_rate_service_handle,
                                                            char_md,
                                                            attr_char_value,
                                                            heart_rate_measurement_handle)

    if error_code != ble_driver.NRF_SUCCESS:
        print "Failed to initialize characteristics. Error code: 0x{0:02X}".format(error_code)
        return error_code

    print "Characteristics initiated"

    return ble_driver.NRF_SUCCESS