BLE connection through MAC Address Issue.

Hey, what i'm trying to do is the following:

- Advertise The unique MAC-Addresse

- Scan and connect to a Peripheral Node with a specific MAC-Address

But it isn't working

My configuration is as following:

advertisement setup:

static void gap_params_init(void) {
  ret_code_t err_code;
  ble_gap_conn_params_t gap_conn_params;
  ble_gap_conn_sec_mode_t sec_mode;

  BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);

  err_code = sd_ble_gap_device_name_set(&sec_mode,
      (const uint8_t *)DEVICE_NAME,
      strlen(DEVICE_NAME));
  APP_ERROR_CHECK(err_code);

  memset(&gap_conn_params, 0, sizeof(gap_conn_params));

  gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
  gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
  gap_conn_params.slave_latency = SLAVE_LATENCY;
  gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;

  //get MAC
  err_code = sd_ble_gap_addr_get(&m_adv_addr);
  APP_ERROR_CHECK(err_code);
  m_adv_addr.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
  err_code = sd_ble_gap_addr_set(&m_adv_addr);
  APP_ERROR_CHECK(err_code);
  err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
  APP_ERROR_CHECK(err_code);
}

static void advertising_init(void) {
  ret_code_t err_code;
  ble_advdata_t advdata;
  ble_advdata_t srdata;

  ble_uuid_t adv_uuids[] =
      {
          {
              LBS_UUID_SERVICE,
              BLE_UUID_TYPE_VENDOR_BEGIN
          }};

  // Build and set advertising data.
  memset(&advdata, 0, sizeof(advdata));

  ble_advdata_manuf_data_t manuf_specific_data;
  uint8_t manuf_data[] = {6, 6, 6, 6};
  manuf_specific_data = set_manuf_data(manuf_data, sizeof(manuf_data));

  advdata.p_manuf_specific_data = &manuf_specific_data;
  advdata.name_type = BLE_ADVDATA_FULL_NAME;
  advdata.include_appearance = false;
  advdata.include_ble_device_addr = true;
  advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;

  memset(&srdata, 0, sizeof(srdata));
  srdata.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
  srdata.uuids_complete.p_uuids = adv_uuids;

  err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
  err_code = ble_advdata_encode(&srdata, m_adv_data.scan_rsp_data.p_data, &m_adv_data.scan_rsp_data.len);
  APP_ERROR_CHECK(err_code);

  ble_gap_adv_params_t adv_params;

  // Set advertising parameters.
  memset(&adv_params, 0, sizeof(adv_params));

  adv_params.primary_phy = BLE_GAP_PHY_1MBPS;
  adv_params.duration = APP_ADV_DURATION;
  adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
  adv_params.p_peer_addr = NULL;
  adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
  adv_params.interval = APP_ADV_INTERVAL;
  advertiser_address_set(&m_adv_handle, &m_adv_addr);
  err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &adv_params);
  APP_ERROR_CHECK(err_code);

scan setup:

static ble_gap_addr_t const mac_addr =
    {
        .addr_type = BLE_GAP_ADDR_TYPE_PUBLIC,
        .addr = {0x32, 0xAA, 0xB8, 0xA2, 0xB8, 0xEA}
        //tried all three
        //.addr      = {0x32, 0xAA, 0xB8, 0xA2, 0xB8, 0xEA, 0x00}
        //.addr      = {0x00,0xEA,0xB8,0xA2,0xB8,0xAA,0x32}
};

static void scan_init(void) {
  ret_code_t err_code;
  nrf_ble_scan_init_t init_scan;
  ble_gap_addr_t m_mac_filter;

  memset(&init_scan, 0, sizeof(init_scan));
  APP_ERROR_CHECK(err_code);

  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, m_mac_filter.addr);
  APP_ERROR_CHECK(err_code);

  // Setting filters for scanning.
  err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_ADDR_FILTER, false);
  APP_ERROR_CHECK(err_code);
}

BLE nRF Connect advertiement :

i also wonder why the 0 is attached.

  • In the scan setup, try changing the following line:

    err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_ADDR_FILTER, m_mac_filter.addr);

    to the following:

    err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_ADDR_FILTER, mac_addr.addr);

    Swap m_mac_filter with mac_addr

    Best regards,

    Simon

  • 0
    159 pts.
    in reply to Simon

    The change unfortunately did not fix the issue.

    A thing i have noticed is that a 0 is attached in the avertissement header and that the address is mirrored, why is this? 

  • +1
    6810 pts.
    in reply to RSA

    I was able to achieve this in the following manner:

    • I modified the examples examples\ble_peripheral\ble_app_blinky and examples\ble_central\ble_app_blinky_c
    • In examples\ble_peripheral\ble_app_blinky I did the following:
      • Built and flashed the example without modifying it
        • If I understand you right, you want to connect to the device based on the unique MAC address. By default, this address will be included in the advertising packet (no need to add it through sd_ble_gap_addr_set()).
      • The blinky peripheral device is now advertising, and I used the nRF Connect app on my phone to check what the device/mac address was:

    • In the examples\ble_central\ble_app_blinky_c I did the following:
      • Added address filtering based on the findings above:

    static ble_gap_addr_t const mac_addr =
        {
            .addr_type = BLE_GAP_ADDR_TYPE_RANDOM_STATIC,
            .addr = {0x4C, 0x9E, 0xFE, 0x20, 0x83, 0xD4}
    };
    
    
    static void scan_init(void)
    {
        ret_code_t          err_code;
        nrf_ble_scan_init_t init_scan;
    
        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);
    
        // Setting filters for scanning.
        err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_ADDR_FILTER, false);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_ADDR_FILTER, mac_addr.addr);
        APP_ERROR_CHECK(err_code);
    }

    A thing i have noticed is that a 0 is attached in the avertissement header and that the address is mirrored, why is this? 

    I think this is related to the specific address type. Since you set it to BLE_GAP_ADDR_TYPE_PUBLIC in the peripheral example, the MSBits was set to '0'. Read more about this in these links:

    Best regards,

    Simon

  • 0
    159 pts.
    in reply to Simon

    what solved the issue was for some reason removing the sd_ble_gap_addr_set()

Related