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

Always reconnect after peers deleted

Hi community,

I'm working on a BLE gamepad with custom board based on 51802 and s130, code modified from ble_app_hids_keyboard. The gamepad works like a charm on my Android phone, but I found there's a strange problem with reconnecting.

If I hold a specific button down while power up, the gamepad deletes all peers, just like the keyboard example does, but I don't know why, it still reconnect to my phone automatically, no matter whether peers were deleted. The only way to unbond to the phone is to delete bond from the phone side. I've tried several phones and several boards, all same results. Is that suppose to happen?

My pm init code is:

#define SEC_PARAM_BOND					1
#define SEC_PARAM_MITM					0
#define SEC_PARAM_LESC					0
#define SEC_PARAM_KEYPRESS				0
#define SEC_PARAM_IO_CAPABILITIES		BLE_GAP_IO_CAPS_NONE
#define SEC_PARAM_OOB					0
#define SEC_PARAM_MIN_KEY_SIZE			7
#define SEC_PARAM_MAX_KEY_SIZE			16
memset(&sec_param, 0, sizeof(ble_gap_sec_params_t));
>
// Security parameters to be used for all security procedures.
sec_param.bond			= SEC_PARAM_BOND;
sec_param.mitm			= SEC_PARAM_MITM;
sec_param.lesc			= SEC_PARAM_LESC;
sec_param.keypress		= SEC_PARAM_KEYPRESS;
sec_param.io_caps		= SEC_PARAM_IO_CAPABILITIES;
sec_param.oob			= SEC_PARAM_OOB;
sec_param.min_key_size	= SEC_PARAM_MIN_KEY_SIZE;
sec_param.max_key_size	= SEC_PARAM_MAX_KEY_SIZE;
sec_param.kdist_own.enc	= 1;
sec_param.kdist_own.id	= 1;
sec_param.kdist_peer.enc = 1;
sec_param.kdist_peer.id	= 1;
err_code = pm_sec_params_set(&sec_param);
APP_ERROR_CHECK(err_code);

the adv init code is:

void advertising_init(void)
{
	uint32_t				err_code;
	uint8_t					adv_flags;
	ble_advdata_t			advdata;
	ble_adv_modes_config_t	options;
	// Build and set advertising data
	memset(&advdata, 0, sizeof(advdata));
	adv_flags						= BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
	advdata.name_type				= BLE_ADVDATA_FULL_NAME;
	advdata.include_appearance		= true;
	advdata.flags					= adv_flags;
	advdata.uuids_complete.uuid_cnt	= sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
	advdata.uuids_complete.p_uuids	= m_adv_uuids;
	memset(&options, 0, sizeof(options));
	options.ble_adv_whitelist_enabled		= false;
	options.ble_adv_directed_enabled		= true;
	options.ble_adv_directed_slow_enabled	= false;
	options.ble_adv_directed_slow_interval	= 0;
	options.ble_adv_directed_slow_timeout	= 0;
	options.ble_adv_fast_enabled			= true;
	options.ble_adv_fast_interval			= APP_ADV_FAST_INTERVAL;
	options.ble_adv_fast_timeout			= APP_ADV_FAST_TIMEOUT;
	options.ble_adv_slow_enabled			= false;
	options.ble_adv_slow_interval			= APP_ADV_SLOW_INTERVAL;
	options.ble_adv_slow_timeout			= APP_ADV_SLOW_TIMEOUT;
	err_code = ble_advertising_init(&advdata,
								NULL,
								&options,
								on_adv_evt,
								ble_advertising_error_handler);
	APP_ERROR_CHECK(err_code);
}

Thanks in advance.

Parents
  • It is expected behavior. Android remembers bonding information and automatically tries to connect to BLE device. Then BLE device accepts connection. If you want to stop that connection then either delete bonding information from Android or somehow reject any connection you don't want. As I mentioned Whitelist will reject unwanted connections.

Reply
  • It is expected behavior. Android remembers bonding information and automatically tries to connect to BLE device. Then BLE device accepts connection. If you want to stop that connection then either delete bonding information from Android or somehow reject any connection you don't want. As I mentioned Whitelist will reject unwanted connections.

Children
  • But when I enabled the whitelist, and had all peers deleted, I thought the whitelist should be empty then, right? but it still reconnect automatically in the blink of an eye.

    Whitelist, no whitelist, bonds, no bonds, I've tried sorts of ways, haven't found one to prohibit this behavior yet.

  • There are probably several ways to manage bonding in different SDKs. I use Device Manager. From examples in device_manager_init() it is enough to set init_data.clear_persistent_data=true; to clear bonded devices , whitelist will be cleared as well. Do you advertise with whitelist by the way? If whitelist is empty then probably no point advertising at all? ble_advertising_start() function if can't find Whitelist(empty) will advertise without whitelist, so you need to control this situation.

  • of course I've tried adv with WL, like I said above, I've tried every possible way, none of them can stop it from auto reconnect to host.

  • As Alex said, it doesn't make sense to do connectable advertising with an empty whitelist, because then no device will be able to connect to you. You are probably doing advertising without a whitelist. If you don't want the Android device to recognize your device, you can try to change the device address.

  • Thanks for your tips Petter, but WL and advertising is actually irrelevant to my question, currently I've always keep WL disabled while advertising.

    What I expect is:

    1. Once I have all peers deleted during power up, my gamepad should not automatically reconnect to last host, but keep advertising.

    2. Once I delete bond info on my phone while connected, the gamepad should restart advertising and appear again in the available device list again, then if I click it, it should connect and bond again.

    But what I get is:

    1. No matter if peers are deleted during power up, the gamepad always auto-reconnected to my phone.

    2. If bond info is deleted on my phone, the gamepad restarts advertising and appear in the available device list as I expected, that's fine. But if I click it, the phone pop up an err message says device refused pairing request.

    So I just want to know how to correct this to what I expected? Or maybe my expectation is impractical?

Related