How to set BLE connection interval?
SO,how to set BLE connection interval?
when the devices are connected,i want to save more power,so i want to increase the conncetion interval ,but i can't find how to redefine this param.
adv and scan interval is easily to set,i can't find the way to set connnection interval
i notice that IRQ has this event:
elif event == _IRQ_CONNECTION_UPDATE:
conn_handle, conn_interval, conn_latency, supervision_timeout, status = data
how to update connection,to change conn_interval, conn_latency, supervision_timeout
BLE().gap_connect() call results in "TypeError: function doesn't take keyword arguments" (ESP32-WROOM-32D)
Hi everyone,
I am calling the following couple of lines in my micropython code:
ble = bluetooth.BLE() ble.gap_connect(addr_type, addr, max_conn_interval_us=5000)
My intention is to increase throughput and according to the docs, this is a possible way to do that (at the cost of higher power consumption, which is acceptable for me).
Unfortunately, I receive an error message saying "TypeError: function doesn't take keyword arguments".
And indeed, when just calling ble.gap_connect(addr_type, addr) everything works just fine, but the reaction time is slow to my taste (around 100ms, so, improvable)..
My board is an ESP32-WROOM-32D (DFRobot DFR0575 board) running MicroPython V1.19.
I have read in other issues that some functions switched to positional arguments entirely, but entering more than three positional arguments (like ble.gap_connect(addr_type, addr, 2000, 0, 5000), the 2000 in representing scan_duration_ms, 0 representing min_conn_interval_us, and 5000 my desired value for max_conn_interval_us, referring to the keywords mentioned in the online doc, https://docs.micropython.org/en/latest/library/bluetooth.html, section "Central Role") results in OSError: -17. The only exception to this is if one only gives one value (ble.gap_connect(addr_type, addr, 2000)), but then I wonder, which variable I assign this value then?
Here's a (not exactly minimal) MWE that replicates the issue connecting to the MicroPython kernel with JupyterNotebook.
When trying this on your machine, make sure to change the target device address according to your needs. Also, if there are a lot of BLE devices around, I have observed that saving all these devices the "discovered_devices" dictionary is more than what my ESP32 board can handle. Filtering your detected devices by the target address would fix that.
A tip to find the address of the device you want to send to is to use an app on your smartphone (for example the nRF connect app from nordic) to identify your device and type its address into the code below.
%serialconnect to --port=COM14 --baud=115200
import bluetooth
import time
import binascii
_IRQ_SCAN_RESULT = const(5)
_IRQ_SCAN_DONE = const(6)
_IRQ_PERIPHERAL_CONNECT = const(7)
discovered_device_addr_list = list()
discovered_devices = dict()
current_conn_handle = 0
current_addr_type = 0
current_addr = 0
def raw_as_hex_bytes(raw_data):
x = binascii.hexlify(raw_data).decode()
return [x[2*(k-1):2*k] for k in range(1,len(x)//2 + 1)]
def irqhandler(event, data):
global current_conn_handle
global current_addr_type
global current_addr
if event == _IRQ_SCAN_RESULT:
addr_type, addr, adv_type, rssi, adv_data = data
addr_hex = ':'.join(raw_as_hex_bytes(addr))
# add the scanned device to the devices dictionary
if not addr_hex in discovered_device_addr_list:
adv_data_hex = ''.join(raw_as_hex_bytes(adv_data))
discovered_device_addr_list.append(addr_hex)
discovered_devices[addr_hex] = {
'addr_type': addr_type,
'addr': binascii.unhexlify(addr_hex.replace(':','')), #addr,
'addr_hex': addr_hex,
'adv_type': adv_type,
'rssi': rssi,
'adv_data': adv_data,
'adv_data_hex': adv_data_hex,
'adv_name': None,
'device_nbr': len(discovered_device_addr_list),
}
print('Found device', discovered_devices[addr_hex]['device_nbr'], ':', addr_hex)
elif event == _IRQ_SCAN_DONE:
print('Scan ended.')
elif event == _IRQ_PERIPHERAL_CONNECT:
# Connect successful.
current_conn_handle, current_ddr_type, current_addr = data
print('Connected.')
ble = bluetooth.BLE()
ble.active(True)
ble.irq(irqhandler)
ble.gap_scan(2000, 30000, 30000)
time.sleep_ms(1000)
ble.gap_scan(None)
target_addr_hex = 'e8:7c:85:7e:a9:34' #<- change to your device address
if target_addr_hex in discovered_device_addr_list:
target_device = discovered_devices[target_addr_hex]
target_addr_type = target_device['addr_type']
target_addr = target_device['addr']
print('Try connecting to ', target_addr_hex, '...')
#ble.gap_connect(target_addr_type, target_addr, max_conn_interval_us=5000) #<- fails
#ble.gap_connect(target_addr_type, target_addr, 2000, 0, 5000) #<- fails
#ble.gap_connect(target_addr_type, target_addr, 5000) #<- works, but what does 5000 specify?
ble.gap_connect(target_addr_type, target_addr) #<- works, but the latency of data transmission is suboptimal
time.sleep_ms(2000)
ble.gap_disconnect(current_conn_handle)