Example #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
Example #2
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
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
Example #5
0
def start_service_discovery():
    print "Discovering primary services"
    start_handle = 0x0001

    srvc_uuid = ble_driver.ble_uuid_t()
    srvc_uuid.type = ble_driver.BLE_UUID_TYPE_BLE
    srvc_uuid.uuid = BLE_UUID_HEART_RATE_SERVICE

    error_code = ble_driver.sd_ble_gattc_primary_services_discover(
        central_connection_handle, start_handle, srvc_uuid)

    if error_code != ble_driver.NRF_SUCCESS:
        print "Failed to discover primary services"
        return error_code

    return ble_driver.NRF_SUCCESS
Example #6
0
def start_service_discovery():
    print "Discovering primary services"
    start_handle = 0x0001

    srvc_uuid = ble_driver.ble_uuid_t()
    srvc_uuid.type = ble_driver.BLE_UUID_TYPE_BLE
    srvc_uuid.uuid = HRM_SERVICE_UUID

    error_code = ble_driver.sd_ble_gattc_primary_services_discover(connection_handle, start_handle,
                                                                   srvc_uuid)

    if error_code != ble_driver.NRF_SUCCESS:
        print "Failed to discover primary services"
        return error_code

    return ble_driver.NRF_SUCCESS