XIAO nRF52840 bluefruit choose a different filter policy

Hi, I wanna begin by thanking Seeedstudio for making such a simple device to get started and do with BLE. I’m new to this but I’m finding it extremely interesting.
I created a BLE script that advertises as a simple peripheral with just a bleuart service. What I would love to do is make my XIAO not scannable nor connectable by devices that have not bound with it. However when I tried changing the advertisement to direct instead of undirected using

  // Advertising type https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/8c8bcf292e7f6ecd505b465f7d5d83c659ab831f/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp#L275
  // default -> BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/8c8bcf292e7f6ecd505b465f7d5d83c659ab831f/cores/nRF5/nordic/softdevice/s132_nrf52_6.1.1_API/include/ble_gap.h#L372
  Bluefruit.Advertising.setType(BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED);

my bonded phone couldn’t find it either.
So after this I went on and searched a bit more when I stumbled upon this FILTER POLICY, however in the adafruit library I couldn’t find how I can specify a different policy and the following way to create a whitelist.
Here’s the code I found: Adafruit_nRF52_Arduino/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp at 8c8bcf292e7f6ecd505b465f7d5d83c659ab831f · adafruit/Adafruit_nRF52_Arduino · GitHub

Hi there,
and Welcome here.
So I haven’t seen the bonding ever work outside of an Adafruit bootloader device, YMMV
I think the softdevice has to support it and Seeed does not, unless someone knows better.
What Mobile device are You using because some of those only bond with Bluetooth , Not BLE
Pretty sure it’s the encryption NOT being present that prevents it not being supported if that makes sense. You may need to load a different softdevice and/or a NimBle stack (oer Adafruit bootloader)
All uncharted areas for the basic ArduinoIDE and Seeeds level of it being supported.
SO encryption needed for pairing, Pairing needed for Bonding and Stack support for filter policy.
I have seen some pairing related to the MIDI HID stuff but I don’t think that applies here.

It IS a great device Xiao Nrf Series, albeit getting OLD now, ie. 2 generations behind of what is available and that supports newer softdevice, Nordic SDK would allow it from the jump along with the newer FDM stuff for Google.
HTH
GL :slight_smile: PJ
:v:

Member Msfujino may know more about the filter and advertising, see if he adds to the comments with any further wisdom from his fast experience with the device and Seeed as well Adafruit’s stuff.

now that’s strange. I’m using a Mi 9T with Lineage OS 21 installed on. I can pair and bond without any issue and I can’t use the uart if I don’t. I’m using a modified version of the example pairing_passkey.

Hi there,
Wow that is Unique, Which BSP are you using?
Does it remember the bond if you reset or power off/on?
You are using the Adafruit, Bluefruit LIB then?
Good Stuff, you got that working, Can you post the code?
I have only witness Struggles on here getting the Bonding to work , so very cool.:+1:
GL :slight_smile: PJ
:v:

this makes me question if maybe I’m getting something wrong, but the nrf app says it’s bonded. I used this example Adafruit_nRF52_Arduino/libraries/Bluefruit52Lib/examples/Peripheral/pairing_passkey/pairing_passkey.ino at 8c8bcf292e7f6ecd505b465f7d5d83c659ab831f · adafruit/Adafruit_nRF52_Arduino · GitHub
Yeah it remembers the bond when I turn it off and on again. I haven’t touched anything, just put the example on it and it worked. Stock bootloader and all. I’m using the normal package, not the mbed. I still can’t get it to reconnect automatically, but that might be because since it’s a peripheral it should be the phone to initiate the reconnection. I hope that Msfujino might be able to help me to get it advertise only for the known devices and not everyone.

Hi Invy55,
BLE’s Advertizement is emitted to all and cannot be aimed at a specific device, can it? However, I think it is possible to limit the connection to a specific party.

The following link “Limiting Connencion” may be helpful.
Ultimate Guide to Managing Your BLE Connection | Punch Through

“Encrypted Advertising Data” in the following link may also be helpful.
www.bluetooth.com/wp-content/uploads/2023/02/2301_5.4_Tech_Overview_FINAL.pdf

However, you will need to read the source code to see if these features are implemented in the “bluefruit” library.
My impression is that the ESP32 BLE library is newer and has more features implemented.

1 Like

For anyone that might be wandering how I got around this issue, well… I did not. I ended up manually checking upon connection if the central has a bond key and if it doesn’t it simply disconnects them unless I enable pairing thru a dedicated hardware button.

void connect_callback(uint16_t conn_handle) {
BLEConnection* connection = Bluefruit.Connection(conn_handle);
ble_gap_addr_t address = connection->getPeerAddr();
bond_keys_t peer_bkeys; 
if (!bond_load_keys(BLE_GAP_ROLE_PERIPH, &address, &peer_bkeys)) {
      Serial.println("Not bonded, disconnecting...");
      connection->disconnect();
      return;
  }
//other stuff todo
}
1 Like

Hi there,
Nice approach, I wish this area of the BLE were more accessible, just isn’t the case now unfortunately.
IMO it would make Xiao more powerful for secure devices. a real opportunity for a LIBRARY creator to take that on. (seeed)?

GL :slight_smile: PJ
:v: