Beispiel #1
0
def init_services():
    global heart_rate_service_handle
    ble_uuid = ble_driver.ble_uuid_t()

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

    heart_rate_service_handle_send = ble_driver.new_uint16()

    error_code = ble_driver.sd_ble_gatts_service_add(
        ble_driver.BLE_GATTS_SRVC_TYPE_PRIMARY, ble_uuid,
        heart_rate_service_handle_send)

    heart_rate_service_handle = ble_driver.uint16_value(
        heart_rate_service_handle_send)
    ble_driver.delete_uint16(heart_rate_service_handle_send)

    if error_code != ble_driver.NRF_SUCCESS:
        print "Could not initialize service. Error code: 0x{0:02X}".format(
            error_code)
        return error_code

    print "Services initiated"

    error_code = init_characteristics()

    if error_code != ble_driver.NRF_SUCCESS:
        return error_code

    return ble_driver.NRF_SUCCESS
Beispiel #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
Beispiel #3
0
def send_heart_rate_relay(length, data):
    hvx_length = ble_driver.new_uint16()
    ble_driver.uint16_assign(hvx_length, length)

    hvx_params = ble_driver.ble_gatts_hvx_params_t()
    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 = data

    error_code = ble_driver.sd_ble_gatts_hvx(peripheral_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
Beispiel #4
0
def init_services():
    global heart_rate_service_handle
    ble_uuid = ble_driver.ble_uuid_t()

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

    heart_rate_service_handle_send = ble_driver.new_uint16()

    error_code = ble_driver.sd_ble_gatts_service_add(
        ble_driver.BLE_GATTS_SRVC_TYPE_PRIMARY, ble_uuid, heart_rate_service_handle_send
    )

    heart_rate_service_handle = ble_driver.uint16_value(heart_rate_service_handle_send)
    ble_driver.delete_uint16(heart_rate_service_handle_send)

    if error_code != ble_driver.NRF_SUCCESS:
        print "Could not initialize service. Error code: 0x{0:02X}".format(error_code)
        return error_code

    print "Services initiated"

    error_code = init_characteristics()

    if error_code != ble_driver.NRF_SUCCESS:
        return error_code

    return ble_driver.NRF_SUCCESS