I'm trying the esp32-based ubluetooth library. The example on github is based on the bluetooth library and is not an example of a Central Role (GATT Client).
Code: Select all
BLE.gap_connect (addr_type, addr, scan_duration_ms s 2000)
Thanks.
Code: Select all
BLE.gap_connect (addr_type, addr, scan_duration_ms s 2000)
Code: Select all
from micropython import const
_IRQ_SCAN_RESULT = const(1 << 4)
_IRQ_SCAN_COMPLETE = const(1 << 5)
def adv_decode(adv_type, data):
i = 0
while i + 1 < len(data):
if data[i + 1] == adv_type:
return data[i + 2:i + data[i] + 1]
i += 1 + data[i]
return None
def adv_decode_name(data):
n = adv_decode(0x09, data)
if n:
return n.decode('utf-8')
return data
def bt_irq(event, data):
if event == _IRQ_SCAN_RESULT:
# A single scan result.
addr_type, addr, connectable, rssi, adv_data = data
print(addr_type, addr, adv_decode_name(adv_data))
elif event == _IRQ_SCAN_COMPLETE:
# Scan duration finished or manually stopped.
print('scan complete')
# Scan for 10s (at 100% duty cycle)
ble.gap_scan(10000, 30000, 30000)
I try this code:jimmo wrote: ↑Fri Nov 08, 2019 10:21 amhi,
Yes, sorry, I haven't gotten around to the central role examples yet.
By far the easiest way is to do a scan, and use the address type and address returned during the scan.
Code: Select all
from micropython import const _IRQ_SCAN_RESULT = const(1 << 4) _IRQ_SCAN_COMPLETE = const(1 << 5) def adv_decode(adv_type, data): i = 0 while i + 1 < len(data): if data[i + 1] == adv_type: return data[i + 2:i + data[i] + 1] i += 1 + data[i] return None def adv_decode_name(data): n = adv_decode(0x09, data) if n: return n.decode('utf-8') return data def bt_irq(event, data): if event == _IRQ_SCAN_RESULT: # A single scan result. addr_type, addr, connectable, rssi, adv_data = data print(addr_type, addr, adv_decode_name(adv_data)) elif event == _IRQ_SCAN_COMPLETE: # Scan duration finished or manually stopped. print('scan complete') # Scan for 10s (at 100% duty cycle) ble.gap_scan(10000, 30000, 30000)
Code: Select all
from ubluetooth import BLE, UUID, FLAG_NOTIFY, FLAG_READ, FLAG_WRITE
from micropython import const
_IRQ_SCAN_RESULT = const(1 << 4)
_IRQ_SCAN_COMPLETE = const(1 << 5)
def adv_decode(adv_type, data):
i = 0
while i + 1 < len(data):
if data[i + 1] == adv_type:
return data[i + 2:i + data[i] + 1]
i += 1 + data[i]
return None
def adv_decode_name(data):
n = adv_decode(0x09, data)
if n:
return n.decode('utf-8')
return data
def bt_irq(event, data):
if event == _IRQ_SCAN_RESULT:
# A single scan result.
addr_type, addr, connectable, rssi, adv_data = data
print(addr_type, addr, adv_decode_name(adv_data))
elif event == _IRQ_SCAN_COMPLETE:
# Scan duration finished or manually stopped.
print('scan complete')
# Scan for 10s (at 100% duty cycle)
bt = BLE()
bt.active(True)
bt.gap_scan(10000, 30000, 30000)
Code: Select all
bt.irq(handler=br_irq)
Code: Select all
>>> %Run -c $EDITOR_CONTENT
>>> 0 b'\xf4^\xab\x91k\x97' b'\x02\x01\x06\x05\x02\r\x18\x0f\x18'
0 b'\xf4^\xab\x91k\x97' b'\x02\x01\x06\x05\x02\r\x18\x0f\x18'
0 b'\xf4^\xab\x91k\x97' b'\x02\x01\x06\x05\x02\r\x18\x0f\x18'
0 b'\xf4^\xab\x91k\x97' b'\x02\x01\x06\x05\x02\r\x18\x0f\x18'
0 b'\xf4^\xab\x91k\x97' b'\x02\x01\x06\x05\x02\r\x18\x0f\x18'
0 b'\xf4^\xab\x91k\x97' b'\x02\x01\x06\x05\x02\r\x18\x0f\x18'
0 b'\xf4^\xab\x91k\x97' b'\x02\x01\x06\x05\x02\r\x18\x0f\x18'
0 b'\xf4^\xab\x91k\x97' b'\x02\x01\x06\x05\x02\r\x18\x0f\x18'
0 b'\xf4^\xab\x91k\x97' b'\x02\x01\x06\x05\x02\r\x18\x0f\x18'
Code: Select all
elif event == _IRQ_SCAN_RESULT:
# A single scan result.
addr_type, addr, connectable, rssi, adv_data = data
Code: Select all
#2019-11-09
from ubluetooth import BLE, UUID, FLAG_NOTIFY, FLAG_READ, FLAG_WRITE
from micropython import const
_IRQ_SCAN_RESULT = const(1 << 4)
_IRQ_SCAN_COMPLETE = const(1 << 5)
_IRQ_PERIPHERAL_CONNECT = const(1 << 6)
_IRQ_PERIPHERAL_DISCONNECT = const(1 << 7)
_IRQ_GATTC_SERVICE_RESULT = const(1 << 8)
_IRQ_GATTC_CHARACTERISTIC_RESULT = const(1 << 9)
_IRQ_GATTC_DESCRIPTOR_RESULT = const(1 << 10)
_IRQ_GATTC_READ_RESULT = const(1 << 11)
_IRQ_GATTC_WRITE_STATUS = const(1 << 12)
def adv_decode(adv_type, data):
i = 0
while i + 1 < len(data):
if data[i + 1] == adv_type:
return data[i + 2:i + data[i] + 1]
i += 1 + data[i]
return None
def adv_decode_name(data):
n = adv_decode(0x09, data)
if n:
return n.decode('utf-8')
return data
def bt_irq(event, data):
if event == _IRQ_SCAN_RESULT:
# A single scan result.
addr_type, addr, connectable, rssi, adv_data = data
print(addr_type, addr, connectable,adv_decode_name(adv_data))
elif event == _IRQ_SCAN_COMPLETE:
# Scan duration finished or manually stopped.
print('scan complete')
elif event == _IRQ_PERIPHERAL_CONNECT:
# A successful gap_connect().
conn_handle, addr_type, addr = data
print('conn_handle','addr_type','addr')
print(conn_handle,addr_type,addr)
print('conn complete')
elif event == _IRQ_PERIPHERAL_DISCONNECT:
# Connected peripheral has disconnected.
conn_handle, addr_type, addr = data
elif event == _IRQ_GATTC_SERVICE_RESULT:
# Called for each service found by gattc_discover_services().
conn_handle, start_handle, end_handle, uuid = data
print(start_handle, end_handle)
print('GATTC_SERVICE ok')
elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT:
# Called for each characteristic found by gattc_discover_services().
conn_handle, def_handle, value_handle, properties, uuid = data
print(value_handle,uuid)
elif event == _IRQ_GATTC_DESCRIPTOR_RESULT:
# Called for each descriptor found by gattc_discover_descriptors().
conn_handle, dsc_handle, uuid = data
elif event == _IRQ_GATTC_READ_RESULT:
# A gattc_read() has completed.
conn_handle, value_handle, char_data = data
print(value_handle, char_data)
elif event == _IRQ_GATTC_WRITE_STATUS:
# A gattc_write() has completed.
conn_handle, value_handle, status = data
# Scan for 10s (at 100% duty cycle)
bt = BLE()
bt.active(True)
bt.irq(handler=bt_irq)
#bt.gap_scan(10000, 30000, 30000)
bt.gap_connect(0, b'\xf4^\xab\x91k\x97', 2000)
bt.gattc_discover_services(0)
Code: Select all
MicroPython v1.11-571-g7e374d231 on 2019-11-08; ESP32 module with ESP32
Type "help()" for more information.
>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
File "<stdin>", line 73, in <module>
OSError: [Errno 107] ENOTCONN
>>> conn_handle addr_type addr
0 0 b'\xf4^\xab\x91k\x97'
conn complete
Code: Select all
bt.gattc_discover_services(conn_handle)