Main Content


Decode BLE L2CAP frame

Download Required: To use bleL2CAPFrameDecode, first download Communications Toolbox™ Library for the Bluetooth® Protocol. For more information, see Get and Manage Add-Ons. Alternatively, see Communications Toolbox Library for the Bluetooth Protocol File Exchange.



[status,cfgL2CAP,SDU] = bleL2CAPFrameDecode(L2CAPFrame) decodes the specified Bluetooth low energy (BLE) logical link control and adaptation protocol (L2CAP) frame, L2CAPFrame. The function returns the decoding status, status, the corresponding BLE L2CAP configuration object, cfgL2CAP, and the upper-layer payload service data unit (SDU), SDU.


collapse all

Create a BLE L2CAP configuration object, 'cfgL2CAP', with default properties and view the applicable properties. Change the value of channel identifier to '0004'.

cfgL2CAP = bleL2CAPFrameConfig
cfgL2CAP = 
  bleL2CAPFrameConfig with properties:

          ChannelIdentifier: '0005'
                CommandType: 'Credit based connection request'
           SignalIdentifier: '01'
    SourceChannelIdentifier: '0040'
                      LEPSM: '001F'
        MaxTransmissionUnit: 23
          MaxPDUPayloadSize: 23
                    Credits: 1

   Read-only properties:
    No properties.

cfgL2CAP.ChannelIdentifier = '0004'; % Channel identifier for ATT

Generate a BLE L2CAP data frame from 'cfgL2CAP', specifying the upper-layer payload SDU from attribute protocol (ATT) layer as '0A0100'.

L2CAPFrame = bleL2CAPFrame(cfgL2CAP, "0A0100");

Decode the generated BLE L2CAP data frame. The returned status indicates decoding was successful. View the output of 'status', 'cfgL2CAP' and the 'SDU'.

[status, cfgL2CAP, SDU] = bleL2CAPFrameDecode(L2CAPFrame)
status = 
  blePacketDecodeStatus enumeration


cfgL2CAP = 
  bleL2CAPFrameConfig with properties:

    ChannelIdentifier: '0004'

   Read-only properties:
    No properties.

SDU = 3x2 char array

Specify a BLE L2CAP frame containing corrupted data values.

l2capFrame = ['090005000107040060005000']; % Sample frame

Decode the specified BLE L2CAP frame. The returned status indicates that the decoding failed due to the corrupted input L2CAP frame. In this case, when decoding fails, the output displays the reason of failure and the BLE L2CAP frame configuration object, 'cfgL2CAP', displays no properties.

[status, cfgL2CAP, SDU] = bleL2CAPFrameDecode(l2capFrame)
status = 
  blePacketDecodeStatus enumeration


cfgL2CAP = 
  bleL2CAPFrameConfig with properties:

   Read-only properties:
    No properties.


  1x0 empty char array

Input Arguments

collapse all

BLE L2CAP frame, specified as one of these values:

  • Character vector — This vector represents octets in hexadecimal format.

  • String scalar — This scalar represents octets in hexadecimal format.

  • Numeric vector of elements in the range [0, 255] — This vector represents octets in decimal format.

  • n-by-2 character array — Each row represents an octet in hexadecimal format.

Data Types: char | double | string

Output Arguments

collapse all

Packet decoding status, returned as a nonpositive integer of type blePacketDecodeStatus. This value represents the result of decoding a BLE L2CAP frame. Each value of status corresponds to a member of the blePacketDecodeStatus enumeration class, which indicates the packet decoding status according to this table.

Enumeration ValueMember of Enumeration ClassDecoding Status
0SuccessPacket decoding succeeded
–301InvalidL2CAPConnectionIntervalRangeInvalid connection intervals
–302InvalidL2CAPSlaveLatencyInvalid slave latency
–303InvalidLECreditsInvalid low energy (LE) credits
–304L2CAPSegmentationUnsupportedSegmentation is not supported
–305MismatchL2CAPHeaderLengthLength mismatches with actual length
–306 IncompleteL2CAPDataFrameL2CAP data frame is not sufficient to decode
–307InvalidL2CAPChannelIdentifierInvalid L2CAP channel identifier
–308InvalidL2CAPCommandInvalid L2CAP command code
–309InvalidL2CAPCommandRejectReasonInvalid command reject reason code
–310InvalidL2CAPParameterUpdateResultInvalid parameters update result
-311InvalidL2CAPConnectionResultInvalid connection result code
-312IllegalL2CAPSignalIdentifierIllegal signal identifier in L2CAP
-313InvalidL2CAPConnectionIntervalMinimumInvalid interval minimum
-314InvalidL2CAPConnectionIntervalMaximumInvalid interval maximum
-315InvalidL2CAPConnectionTimeoutInvalid connection timeout
-316 InvalidLEPSMInvalid LE protocol/service multiplexer
-317InvalidL2CAPChannelMTUInvalid maximum transmission unit
-318InvalidL2CAPChannelMPSInvalid maximum PDU payload size
-319InvalidL2CAPSDULengthInvalid SDU length
-320MismatchL2CAPSignalFrameLengthLength mismatches with actual length
-321IncompleteL2CAPSignalFrameL2CAP signal frame is not valid or not sufficient

An enumeration value other than 0 means that the BLE ATT PDU decoding failed. If the decoding fails, object cfgATT displays no output.

BLE L2CAP frame configuration object, returned as a bleL2CAPFrameConfig object. This value denotes the decoded BLE L2CAP frame configuration.

Upper-layer payload, returned as a character array. Each row represents an octet in hexadecimal format.

Data Types: char | string


[1] Bluetooth Technology Website. “Bluetooth Technology Website | The Official Website of Bluetooth Technology.” Accessed November 22, 2019.

[2] Bluetooth Special Interest Group (SIG). "Bluetooth Core Specification." Version 5.1.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced in R2019b