L2CAP IOS + Linux(Bluez)

问题描述 投票:2回答:1

我正在尝试在IOS和Linux PC之间进行简单的L2CAP套接字通信。

我已经能够:

在该IOS示例中,他们使用一些PSM广告来为L2CAP信道选择​​正确的PSM。在集成方面,我已经在两侧设置了固定的PSM。 Iphone连接到Linux机器固定的PSM。我尝试了多个PSM(0x1001,0x25)。

问题是,我无法连接,也无法获得有关播出内容的任何信息。

我的问题是,我是否需要在Linux应用程序上实现动态/广告PSM?我需要选择特定的PSM吗?你能做到这一点吗?你有什么建议吗?

提前致谢!

服务器代码:

#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;
}

客户端基于(来自https://github.com/bluekitchen/CBL2CAPChannel-Demo)。

ios linux bluetooth-lowenergy bluez l2cap
1个回答
2
投票

我现在有一个基于https://github.com/bluekitchen/btstack的工作版本

在iOS方面,我一直在服务器端使用https://github.com/bluekitchen/CBL2CAPChannel-Demo le_data_channel_server

© www.soinside.com 2019 - 2024. All rights reserved.