4

I would like to disable the 'Filter duplicates' setting on bluetooth controller level with the HCI Command 'LE Set Scan Enable' using the D-Bus Interface of bluetoothd.

I already tried to set the 'DuplicateData' parameter from SetDiscoveryFilter(org.bluez.Adapter1), but according to btmon this doesn't change the value of 'Filter duplicates' for LE Set Scan Enable. I also read the man pages for 'bluetoothd' and 'main.conf' with no success.

By contrast I found that a 'hcitool lescan --duplicates' does the trick.

Any pointers would be greatly appreciated!

Risto
  • 983
  • 7
  • 14

3 Answers3

2

One issue with scanning using bluez is that the current Linux kernel support always turns on de-duplication of advertisements. See this thread in the linux-bluetooth mailing list. - dhalbert's comment on GitHub

Even if you perform

sudo bluetoothctl
menu scan
duplicate-data on

or pass {"DuplicateData": true} to D-Bus API SetDiscoveryFilter(), kernel driver will always send the following HCI command when the scanning is started:

< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2
        Scanning: Enabled (0x01)
        Filter duplicates: Enabled (0x01)

Using hcitool lescan --duplicates bypasses kernel interpretation of BT MGMT command and send proper HCI command (Filter duplicates: Disabled (0x00)).

My workaround, as explained in comment on GitHub is the following (source of hci_le_set_scan_enable command):

# This is executed every time after 'scan on' is executed in bluetoothctl.
# First, disable ongoing scan enabled by bluetoothctl - if this is not executed
# then next command to enable scanning will result in Command Disallowed (0x0c)
# status. Fortunatelly, bluetoothctl seems not to be bothered by execution of
# this commands.
hcitool cmd 0x08 0x000C 0x00 0x00
# This results in
# < HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2
#         Scanning: Disabled (0x00)
#         Filter duplicates: Disabled (0x00)
# > HCI Event: Command Complete (0x0e) plen 4
#       LE Set Scan Enable (0x08|0x000c) ncmd 1
#         Status: Success (0x00)
# Now, enable scanning with duplicate filtering disabled
hcitool cmd 0x08 0x000C 0x01 0x00
# This results in
# < HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2
#         Scanning: Enabled (0x01)
#         Filter duplicates: Disabled (0x00)
# > HCI Event: Command Complete (0x0e) plen 4
#       LE Set Scan Enable (0x08|0x000c) ncmd 1
#         Status: Success (0x00)
# and bluetoothctl now reports all packets, as if the 'duplicate-data on'
# actually works as expected. Note: 'duplicate-data on' shall still be 
# executed to prevent additional layer of filtering in bluetoothd itself.

For other workaround using hcitool lescan --duplicates + hciump, check Adafruit_Blinka_bleio.

Note, however, that both workarounds require elevated privileges.

Bojan P.
  • 942
  • 1
  • 10
  • 21
1

Thank you very much for your answer. I tried the bluetoothctl command on bluez 5.48 and 5.50 and get the same result as with my D-Bus application. Regardless of the 'duplicate-data' setting (on/off), btmon/HCI always shows 'Filter duplicates: Enabled' on 'scan on'

< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2      #5 [hci0] 10.895438
        Scanning: Enabled (0x01)
        Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4                 #6 [hci0] 10.898311
      LE Set Scan Enable (0x08|0x000c) ncmd 2
        Status: Success (0x00)

What really puzzles me is that disabling LE scan ('scan off') also disables filter duplicates ... :-(

< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2     #21 [hci0] 14.969999
        Scanning: Disabled (0x00)
        Filter duplicates: Disabled (0x00)
> HCI Event: Command Complete (0x0e) plen 4                #22 [hci0] 14.973667
      LE Set Scan Enable (0x08|0x000c) ncmd 2
        Status: Success (0x00)

After reading doc/adapter-api.txt several times I assume that 'DuplicateData' filter is meant to apply to bluez itself and not to the Bluetooth hardware, but I might be wrong

Risto
  • 983
  • 7
  • 14
  • Yes, of course. Data duplication filter is a higher level feature and not an HCI or baseband level feature. This is why if you are using btmon you will always see the data. This is because the baseband is still receiving the advertising packets from the remote device, and the HCI layer is still seeing the events. Filter duplicate feature is when in the app layer (i.e. bluetoothctl) when you see adverts coming from the same device, you suppress the extra adverts so that you only see the advertising device once. – Youssif Saeed Apr 02 '19 at 06:32
  • Thanks for the clarification! Do you know any way to change the "Filter duplicates" value for HCI/baseband with 'bluetoothd', like it is possible with 'hcitool'? – Risto Apr 02 '19 at 11:56
  • Actually I take it back, I double checked the spec and in Version 5.1 Vol 2 Part E Section 7.8.11 indicates that you should be able to apply filter duplicates to the HCI layer. I will have another look at this and try to test it out myself with bluetoothctl. – Youssif Saeed Apr 02 '19 at 12:24
  • @YoussifSaeed sorry to bother you, but have you had time to look at this issue or any tips on how to proceed? – Risto Jun 04 '19 at 08:21
  • The same issue still persists using BlueZ version 5.55. On Raspberry Pi (RPi OS Buster) `Filter duplicates: Enabled (0x01)` is used despite `duplicate-data on` and packets are not reported - but `hcitool lescan --duplicates` works properly and shows all packets. Interestingly, this is not the case on my ThinkPad laptop (BlueZ either installed from apt or compiled from repository, tag 5.55). – Bojan P. Oct 14 '20 at 20:58
0

Welcome to StackOverflow. When posting questions, it is very useful to post the version of the software and hardware used as this can help you get a better answer.

Regarding your question, this depends on the version of BlueZ you're using. Assuming that this is the current latest version (v5.50), then there is an option for Low Energy scanning to disable duplicating filters. Please have a look at the documentation here:-

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt#n107

You can also view this being used in the bluetoothctl command. Please have a look at this:-

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/client/main.c#n1390

If you want to try this out, you can use the bluetoothctl command as follows:-

#bluetoothctl
[bluetoothctl] menu scan
[bluetoothctl] duplicate-data on
[bluetoothctl] back
[bluetoothctl] scan on

This will return the adverts only once and duplicate adverts will be suppressed.

I hope this helps.

Youssif Saeed
  • 11,789
  • 4
  • 44
  • 72
  • `duplicate-data on` sets `SetDiscoveryFilter({"DuplicateData": true})` under the hood. And it does not help, even on version 5.55, setting `duplicate-data on` does not change behaviour and `< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2; Filter duplicates: Enabled (0x01)` is still sent. – Bojan P. Oct 14 '20 at 20:54