Skip to content

Conversation

@jaguilar
Copy link
Contributor

@jaguilar jaguilar commented Jan 15, 2026

Please ignore the whitespace changes. I'll revert them when I get a chance. They weren't intended.

This has been tested with the example programs in pybricks/pybricks-projects#100

I expect some changes will be required for this pull request, but I'm putting it out there to demonstrate I have something working and as a jumping off point for further discussion.

(We will also need to decide exactly what functionality will be #ifdef'd out. If we've decided to put classic on all of the hubs that use btstack we could just remove some ifdefs and things would build.)

TODO

  • Revert unnecessary whitespace changes/fix formatting.
  • Add socket resets to soft reset when user program ends.
  • Add cancellation support.
  • Convert rfcomm lib to make the whole thing an object. Add context manager support.
  • General cleanup.
  • Consider moving to the style where we process the events leading up to connection linearly inside connection process functions, similar to inquiry scan? Per discussion with @laurensvalk we will defer this if we do it at all.
  • Consider changing python API to support direct awaiting? (Current API is more efficiently used with asyncio library, which we don't currently have but could have if we wanted.)
  • Fix build/decide what to do about ifdefs.

@BertLindeman
Copy link
Contributor

James,

Trying my EV3 with this firmware on the tankbot_rc
The program runs up to the rfcomm_listen and waits.
Log:

Local address:  A0:E6:F8:E4:42:36
Waiting for connection...
Loaded 0 link keys from settings
[btc:rfcomm_listen] Listening for incoming RFCOMM connections...
sm.c.720: GAP Random Address Update due
sm.c.711: gap_random_address_trigger, state 0
HCI out packet type: 01, len: 3
HCI in packet type: 04, len: 2
HCI in packet type: 04, len: 6
HCI out packet type: 01, len: 9
HCI in packet type: 04, len: 2
HCI in packet type: 04, len: 6
The program was stopped (SystemExit).

What kind of device did you use as RC?
Or do I need 2 EV3brcks for this?

Bert

@jaguilar
Copy link
Contributor Author

jaguilar commented Jan 15, 2026

Hi, @BertLindeman! So kind of you to try this. You do need some sort of client to talk to the tankbot, be it another EV3 or a PC. If you have two EV3s, the client program here is designed to implement this LEGO model. If you run the program on another brick it should connect.

If you want to try connecting from a computer, you'll need a computer with bluetooth. I believe you should be able to connect in Python with something like:

import socket
sock = socket.socket(family=socket.AF_BLUETOOTH)
await sock.connect('XX:XX:XX:XX:XX:XX') # The address printed on the terminal by the tankbot.

You would send messages to the socket that are packed with the struct module -- the code is very short so you should be able to see the desired format.

This class is useful for rfcomm communication,
specifically the readinto function.
This implements the complete RFCOMM socket API, including listen,
connect, send and recv. This does not contain python bindings for the
API.

Tested via manual ping-pong testing for both listen and connect, against
a Windows desktop. EV3<->EV3 not yet tested.
@jaguilar jaguilar force-pushed the ev3-bluetooth-rfcomm branch 2 times, most recently from 707a76a to 23477a9 Compare January 15, 2026 17:51
This commit adds support for rfcomm socket disconnection and
cancellation. It also unifies some of the error checking in the
rfcomm listen and connect functions in btstack.
Fix assumption that umm_malloc is present.
@jaguilar jaguilar force-pushed the ev3-bluetooth-rfcomm branch from 23477a9 to 29f1254 Compare January 15, 2026 20:34
@jaguilar
Copy link
Contributor Author

FYI, this is building fine locally. Not sure what the deal is with CI. Possibly because the build state is not good at certain intermediate commits. I can squash whenever it is so desired.

@BertLindeman
Copy link
Contributor

Used firmware ('ev3', '4.0.0b3', 'ci-build-4625-v4.0.0b3-114-gab4df866 on 2026-01-15')
on Windows 11 25H2.

With EV3 program:#!/usr/bin/env pybricks-micropython # simple tankbot program to test Bluetooth classic connection from pybricks.hubs import EV3Brick from pybricks.ev3devices import Motor, GyroSensor from pybricks.parameters import Port, Direction, Button, Color from pybricks.tools import StopWatch, wait, run_task from pybricks.robotics import DriveBase from pybricks.messaging import rfcomm_listen, local_address from micropython import const import ustruct from pybricks import version # Initialize the EV3 brick. ev3 = EV3Brick() print(version) left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE) right_motor = Motor(Port.D, Direction.COUNTERCLOCKWISE) WHEEL_DIAMETER = 54 AXLE_TRACK = 200 robot = DriveBase(left_motor, right_motor, WHEEL_DIAMETER, AXLE_TRACK) SPEED_SCALE = 6 TURN_SCALE = 2 # Storage for incoming messages from remote control. msg_buf = bytearray(2) msg_buf_view = memoryview(msg_buf) # Tracks the next-to-be-filled index in msg_buf. cur_idx = 0 # keep the local address out of the loop. It will not change. addr = local_address() print("Local address:", addr) async def main(): # light red on listening for a connection ev3.light.on(Color.RED) print('Waiting for connection...') try: conn = await rfcomm_listen() print('Connected!') # light green on connection ev3.light.on(Color.GREEN) except KeyboardInterrupt: conn.close() wait(200) raise SystemExit(0) timeout = StopWatch() cur_idx = 0 try: while timeout.time() < 2000: # Do not kame the wait too short cur_idx += conn.readinto(msg_buf_view[cur_idx:], len(msg_buf) - cur_idx) if cur_idx != len(msg_buf): # We were not able to read the entire message. Loop again. await wait(1) continue timeout.reset() axis1, axis2 = ustruct.unpack('>bb', msg_buf) cur_idx = 0 if axis1 == axis2 == -127: # stop connection print("\tGot stop signal from client") break speed = axis2 * SPEED_SCALE # -768 to +768 mm/s turn_rate = axis1 * TURN_SCALE # -320 to +320 deg/s robot.drive(speed, turn_rate) except OSError: print("\tRFCOMM error") except KeyboardInterrupt: print("\tGot keyboard interrupt") pass finally: robot.stop() # make sure to socket and channel are cleaned up conn.close() wait(200) # wait a bit to give the close some time. print('\tIn finally: Closed connection') run_task(main())
And Windows program:
import socket
import struct
import time

ADDR = "A0:E6:F8:E4:42:36"
CHANNEL = 1

def get_key():
    import msvcrt
    if msvcrt.kbhit():
        return msvcrt.getch().decode("ascii").lower()
    return None

sock = socket.socket(
    socket.AF_BLUETOOTH,
    socket.SOCK_STREAM,
    socket.BTPROTO_RFCOMM
)

STD_MOTOR_SCALE = 20
try:
    sock.connect((ADDR, CHANNEL))
    sock.settimeout(2.0)
    print("Connected. W/S/A/D to drive, space=stop, Q=quit")

    axis1 = 0  # turn
    axis2 = 0  # speed

    while True:
        key = get_key()

        if key == 'w':
            axis2 = STD_MOTOR_SCALE
        elif key == 's':
            axis2 = -STD_MOTOR_SCALE
        elif key == 'a':
            axis1 = -STD_MOTOR_SCALE
        elif key == 'd':
            axis1 = STD_MOTOR_SCALE
        elif key == ' ':
            axis1 = 0
            axis2 = 0
        elif key == 'q':
            axis1 = -127  # signal stop run
            axis1 = -127
            msg = struct.pack('>bb', axis1, axis2)
            sock.send(msg)
            break

        # ALWAYS send, even if unchanged
        msg = struct.pack('>bb', axis1, axis2)
        sock.send(msg)

        time.sleep(0.05)

except OSError as e:
    print("RFCOMM error:", e)

finally:
    try:
        sock.close()
    except OSError:
        pass
    print("Disconnected")
Log of one run: ``` pybricksdev run usb ..\EV3\tankbot_rc.py 100%|██████████████████████████████████████████████████████████████████████████████████████████████████| 1.33k/1.33k [00:00<00:00, 432kB/s] ('ev3', '4.0.0b3', 'ci-build-4625-v4.0.0b3-114-gab4df866 on 2026-01-15') Local address: A0:E6:F8:E4:42:36 Waiting for connection... [btc:rfcomm_listen] Listening for incoming RFCOMM connections... hci.c.3431: Connection_incoming: 8C:90:2D:41:E4:60, type 1 hci.c.312: create_connection_for_addr 8C:90:2D:41:E4:60, type fd hci.c.6672: sending hci_accept_connection_request hci.c.3457: Connection_complete (status=0) 8C:90:2D:41:E4:60 hci.c.3494: New connection: handle 1, 8C:90:2D:41:E4:60 hci.c.7505: BTSTACK_EVENT_NR_CONNECTIONS_CHANGED 1 IDENTITY_RESOLVING_STARTED sm.c.2269: LE Device Lookup: not found IDENTITY_RESOLVING_FAILED hci.c.506: pairing started, ssp 1, initiator 0, requested level 2 hci.c.7677: gap_mitm_protection_required_for_security_level 2 hci.c.6736: Remote not bonding, dropping local flag SSP User Confirmation Request. Auto-accepting... hci.c.528: pairing complete, status 00 Link key updated, saving to settings. Saved 0 link keys to settings sm.c.3900: Encryption state change: 1, key size 0 sm.c.3902: event handler, state 82 hci.c.2788: Handle 0001 key Size: 16 hci.c.7536: hci_emit_security_level 2 for handle 1 l2cap.c.2833: security level update for handle 0x0001 l2cap.c.3633: extended features mask 0xba l2cap.c.2460: create channel c007e770, local_cid 0x0042 l2cap.c.3649: fixed channels mask 0x8a hci.c.2509: Remote features 03, bonding flags 70 l2cap.c.2822: remote supported features, channel c007e770, cid 0042 - state 4 l2cap.c.1159: L2CAP_EVENT_INCOMING_CONNECTION addr 8C:90:2D:41:E4:60 handle 0x1 psm 0x3 local_cid 0x42 remote_cid 0x40 rfcomm.c.382: rfcomm_max_frame_size_for_l2cap_mtu: 1691 -> 1686 rfcomm.c.1074: RFCOMM incoming (l2cap_cid 0x42) => accept l2cap.c.3131: L2CAP_ACCEPT_CONNECTION local_cid 0x42 l2cap.c.1404: l2cap_stop_rtx for local cid 0x42 l2cap.c.1441: l2cap_start_rtx for local cid 0x42 l2cap.c.3359: L2CAP signaling handler code 4, state 11 l2cap.c.3187: Remote MTU 1017 l2cap.c.3359: L2CAP signaling handler code 5, state 11 l2cap.c.1404: l2cap_stop_rtx for local cid 0x42 l2cap.c.3289: l2cap_signaling_handle_configure_response l2cap.c.1129: L2CAP_EVENT_CHANNEL_OPENED status 0x0 addr 8C:90:2D:41:E4:60 handle 0x1 psm 0x3 local_cid 0x42 remote_cid 0x40 local_mtu 1691, remote_mtu 1017, flush_timeout 0 rfcomm.c.1101: channel opened, status 0 rfcomm.c.382: rfcomm_max_frame_size_for_l2cap_mtu: 1691 -> 1686 rfcomm.c.1219: Received SABM #0 rfcomm.c.1364: Sending UA #0 rfcomm.c.941: Multiplexer up and running rfcomm.c.1640: Received UIH Parameter Negotiation Command for #2, credits 7 rfcomm.c.509: rfcomm_channel_create for service c007e44c, channel 1 --- list of channels: rfcomm.c.1997: -> Inform app rfcomm.c.247: RFCOMM_EVENT_INCOMING_CONNECTION addr 8C:90:2D:41:E4:60 channel #1 cid 0x02 rfcomm.c.2628: accept cid 0x02 rfcomm.c.2025: Sending UIH Parameter Negotiation Respond for #2 rfcomm.c.1782: rfcomm_channel_ready_for_incoming_dlc_setup state var 00000003 rfcomm.c.1607: Received SABM #2 rfcomm.c.2029: Sending UA #2 rfcomm.c.1782: rfcomm_channel_ready_for_incoming_dlc_setup state var 00000007 rfcomm.c.2034: Incomping setup done, requesting send MSC CMD and send Credits rfcomm.c.1942: Sending MSC CMD for #2 rfcomm.c.2123: Providing credits for #2 rfcomm.c.1773: rfcomm_channel_ready_for_open state 8, flags needed 0010, current 00014007, rf credits 7 rfcomm.c.1660: Received MSC CMD for #2, rfcomm.c.1773: rfcomm_channel_ready_for_open state 8, flags needed 0010, current 0001500f, rf credits 7 rfcomm.c.1949: Sending MSC RSP for #2 rfcomm.c.1667: Received MSC RSP for #2 rfcomm.c.1773: rfcomm_channel_ready_for_open state 8, flags needed 0010, current 0001c01f, rf credits 7 rfcomm.c.1410: opened rfcomm.c.265: RFCOMM_EVENT_CHANNEL_OPENED status 0x0 addr 8C:90:2D:41:E4:60 handle 0x1 channel #1 cid 0x02 mtu 1011 RFCOMM channel opened: cid=2. rfcomm.c.2667: grant cid 0x02 credits 4 [btc:rfcomm_listen] Connected Connected! rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 25, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.1447: RFCOMM data UIH_PF, new credits channel 0x02: 0, now 32 rfcomm.c.2667: grant cid 0x02 credits 2 rfcomm.c.291: RFCOMM_EVENT_CHANNEL_CLOSED cid 0x02 RFCOMM_EVENT_CHANNEL_CLOSED by remote for cid=2. RFCOMM channel closed: cid=2. rfcomm.c.2214: Sending UA after DISC for #2 [btc:rfcomm_recv] Socket is not connected or does not exist. RFCOMM error rfcomm.c.2553: disconnect cid 0x02 In finally: Closed connection rfcomm.c.1237: Received DISC #0, (ougoing = 0) rfcomm.c.1371: Sending UA #0 rfcomm.c.1372: Closing down multiplexer l2cap.c.3359: L2CAP signaling handler code 6, state 13 l2cap.c.1188: L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x42 rfcomm.c.1174: channel closed cid 0x42, mult 0 l2cap.c.2466: free channel c007e770, local_cid 0x0042 l2cap.c.1404: l2cap_stop_rtx for local cid 0x42 ```

Fun.
I had to add the finally to the EV3 program to prevent already listening on a program restart.
Or I needed to reboot the EV3 to get rid of the socket? connection? or channel?

@jaguilar
Copy link
Contributor Author

Bert, did you still have to add the finally with 29f1254, or was that with the firmware you built earlier? The fixup commits that I added were meant to fix that issue, but I guess I never tried it without removing the finally from my own test scripts.

@BertLindeman
Copy link
Contributor

Bert, did you still have to add the finally with 29f1254, or was that with the firmware you built earlier? The fixup commits that I added were meant to fix that issue, but I guess I never tried it without removing the finally from my own test scripts.

James,
The finally is needed using ('ev3', '4.0.0b3', 'ci-build-4625-v4.0.0b3-114-gab4df866 on 2026-01-15') from the CI builds.
I did no builds myself.
Before that firmware I did not succeed as my programs were no good yet.
Do you want me to try another CI build?

@BertLindeman
Copy link
Contributor

Just for my understanding:

If the user program stops after
conn = await rfcomm_listen()

Then the next run of the program fails with [btc:rfcomm_listen] Already listening.:

pybricksdev run usb  ..\EV3\tankbot_rc.py
100%|██████████████████████████████████████████████████████████████████████████████████████████████████| 1.29k/1.29k [00:00<00:00, 403kB/s]
('ev3', '4.0.0b3', 'ci-build-4625-v4.0.0b3-114-gab4df866 on 2026-01-15')
Local address: A0:E6:F8:E4:42:36
Waiting for connection...
[btc:rfcomm_listen] Already listening.
Traceback (most recent call last):
  File "tankbot_rc.py", line 99, in <module>
  File "tankbot_rc.py", line 50, in main
OSError: [Errno 16] EBUSY: Device or resource busy

Is that the expected result, so the python program should use e.g. a finally clause to stop the listening?

@jaguilar
Copy link
Contributor Author

No, that is not the expected result. I will have to go back and test my fixes in the latest set of commits. They are intended to clean up all of the outstanding sockets, but it may be that I made a mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants