L2CAP Connection Oriented Channels (Initiator)

Browse source code on GitHub

Overview

This sample demonstrates how to create an L2CAP Connection Oriented Channel (CoC) client that scans for Bluetooth devices, connects to one, and establishes an L2CAP channel to communicate with a server.

The sample periodically sends short text messages over the L2CAP channel to demonstrate basic data transmission.

Requirements

Building and Running

This sample can be built and run on any board with Bluetooth LE support.

To build the sample:

west build -b nrf52840dk/nrf52840 samples/bluetooth/l2cap_coc_initiator
west flash

After flashing, the device will scan for nearby Bluetooth devices, connect to the first one it finds, and attempt to establish an L2CAP channel on PSM 0x29.

Pair this sample with the L2CAP Connection Oriented Channels (Acceptor) sample running on another device. The initiator will automatically scan, connect, and establish the L2CAP channel. Monitor the serial output on both devices to observe the connection and data exchange.

Sample Output

Upon successful connection and channel establishment:

Scanning...
Found device: XX:XX:XX:XX:XX:XX (RSSI -45)
Connecting to XX:XX:XX:XX:XX:XX ...
Connected
L2CAP channel connection initiated
L2CAP channel connected
Sent: Hello from client

The message “Hello from client” will be sent periodically.

See also

L2CAP