MatanCode
MatanCode

Reputation: 47

Unable to Pair with an RFCOMM Bluetooth Server on Raspberry Pi

I'm running an RFCOMM Bluetooth server on my Raspberry Pi (headless ubuntu) using the BlueZ stack. My goal is to allow a smartphone or PC to pair with the Raspberry Pi via the GUI (e.g., from the Bluetooth settings) and enable serial communication using a virtual serial port.

Current Issue: The Raspberry Pi is visible in the device list on the smartphone/PC. Pairing fails when attempting to connect via the GUI. The only way I can successfully connect is by manually specifying the MAC address and using rfcomm from the PC:

    Pi:
    $ ./bluetooth_server
    Waiting for Bluetooth connections on RFCOMM channel 1...

    PC:
    ➜  ~ sudo rfcomm connect 0 B2:25:EB:16:BC:09 1
    Connected /dev/rfcomm0 to B2:25:EB:16:BC:09 on channel 1

Question: How can I enable GUI-based pairing and connection, allowing the smartphone or PC to seamlessly connect to the Serial Port service without manually specifying the MAC address?

the code:

int main() 
{
    struct sockaddr_rc loc_addr = { 0 }; // server (rpi)
    struct sockaddr_rc rem_addr = { 0 }; // client (pc)
    socklen_t opt = sizeof(rem_addr);
    char buf[1024] = { 0 };
    int socket_descriptor = 0; 
    int client_descriptor = 0; 
    int bytes_read = 0;

    socket_descriptor = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
    if (socket_descriptor < 0) {
        perror("Failed to create socket");
        exit(1);
    }

    loc_addr.rc_family = AF_BLUETOOTH;
    loc_addr.rc_bdaddr = *BDADDR_ANY; // no specific mac_add
    loc_addr.rc_channel = (uint8_t) 1;  // ערוץ 1

    if (bind(socket_descriptor, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) < 0) {
        perror("Failed to bind socket");
        close(socket_descriptor);
        exit(1);
    }

    listen(socket_descriptor, 1);
    printf("Waiting for Bluetooth connections on RFCOMM channel 1...\n");

    client_descriptor = accept(socket_descriptor, (struct sockaddr *)&rem_addr, &opt);
    if (client_descriptor < 0) {
        perror("Failed to accept connection");
        close(socket_descriptor);
        exit(1);
    }

    char client_addr[18] = { 0 };
    ba2str(&rem_addr.rc_bdaddr, client_addr);
    printf("Accepted connection from %s\n", client_addr);

    const char *welcome_message = "Welcome to Raspberry Pi Bluetooth Server!\r\n";
    write(client_descriptor, welcome_message, strlen(welcome_message));

    while (1) {
        memset(buf, 0, sizeof(buf));
        bytes_read = (int)read(client_descriptor, buf, sizeof(buf) - 1);
        if (bytes_read > 0) {
            buf[bytes_read] = '\0'; 
            printf("Received: %s\n", buf);

            write(client_descriptor, buf, (size_t)bytes_read);
        } else if (bytes_read < 0) {
            perror("Failed to read from client");
            break;
        }
    }

    close(client_descriptor);
    close(socket_descriptor);
    return 0;
}

in addition, I have the following coniguration:

$ hciconfig
hci0:   Type: Primary  Bus: UART
    BD Address: B8:27:EB:16:BC:06  ACL MTU: 1021:8  SCO MTU: 64:1
    **UP RUNNING PSCAN ISCAN**
    RX bytes:83994 acl:4189 sco:0 events:2388 errors:0
    TX bytes:61466 acl:4071 sco:0 commands:279 errors:0

$ bluetoothctl
Agent registered
[CHG] Controller B8:27:EB:16:BC:06 Pairable: yes
[bluetooth]# agent on
Agent is already registered
[bluetooth]# default-agent
Default agent request successful
[bluetooth]# discoverable on
Changing discoverable on succeeded
[bluetooth]# pairable on
[bluetooth]# quit


$ sudo systemctl status bluetooth
● bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2024-12-22 22:30:04 UTC; 9s ago
       Docs: man:bluetoothd(8)
   Main PID: 2639 (bluetoothd)
     **Status: "Running"**
      Tasks: 1 (limit: 961)
     Memory: 624.0K
        CPU: 276ms
     CGroup: /system.slice/bluetooth.service
             **└─2639 /usr/lib/bluetooth/bluetoothd --plugin=simple-agent --compat**

Dec 22 22:30:04 matan bluetoothd[2639]: Ignoring (cli) avrcp
Dec 22 22:30:04 matan bluetoothd[2639]: Ignoring (cli) network
Dec 22 22:30:04 matan bluetoothd[2639]: Ignoring (cli) input
Dec 22 22:30:04 matan bluetoothd[2639]: Ignoring (cli) hog
Dec 22 22:30:04 matan bluetoothd[2639]: Ignoring (cli) gap
Dec 22 22:30:04 matan bluetoothd[2639]: Ignoring (cli) scanparam
Dec 22 22:30:04 matan bluetoothd[2639]: Ignoring (cli) deviceinfo
Dec 22 22:30:04 matan bluetoothd[2639]: Ignoring (cli) battery
Dec 22 22:30:04 matan bluetoothd[2639]: Ignoring (cli) sixaxis
Dec 22 22:30:04 matan bluetoothd[2639]: Bluetooth management interface 1.21 initialized

What I've Tried: Added the Serial Port (SP) service using sdptool add SP. Made the Raspberry Pi discoverable and pairable. Verified that the RFCOMM server is running and listening on Channel 1. Any guidance on enabling automatic pairing and connection for GUI-based interaction would be greatly appreciated! Thank you!

Upvotes: 1

Views: 61

Answers (0)

Related Questions