ioslinuxbluetooth-lowenergybluezl2cap

L2CAP IOS + Linux (Bluez)


I'm trying to make a simple L2CAP Socket communication between IOS and a Linux PC.

I've been able to:

On that IOS example they are using some PSM advertise in order to chose the correct PSM for the L2CAP channel. On the integration, I've set a fixed PSM on both sides. The Iphone is connecting to the Linux machine fixed PSM. I've tried multiple PSM (0x1001, 0x25).

The problem is, I can't connect and can't get any information on what is happening on the air.

My question is, do I need to implement a dynamic/advertise PSM on the Linux application? Do I need to pick a specific PSM? Have you been able to make this work? Do you have any suggestions?

Thanks in advance!

Server code:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/l2cap.h>
#include "l2cap_socket.h"

int main(int argc, char **argv)
{
    struct sockaddr_l2 loc_addr = { 0 }, rem_addr = { 0 };
    char buf[1024] = { 0 };
    int server_socket, client_socket, bytes_read;
    unsigned int opt = sizeof(rem_addr);

    printf("Start Bluetooth L2CAP server...\n");

    /* allocate socket */
    server_socket = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);

    /* bind socket to the local bluetooth adapter */
    loc_addr.l2_family = AF_BLUETOOTH;                      /* Addressing family, always AF_BLUETOOTH */
    bacpy(&loc_addr.l2_bdaddr, BDADDR_ANY);                 /* Bluetooth address of local bluetooth adapter */
    loc_addr.l2_psm = htobs(L2CAP_SERVER_PORT_NUM);         /* port number of local bluetooth adapter */

    printf("binding\n");
    if(bind(server_socket, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) < 0) {
        perror("failed to bind");
        exit(1);
    }

    printf("listening\n");
    /* put socket into listening mode */
    listen(server_socket, 1);

    /* accept one connection */
    client_socket = accept(server_socket, (struct sockaddr *)&rem_addr, &opt);  /* return new socket for connection with a client */

    ba2str( &rem_addr.l2_bdaddr, buf );
    printf("connected from %s\n", buf);

    /* read data from the client */
    memset(buf, 0, sizeof(buf));
    bytes_read = recv(client_socket, buf, sizeof(buf), 0);
    if( bytes_read > 0 ) {
        printf("received [%s]\n", buf);
    }

    /* close connection */
    close(client_socket);
    close(server_socket);
    return 0;
}

Client is based on (from https://github.com/bluekitchen/CBL2CAPChannel-Demo).


Solution

  • I have now a working version based on https://github.com/bluekitchen/btstack

    On the iOS side i have been using https://github.com/bluekitchen/CBL2CAPChannel-Demo On the server side le_data_channel_server.