cancel
Showing results for 
Search instead for 
Did you mean: 

Pairing with smartphone started by device

LSpad.2
Associate

Hi,

i have some pairing problems with a STM32WB board and a smartphone.

I have configured the STM32WB for working with the "Just Works" method (code attached).

static void Ble_Hci_Gap_Gatt_Init(){
  uint8_t role;
  uint16_t gap_service_handle, gap_dev_name_char_handle, gap_appearance_char_handle;
  uint8_t *bd_addr;
  uint16_t appearance[1] = { BLE_CFG_GAP_APPEARANCE };
 
  hci_reset(); // Initialize HCI layer
 
  // Write the BD Address
  bd_addr = BleGetBdAddress();
  aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN, (uint8_t*) bd_addr);
 
  // Write Identity root key used to derive LTK and CSRK
  aci_hal_write_config_data(CONFIG_DATA_IR_OFFSET, CONFIG_DATA_IR_LEN, (uint8_t*) BLE_CFG_IR_VALUE);
 
  // Write Encryption root key used to derive LTK and CSRK
  aci_hal_write_config_data(CONFIG_DATA_ER_OFFSET, CONFIG_DATA_ER_LEN, (uint8_t*) BLE_CFG_ER_VALUE);
 
   // Set TX Power to max.
  aci_hal_set_tx_power_level(1, CFG_TX_POWER);
 
  // Initialize GATT interface
  aci_gatt_init();
 
  // Initialize GAP interface
  role = GAP_PERIPHERAL_ROLE;
 
  if (role > 0) {
    const char *name = "IfSat";
    aci_gap_init(role, PRIVACY_DISABLED, APPBLE_GAP_DEVICE_NAME_LENGTH, &gap_service_handle, &gap_dev_name_char_handle, &gap_appearance_char_handle);
    if (aci_gatt_update_char_value(gap_service_handle, gap_dev_name_char_handle, 0, strlen(name), (uint8_t *) name))
      BLE_DBG_SVCCTL_MSG("Device Name aci_gatt_update_char_value failed.\n");
  }
 
  if(aci_gatt_update_char_value(gap_service_handle, gap_appearance_char_handle, 0, 2, (uint8_t *)&appearance))
    BLE_DBG_SVCCTL_MSG("Appearance aci_gatt_update_char_value failed.\n");
 
  // Initialize Default PHY
  hci_le_set_default_phy(0x00,0x02,0x02);
 
  // Initialize IO capability
  aci_gap_set_io_capability(CFG_IO_CAPABILITY); //3
 
  aci_gap_set_authentication_requirement(CFG_BONDING_MODE, //1
  																			 CFG_MITM_PROTECTION, //0
																				 CFG_SC_SUPPORT, //1
																				 CFG_KEYPRESS_NOT_SUPPORTED, //0
																				 CFG_ENCRYPTION_KEY_SIZE_MIN, //8
																				 CFG_ENCRYPTION_KEY_SIZE_MAX, //16
																				 CFG_USED_FIXED_PIN, // NOT USE FIXED PIN //1
                                         111111,// FIXED PIN
																				 PUBLIC_ADDR);
 
  // Initialize whitelist
   if (CFG_BONDING_MODE)
     aci_gap_configure_whitelist();
}

When the Smartphone connects, the board receives the connection complete event. In this event i ask for security update (line 20):

        case HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE:
        	HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_SET);
          connection_complete_event = (hci_le_connection_complete_event_rp0 *) meta_evt->data;
					aci_gap_get_security_level(connection_complete_event->Connection_Handle,&sec_mode,&sec_level);
					hci_le_read_phy(connection_complete_event->Connection_Handle,&TX_PHY,&RX_PHY);
					printf("Client connected: %02X%02X%02X%02X%02X%02X SM=%d SL=%d TX=%d RX=%d\r\n", connection_complete_event->Peer_Address[0],
																																													 connection_complete_event->Peer_Address[1],
																																													 connection_complete_event->Peer_Address[2],
																																													 connection_complete_event->Peer_Address[3],
																																													 connection_complete_event->Peer_Address[4],
																																													 connection_complete_event->Peer_Address[5],
																																													 sec_mode,sec_level,TX_PHY,RX_PHY);
					BleParams.ClientStatus = CLIENT_CONNECTED;
					BleParams.connectionHandle = connection_complete_event->Connection_Handle;
					TimerStop(BleParams.AdvTimeoutTimerId);
					tBleStatus alfa = aci_gap_is_device_bonded(0,connection_complete_event->Peer_Address);
					memcpy(BleParams.ClientAddress,connection_complete_event->Peer_Address,6);
					printf("Client bonding status %02X\r\n",alfa);
					printf("Pairing Request Sent\r\n");
					aci_gap_slave_security_req(BleParams.connectionHandle);
					P2PS_APP_ConnHandle_Not_evt_t pNotification = {EVT_LE_CONN_COMPLETE, BleParams.connectionHandle};
					P2PS_APP_Notification(&pNotification);
          break; /* HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE */

So i send a pairing request to the initiator of the connection, this is made this way because in some smartphone OS it is not possible to send a pairing request, but only receive it.

This request sometimes does not work:

  • when it works on the smartphone i see a popup window in which is asked to confirm the pairing, I click accept and the pairing works.
  • when it does not work the popup is not shown and some seconds later (~20s) a PAIRING_COMPLETE_EVENT event is raised inthe STM32WB with failing result.

The number of successful cases are way less than the failures. Failures can happen in all of the following cases:

  • one of the two devices is bonded with the other one and the other one has canceled the bonding.
  • both devices are bonded each other.
  • none of the devices is bonded each other.

Which can be the problem? Is there a way to avoid it?

Thank you

1 REPLY 1
Remy ISSALYS
ST Employee

Hello,

When you received the pairing complete event, what is the reason of the failure? Which smartphone application are you using?

Best Regards