Currently using on OH1 Heart rate sensor (tiny.cc/mom03y)
I'm looking to subscribe to HR notification using bluepy. I have got the notifications working but the OH1 device disconnects after about 20-30secs in both bluepy and gatttool (remote user terminated) but not in bluetoothctl.
Looking for a reason why the connection stays alive in bluetoothctl and not in bluepy or gatttool, code and hcidump below, using bluez 5.50 and bluepy 1.30 on rasbian 4.14.
Bluepy
#packet count
packets = 0
class hrCallback(btle.DefaultDelegate):
def __init__(self):
btle.DefaultDelegate.__init__(self)
def handleNotification(self, cHandle, data):
global packets
packets += 1
print("packet: %s Handle: %s HR (bpm): %s " % (packets, cHandle, data[1]))
#connect to OH1
mac = "a0:9e:1a:4f:ef:8b"
oh1 = btle.Peripheral( mac )
oh1.setDelegate( hrCallback() )
#start hr notification
oh1.writeCharacteristic(38, b"\x01\x00", True)
#listen for notifications
while True:
try:
if oh1.waitForNotifications(1.0):
continue
except btle.BTLEDisconnectError:
pass
hcidump
> HCI Event: Command Complete (0x0e) plen 4
LE Set Scan Parameters (0x08|0x000b) ncmd 1
status 0x00
> HCI Event: Command Complete (0x0e) plen 4
LE Set Scan Enable (0x08|0x000c) ncmd 1
status 0x00
> HCI Event: Command Complete (0x0e) plen 4
LE Set Scan Enable (0x08|0x000c) ncmd 1
status 0x00
> HCI Event: Command Status (0x0f) plen 4
LE Create Connection (0x08|0x000d) status 0x00 ncmd 1
> HCI Event: Command Status (0x0f) plen 4
LE Read Remote Used Features (0x08|0x0016) status 0x00 ncmd 1
> HCI Event: Disconn Complete (0x05) plen 4
status 0x00 handle 64 reason 0x13
Reason: Remote User Terminated Connection
Which version of BlueZ are you using? several tools (e.g. gatttool, hcitool, hciconfig) was deprecated and replaced by bluetoothctl and btmgmt, and the recommendation from the BlueZ team is to use the new tools instead. Please have a look at the following link:-
Deprecated BlueZ Tools
The difference between the new tools and the old tools is that the old tools were able of interfacing directly with the kernel, whereas the new tools perform operations by interfacing with D-Bus instead.
Therefore, the recommendation is always to use bluetoothctl as old tools are not maintained and this is likely why you are seeing the issue.
Related
Need to determine whether the central and peripheral devices (both running Bluetooth 5.0) are communicating via the faster LE 2M PHY bit rate.
The command I am sending via the hcitool:
sudo hcitool cmd 8 30 40 0
The response received:
01 30 20 01
The btmon output:
# RAW Open: hcitool (privileged) version 2.22 {0x0005} 3503.199467
# RAW Close: hcitool {0x0005} 3503.199506
# RAW Open: hcitool (privileged) version 2.22 {0x0005} [hci0] 3503.199594
< HCI Command: LE Read PHY (0x08|0x0030) plen 2 #11120 [hci0] 3503.200023
Handle: 64
> HCI Event: Command Complete (0x0e) plen 4 #11121 [hci0] 3503.200353
LE Read PHY (0x08|0x0030) ncmd 1
Status: Unknown HCI Command (0x01)
# RAW Close: hcitool
For some reason I am getting the Unknown HCI Command error when running this command on the RPi 4 which is set as the peripheral via bleno.
Am I doing something wrong? Are there preliminary steps I need to take before I can read and set the symbol rate?
More info:
< HCI Command: Read Local Ve.. (0x04|0x0001) plen 0 #3 [hci0] 42.465863
> HCI Event: Command Complete (0x0e) plen 12 #4 [hci0] 42.466325
Read Local Version Information (0x04|0x0001) ncmd 1
Status: Success (0x00)
HCI version: Bluetooth 5.0 (0x09) - Revision 315 (0x013b)
LMP version: Bluetooth 5.0 (0x09) - Subversion 24857 (0x6119)
Manufacturer: Cypress Semiconductor Corporation (305)
# RAW Close: hcitool {0x0003} [hci0] 42.466507
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!
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.
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
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.
I have a custom nrf52 chip on a pcb with swd pins exposed. I have cloned and installed the latest openocd from https://github.com/ntfreak/openocd. The latest version includes all the latest pathes for the nrf52 chip, so no need for any additional changes as suggested in many older guides online. I am able to connect to the chip using ST-LinkV2. when connected I can read and write memory locations using mdw and mdb. I can also run some basic openocd commands like dump_image e.t.c, which confirms that the setup is good. But halt and program commannds always lead to errors like:
JTAG failure -4
JTAG failure -4
JTAG failure -4
JTAG failure -4
JTAG failure -4
JTAG failure -4
target halted due to debug-request, current mode: Thread
xPSR: 00000000 pc: 00000000 msp: 00000000
jtag status contains invalid mode value - communication failure
Polling target nrf52.cpu failed, trying to reexamine
Examination failed, GDB will be halted. Polling again in 100ms
Previous state query failed, trying to reconnect
jtag status contains invalid mode value - communication failure
Polling target nrf52.cpu failed, trying to reexamine
if I try to use flash image_write I get the error,
JTAG failure
Error setting register
error starting target flash write algorithm
Failed to enable read-only operation
Failed to write to nrf52 flash
error writing to flash at address 0x00000000 at offset 0x00000000
in procedure 'dap'
jtag status contains invalid mode value - communication failure
Polling target nrf52.cpu failed, trying to reexamine
I have read different guides online, and one of the possible solutions involves the APPPROTECT register which has to be disabled to enable any writes to flash.
APP_PROTECT, But the dap commmand which is supposed to help us access this bit,
dap apreg 1 0x04 0x01
returns an error:
invalid subcommand apreg 1 0x04 0x01
Please, I will like to know if anyone has had success programming a new empty nrf52 chip with the stlink-v2 and the steps which are necessary, or if any one has encountered similar problems. Thanks.
Here is my config file:
#nRF52832 Target
source [find interface/stlink.cfg]
transport select hla_swd
source [find target/nrf52.cfg]
#reset_config srst_nogate connect_assert_srst
I solved the "protected nRF52" chip problem this way, on Windows, using a Particle.io Debugger https://store.particle.io/products/particle-debugger setup to program nRF52 chips from Arduino as described in https://www.forward.com.au/pfod/BLE/LowPower/index.html
Note: The recovery process described here does NOT need Arduino to be installed
Download OpenOCD-20181130.7z pre-compiled openocd for windows from
http://gnutoolchains.com/arm-eabi/openocd/
The latest version of openocd src on https://github.com/ntfreak/openocd should also work as it includes the apreg cmd in target\arm_adi_v5.c
unzip, open cmd prompt to unzip dir, enter cmd
bin\openocd.exe -d2 -f interface/cmsis-dap.cfg -f target/nrf52.cfg
response
Info : auto-selecting first available session transport "swd". To override use '
transport select <transport>'.
adapter speed: 1000 kHz
cortex_m reset_config sysresetreq
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : CMSIS-DAP: SWD Supported
Info : CMSIS-DAP: FW Version = 1.10
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 0 TDO = 0 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : clock speed 1000 kHz
Info : SWD DPIDR 0x2ba01477
Error: Could not find MEM-AP to control the core
Info : Listening on port 3333 for gdb connections
Open telnet program eg teraTerm and connect to localhost on port 4444, i.e. 127.0.0.1 telnet port 4444
cmd window shows
Info : accepting 'telnet' connection on tcp/4444
in telnet (i.e. teraTerm) type
nrf52.dap apreg 1 0x04
returns 0 <<< chip protected
then
nrf52.dap apreg 1 0x04 0x01
then
nrf52.dap apreg 1 0x04
returns 1 << chip un-protected
then power cycle board
Can now use arduino ide to flash softdevice and code low power BLE
Even though the dap command is listed by openOCD help, it isn't implemented for transport hla_swd that you have to use with ST-Link.
If the ST-Link is a generic type from China, it can be upgraded to CMSIS-DAP which uses the swd transport and supports the nrf52.dap apreg 1 0x04 0x01 command to disable the readback protection and erase the flash. You'll need another ST-Link to do that, or you can instead install CMSIS-DAP on a generic STM32F103C8T6 board.
After that you can either use ST-Link to program the nRF52 or continue using CMSIS-DAP, which can also be used to program STM32 MCU.
Nucleo board embedded ST-Links can also be upgraded to J-Link, which allow the use of the "recover" option in nRFgo Studio to erase the flash, it should also work with "nrfjtool --recover" or OpenOCD.
If anyone encounters this problem, I solved the problem by getting an original Jlink-Edu. I also had to pull the reset pin of the microcontroller high to get the jlink working.
There are lots of JTAG messages.
I think you might be missing the
transport select hla_swd
line in your (board) cfg file. The NRF5x chips only work properly with SWD, and ST-Link uses the hla_swd variant.
I'm attempting to connect a bluez peripheral to an iOS device; it's not working.
Please find below logs on peripheral side. I'm advertising using hciconfig hci0 leadv but I don't see any "Connect" option on Scanner applications on phone. Also bluez-4.101 doesn't have hciconfig hci0 leadv0 option to make it connectable.
With the same device acting as a Central I am able to make LE connections with other BLE devices, so that confirms kernel LE support and device is fine, only some bluez issues I assume are there.
> HCI Event: LE Meta Event (0x3e) plen 19
LE Connection Complete
status 0x00 handle 1025, role slave
bdaddr 67:5D:F6:87:3D:2C (Random)
> ACL data: handle 1025 flags 0x02 dlen 7
ATT: MTU req (0x02)
client rx mtu 158
> ACL data: handle 1025 flags 0x02 dlen 27
> ACL data: handle 1025 flags 0x01 dlen 27
> ACL data: handle 1025 flags 0x01 dlen 9
L2CAP(d): cid 0x003a len 59 [psm 0]
0000: .9......com.appl
0010: e.BT.TS."....com
0020: .apple.BTLEServe
0030: r.classic..
> ACL data: handle 1025 flags 0x02 dlen 11
L2CAP(d): cid 0x003a len 7 [psm 0]
0000: ......
> HCI Event: Disconn Complete (0x05) plen 4
status 0x00 handle 1025 reason 0x13
Reason: Remote User Terminated Connection
How can I get this peripheral to connect?
Upgrading to the latest BlueZ version will solve the GATT related issues.
Quote from the linked page:
The 2.x , 3.x and 4.x series of libraries and packages are deprecated and not supported anymore by BlueZ developers. If you are using them please update to the 5.x series. The download link is only provided for reference.
This problem only occurs after I updated to Bluez-5.20+ (or maybe earlier)
I compiled Bluez with maintainer mode and experimental to get gatt-example included in the service list, and enabled the advertisement via hciconfig.
Every time I connect with iOS (with app like LightBlue, etc.), I got disconnected after this event :
> HCI Event: LE Meta Event (0x3e) plen 19 [hci0] 48449.282018
LE Connection Complete (0x01)
Status: Success (0x00)
Handle: 64
Role: Slave (0x01)
Peer address type: Random (0x01)
Peer address: 7A:A0:D6:50:69:CE (Resolvable)
Connection interval: 30.00 msec (0x0018)
Connection latency: 0.00 msec (0x0000)
Supervision timeout: 720 msec (0x0048)
Master clock accuracy: 0x05
# Device Connected: 7A:A0:D6:50:69:CE (2) flags 0x0000
> HCI Event: LE Meta Event (0x3e) plen 13 [hci0] 48449.356021
LE Long Term Key Request (0x05)
Handle: 64
Random number: be7ee4252ef5d67f
Encryption diversifier: 0x57f5
< HCI Command: LE Long Term Key Request Reply (0x08|0x001a) plen 18 [hci0] 48449.356058
Handle: 64
Long term key: 696469e1eaa559f7707643bf410ab39b
> HCI Event: Command Complete (0x0e) plen 6 [hci0] 48449.357019
LE Long Term Key Request Reply (0x08|0x001a) ncmd 1
Status: Success (0x00)
Handle: 64
> HCI Event: Encryption Change (0x08) plen 4 [hci0] 48449.506019
Status: Success (0x00)
Handle: 64
Encryption: Enabled with AES-CCM (0x01)
> ACL Data RX: Handle 64 flags 0x02 dlen 7 [hci0] 48449.594893
ATT: Exchange MTU Request (0x02) len 2
Client RX MTU: 158
< HCI Command: Disconnect (0x01|0x0006) plen 3 [hci0] 48453.289196
Handle: 64
Reason: Remote User Terminated Connection (0x13)
> HCI Event: Command Status (0x0f) plen 4 [hci0] 48453.289992
Disconnect (0x01|0x0006) ncmd 1
Status: Success (0x00)
> HCI Event: Disconnect Complete (0x05) plen 4 [hci0] 48453.345991
Status: Success (0x00)
Handle: 64
Reason: Connection Terminated By Local Host (0x16)
And bluetoothd debug log :
bluetoothd[3381]: src/adapter.c:resume_discovery()
bluetoothd[3381]: src/adapter.c:connected_callback() hci0 device 7A:A0:D6:50:69:CE connected eir_len 0
bluetoothd[3381]: src/device.c:device_create() dst 7A:A0:D6:50:69:CE
bluetoothd[3381]: src/device.c:device_new() address 7A:A0:D6:50:69:CE
bluetoothd[3381]: src/device.c:device_new() Creating device /org/bluez/hci0/dev_7A_A0_D6_50_69_CE
bluetoothd[3381]: src/attrib-server.c:connect_event()
bluetoothd[3381]: src/device.c:device_create() dst 7A:A0:D6:50:69:CE
bluetoothd[3381]: src/device.c:device_new() address 7A:A0:D6:50:69:CE
bluetoothd[3381]: src/device.c:device_new() Creating device /org/bluez/hci0/dev_7A_A0_D6_50_69_CE
bluetoothd[3381]: Unable to register device interface for 7A:A0:D6:50:69:CE
bluetoothd[3381]: src/device.c:device_free() 0x11dd220
bluetoothd[3381]: src/adapter.c:dev_disconnected() Device 7A:A0:D6:50:69:CE disconnected, reason 2
bluetoothd[3381]: src/adapter.c:adapter_remove_connection()
bluetoothd[3381]: src/adapter.c:adapter_remove_connection() Removing temporary device /org/bluez/hci0/dev_7A_A0_D6_50_69_CE
bluetoothd[3381]: src/device.c:device_remove() Removing device /org/bluez/hci0/dev_7A_A0_D6_50_69_CE
bluetoothd[3381]: src/device.c:btd_device_unref() Freeing device /org/bluez/hci0/dev_7A_A0_D6_50_69_CE
bluetoothd[3381]: src/device.c:device_free() 0x11e9720
bluetoothd[3381]: plugins/policy.c:disconnect_cb() reason 2
Any possible explanation to this?