Re: Bluetooth from Windows to Linux using BlueZ

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Peter,

On Mon, Apr 15, 2024 at 11:19 AM Peter Mortier <peter.mortier@xxxxxxxxxx> wrote:
>
> Dear all,
> I want to set up a simple bluetooth connection between 2 PC’s, client running Windows and server running Linux.
> There is no need for a lot of overhead such as pairing.
> I am running the server software on Linux Ubuntu with the following code:
>
>     int serverSocket, clientSocket;
>     struct sockaddr_rc serverAddr = {0}, clientAddr={0};
>
>     socklen_t clientAddrSize = sizeof(sockaddr_rc);
>
>     // Create Bluetooth socket
>     serverSocket=socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
>     if (serverSocket<0)
>     {
>       perror("socket creation failed: ");
>       return false;
>     }
>     bdaddr_t  bdaddr_any={{0,0,0,0,0,0}};
>
>   // Bind the socket to any local Bluetooth adapter
>     serverAddr.rc_family = AF_BLUETOOTH;
>     serverAddr.rc_bdaddr = bdaddr_any; // bind to any local Bluetooth adapter
>     serverAddr.rc_channel = 25;
>
>     if (bind(serverSocket,(struct sockaddr*)&serverAddr,sizeof(serverAddr))<0)
>     {
>       perror("Bind failed");
>       close(serverSocket);
>       exit(EXIT_FAILURE);
>     }
>
>     // listen for incoming connections
>     if (listen(serverSocket,1) <0)
>     {
>       perror("Listen failed");
>       close(serverSocket);
>       exit(EXIT_FAILURE);
>     };
>
>     while (true)
>     {
>       wxPrintf("Waiting for incoming Bluetooth connections...\n");
>
>       // Accept incoming connection
>       clientSocket = accept(serverSocket, (struct sockaddr*) &clientAddr,&clientAddrSize);
>       if (clientSocket<0)
>       {
>         perror("accept failed");
>         continue;
>       }
>
> I am running the client software on Windows 10 with the following code:
>
>     // Create a Bluetooth socket
>     m_serverSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
>     SOCKADDR_BTH serverAddr = { 0 };
>     if (m_serverSocket == INVALID_SOCKET) {
>       return false;
>     }
>
>     // Set the address of the remote Bluetooth device to connect to
>     serverAddr.addressFamily = AF_BTH;
>     serverAddr.btAddr = *reinterpret_cast<BTH_ADDR*>(&m_Server.Address.ullLong); // this is the addres found by function BluetoothFindFirstDevice()
>     serverAddr.serviceClassId = RFCOMM_PROTOCOL_UUID;  // I also tried GUID_NULL
>     serverAddr.port = 25; // I also tried 0
>
>     // Connect to the remote Bluetooth device
>     if (connect(m_serverSocket, reinterpret_cast<SOCKADDR*>(&serverAddr), sizeof(serverAddr)) == SOCKET_ERROR) {
>        std::cout << "error: " << WSAGeLastError() << std::endl;
>       return false;
>     }
>
> The Linux server is listening on port 25 while the Windows client tries to connect to the same port. The client first searches for the server with BluetoothFindFirstDevice and BluetoothFindNextDevice, finds the server bluetoooth address and uses that address to connect.
> The connection on the client then fails with error code 10051 (or sometimes 10060)
>
> On Linux there is no firewall installed, using bluetoothclt I see it is registered and pairable. The bluetooth dongle is plugged into a USB port and is seen as USB2.0-BT. I have tried different ports without success.
>
> I've done some testing with my code. It seems bluetooth is working well with code above in the following circumstances :
> - windows → windows
> - linux → linux
> - linux → windows
> The only thing that doesn't work is: windows → linux
>
> I am told the problem lies in the BlueZ library, but I find this hard to believe. My guesses are I am doing something wrong.

We probably need to HCI trace to tell what is going on, no idea what
are the errors that windows stack is throwing, besides if you manage
to connect Linux to Linux I assume the server is listening, but
perhaps  there is a different problem, anyway I would recommend using
rctest instead before attempting to write your own version of it:

https://github.com/bluez/bluez/blob/master/tools/rctest.rst

> Any help is much appreciated!
>
> Best regards,
> Peter



-- 
Luiz Augusto von Dentz





[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux