Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    BLE Features - non-blocking connects, char presentation format

    Scheduled Pinned Locked Moved WiPy 2&3
    bluetoothble
    1 Posts 1 Posters 955 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S Offline
      semireg
      last edited by semireg

      After working on adding a callback on characteristic subscription I've got a few more feature requests for the BLE API. I might attempt these myself... but first I'm looking to make sure that they are achievable given other's knowledge of the underlying ESP-32 API.

      1. ✅ Read a characteristic's presentation format descriptor. I created a pull request for this feature. However, it could use a second pair of eyes to make sure this is a good approach for future extensibility.

      2. ✅ Non-blocking connect with configurable timeout. I've got this working. Looks like "background auto connection" is a red herring. I can't find any documentation on it, and setting is_direct to false never connects. A quick repl paste below shows how it works. I created a pull request that needs review, testing, etc.

      3. ✅ Making sure scan response data is available in the advertisement. Looking into this possibly related issue and associated ESP-idf pull request. Update: I have confirmed that the current WiPy firmware is affected by this issue. Implementing sizeof instead of ESP_BLE_ADV_DATA_LEN_MAX allows for the parsing of the full scan response. I created a pull request for this fix.

      As you may have gathered, these are on the GATTC side of the API.

      #2 REPL paste...

      Vanilla async_connect:

      paste mode; Ctrl-C to cancel, Ctrl-D to finish
      === from network import Bluetooth
      === ble = Bluetooth()
      === import binascii
      === 
      === def conn_cb (bt_o):
      ===     print('conn_cb events {}'.format(bt_o.events()))
      === 
      === ble.callback(trigger=Bluetooth.SERVER_ASYNC_CONNECTED, handler=conn_cb)
      === 
      === ble.start_scan(-1)
      === c = ble.async_connect(binascii.unhexlify('12345678ABCD'))
      === 
      bt_async_connect: creating connection object
      >>> 
      >>> 
      >>> c.isconnected()
      False
      >>> c.isconnected()
      False
      >>> 
      >>> 
      >>> 
      >>> 
      >>> gattc_events_handler: ESP_GATTC_CONNECT_EVT
      gattc_events_handler: loop 
      gattc_events_handler: found match
      gattc_events_handler: async_disconnect false: sending callback
      conn_cb events 262
      
      >>> c.isconnected()
      True
      >>> 
      >>> 
      >>> c.disconnect()
      >>> c.isconnected()
      False
      

      Then, immediately afterwards in the same REPL, a demonstration of cancelling a connection.

      >>> c = ble.async_connect(binascii.unhexlify('12345678ABCD'))
      bt_async_connect: creating connection object
      >>> c.disconnect()
      >>> c.isconnected()
      False
      >>> 
      >>> 
      >>> 
      >>> close_connection: async_disconnect: truegattc_events_handler: ESP_GATTC_CONNECT_EVT
      gattc_events_handler: loop 
      gattc_events_handler: found match
      gattc_events_handler: async_disconnect true: closing
      
      >>> 
      >>> c.isconnected()
      False
      

      This works for my use case. I couldn't find a better way to effectively cancel esp_ble_gattc_open. Again, check out the pull request.

      Here's a version that's easily pastable via ctrl-e:

      from network import Bluetooth
      ble = Bluetooth()
      import binascii
      
      def conn_cb (bt_o):
      	print('--- conn_cb events {}'.format(bt_o.events()))
      
      ble.callback(trigger=Bluetooth.SERVER_ASYNC_CONNECTED, handler=conn_cb)
      
      ble.start_scan(-1)
      c = ble.async_connect(binascii.unhexlify('12345678ABCD'))
      
      1 Reply Last reply Reply Quote 1

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      • 1 / 1
      • First post
        Last post

      Pycom on Twitter

      Pycom