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

How to compare the peripheral device adresses and connect from the UART string

Hi I want to get the peripheral address from the UART and compare with scanned peripheral addresses from the central (nrf52832) and if the received address string is of one of the scanned devices list, the central will connect to that particular device. Actually it's like "connect <device address>" in "ble_app_interactive" (SDK15.2.0) but from the UART instead of CLI. But I don't know how to do this.

I use ble_app_uart_c as a central and scan for the peripheral of NUS type and I found. Now I want to connect one of the scanned NUS peripheral devices which address is from the UART. When I studied "cmd_device_connect()" function in ble_app_interactive example, I couldn't find how they retreive the address from the string, compare and connect.

How can I do this?  Thanks

 

/**@brief Command handler for establishing a connection.
 */
static void cmd_device_connect(nrf_cli_t const * p_cli, size_t argc, char ** argv)
{
    if ((argc == 1) || nrf_cli_help_requested(p_cli))
    {
        nrf_cli_help_print(p_cli, NULL, 0);
        return;
    }

    if (argc == 2)
    {
        //Connect by peer ID if davice was bonded and using privacy.
        if (strlen(argv[1]) < (ADDR_STRING_LEN - 1))
        {
            uint16_t pm_id;
            pm_id = atoi(argv[1]);
            private_connect(&pm_id);
            return;
        }

        // If address or peer ID have incorrect format, then return.
        if (strlen(argv[1]) != (ADDR_STRING_LEN - 1))
        {
            nrf_cli_fprintf(p_cli,
                            NRF_CLI_ERROR,
                            "wrong address length - Correct format: XX:XX:XX:XX:XX:XX\r\n");
            return;
        }

        //Connect by device address.
        addr_store_value_set(argv[1]);
    }
    else
    {
        nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, WRONG_PARAMETER_COUNT);
    }
}

Parents
  • Hi,

    You can modify the NUS central example to connect to a specific address for instance by changing the scan_init() function to this:

    static void scan_init(void)
    {
        ret_code_t          err_code;
        nrf_ble_scan_init_t init_scan;
        static uint8_t      addr[BLE_GAP_ADDR_LEN] = {0x45, 0x9F, 0xC7, 0xF7, 0x66, 0xEF};
    
        memset(&init_scan, 0, sizeof(init_scan));
    
        init_scan.connect_if_match = true;
        init_scan.conn_cfg_tag     = APP_BLE_CONN_CFG_TAG;
    
        err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_ADDR_FILTER, addr);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_ADDR_FILTER, false);
        APP_ERROR_CHECK(err_code);
    }

    You also have to modify sdk_config.h, setting NRF_BLE_SCAN_ADDRESS_CNT to 1. This will only connect to a device with BLE MAC address EF:66:F7:C7:9F:45.

    The mechanism is the same if you get the address via UART instead, and then you can update the filter as needed by using nrf_ble_scan_all_filter_remove() to remove an old address and set a new filter. ( In fact the filtering mechanism is quite flexible, but that should not be relevant here. If you are interested, just look at the API documentation in <SDK>\components\ble\nrf_ble_scan\nrf_ble_scan.h

Reply
  • Hi,

    You can modify the NUS central example to connect to a specific address for instance by changing the scan_init() function to this:

    static void scan_init(void)
    {
        ret_code_t          err_code;
        nrf_ble_scan_init_t init_scan;
        static uint8_t      addr[BLE_GAP_ADDR_LEN] = {0x45, 0x9F, 0xC7, 0xF7, 0x66, 0xEF};
    
        memset(&init_scan, 0, sizeof(init_scan));
    
        init_scan.connect_if_match = true;
        init_scan.conn_cfg_tag     = APP_BLE_CONN_CFG_TAG;
    
        err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_ADDR_FILTER, addr);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_ADDR_FILTER, false);
        APP_ERROR_CHECK(err_code);
    }

    You also have to modify sdk_config.h, setting NRF_BLE_SCAN_ADDRESS_CNT to 1. This will only connect to a device with BLE MAC address EF:66:F7:C7:9F:45.

    The mechanism is the same if you get the address via UART instead, and then you can update the filter as needed by using nrf_ble_scan_all_filter_remove() to remove an old address and set a new filter. ( In fact the filtering mechanism is quite flexible, but that should not be relevant here. If you are interested, just look at the API documentation in <SDK>\components\ble\nrf_ble_scan\nrf_ble_scan.h

Children
No Data
Related