Exemple #1
0
def main(serial_port):
    print "Serial port used: {}".format(serial_port)
    ble_driver.sd_rpc_serial_port_name_set(serial_port)
    ble_driver.sd_rpc_serial_baud_rate_set(115200)
    ble_driver.sd_rpc_log_handler_set(log_message_handler)
    ble_driver.sd_rpc_evt_handler_set(ble_evt_handler)
    error_code = ble_driver.sd_rpc_open()

    if error_code != ble_driver.NRF_SUCCESS:
        print "Failed to open the nRF51 BLE Driver. Error code: 0x{0:X}.".format(
            error_code)
        return

    error_code = init_ble_stack()

    if error_code != ble_driver.NRF_SUCCESS:
        return

    init_connection_params()
    init_scan_params()

    error_code = init_services()

    if error_code != ble_driver.NRF_SUCCESS:
        return

    set_adv_data()

    start_scan()

    cccd_value = 0

    while True:
        char = sys.stdin.readline()
        if 'q' in char:
            break
        else:
            print "Received character {}".format(char)
        cccd_value ^= CCCD_NOTIFY
        set_hrm_cccd(cccd_value)

    if peripheral_connection_handle != ble_driver.BLE_CONN_HANDLE_INVALID:
        ble_driver.sd_ble_gap_disconnect(
            peripheral_connection_handle,
            ble_driver.BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)

    if central_connection_handle != ble_driver.BLE_CONN_HANDLE_INVALID:
        ble_driver.sd_ble_gap_disconnect(
            peripheral_connection_handle,
            ble_driver.BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)

    error_code = ble_driver.sd_rpc_close()

    if error_code != ble_driver.NRF_SUCCESS:
        print "Failed to close the nRF51 BLE Driver. Error code: 0x{0:X}".format(
            error_code)
        return

    print "Closed"
Exemple #2
0
def main(serial_port):
    print "Serial port used: {}".format(serial_port)
    ble_driver.sd_rpc_serial_port_name_set(serial_port)
    ble_driver.sd_rpc_serial_baud_rate_set(115200)
    ble_driver.sd_rpc_log_handler_set(log_message_handler)
    ble_driver.sd_rpc_evt_handler_set(ble_evt_handler)
    error_code = ble_driver.sd_rpc_open()

    if error_code != ble_driver.NRF_SUCCESS:
        print "Failed to open the nRF51 BLE Driver. Error code: 0x{0:X}.".format(error_code)
        return

    error_code = init_ble_stack()

    if error_code != ble_driver.NRF_SUCCESS:
        return

    init_connection_params()
    init_scan_params()

    error_code = init_services()

    if error_code != ble_driver.NRF_SUCCESS:
        return

    set_adv_data()

    start_scan()

    cccd_value = 0

    while True:
        char = sys.stdin.readline()
        if 'q' in char:
            break
        else:
            print "Received character {}".format(char)
        cccd_value ^= CCCD_NOTIFY
        set_hrm_cccd(cccd_value)

    if peripheral_connection_handle != ble_driver.BLE_CONN_HANDLE_INVALID:
        ble_driver.sd_ble_gap_disconnect(peripheral_connection_handle,
                                         ble_driver.BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)

    if central_connection_handle != ble_driver.BLE_CONN_HANDLE_INVALID:
        ble_driver.sd_ble_gap_disconnect(peripheral_connection_handle,
                                         ble_driver.BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)

    error_code = ble_driver.sd_rpc_close()

    if error_code != ble_driver.NRF_SUCCESS:
        print "Failed to close the nRF51 BLE Driver. Error code: 0x{0:X}".format(error_code)
        return

    print "Closed"
Exemple #3
0
def on_disconnected(gap_event):
    global peripheral_connection_handle
    global central_connection_handle

    if peripheral_connection_handle == gap_event.conn_handle:
        print "Disconnected from a central, reason: 0x{0:02X}".format(
            gap_event.params.disconnected.reason)
        peripheral_connection_handle = ble_driver.BLE_CONN_HANDLE_INVALID
        # If we have a connection to a periperhal device we start to advertise again
        if central_connection_handle != ble_driver.BLE_CONN_HANDLE_INVALID:
            start_advertising()
    elif central_connection_handle == gap_event.conn_handle:
        print "Disconnected from a peripheral, reason: 0x{0:02X}".format(
            gap_event.params.disconnected.reason)
        # If we have a connection to a central device we disconnect from it since we have no
        # peripheral that gives us values to propagate
        if peripheral_connection_handle == ble_driver.BLE_CONN_HANDLE_INVALID:
            ble_driver.sd_ble_gap_disconnect(peripheral_connection_handle,
                                             ble_driver.BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)
        start_scan()
Exemple #4
0
def on_disconnected(gap_event):
    global peripheral_connection_handle
    global central_connection_handle

    if peripheral_connection_handle == gap_event.conn_handle:
        print "Disconnected from a central, reason: 0x{0:02X}".format(
            gap_event.params.disconnected.reason)
        peripheral_connection_handle = ble_driver.BLE_CONN_HANDLE_INVALID
        # If we have a connection to a periperhal device we start to advertise again
        if central_connection_handle != ble_driver.BLE_CONN_HANDLE_INVALID:
            start_advertising()
    elif central_connection_handle == gap_event.conn_handle:
        print "Disconnected from a peripheral, reason: 0x{0:02X}".format(
            gap_event.params.disconnected.reason)
        # If we have a connection to a central device we disconnect from it since we have no
        # peripheral that gives us values to propagate
        if peripheral_connection_handle == ble_driver.BLE_CONN_HANDLE_INVALID:
            ble_driver.sd_ble_gap_disconnect(
                peripheral_connection_handle,
                ble_driver.BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)
        start_scan()