Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Using serial port for bluetooth app on Mac



  • Hi all,

    I have a program on windows that uses the COM port to communicate with the bluetooth. What I'm trying to do is to port the project on Mac, but I do not know if this is really possible. I know that Qt has the bluetooth support for Mac but I'm trying to keep the project as equal as possible.
    So the first question is: is it possible to use serial port to communicate with bluetooth devices on Mac?

    Actually I have done some tests but sometimes it works and sometimes no.
    So what I do on Mac is:

    • I search for new devices using QBluetoothDeviceDiscoveryAgent
    • I pair a new device using QBluetoothLocalDevice and the function requestPairing
    • from this moment the device should be paired to the Mac and I would like to communicate using the SPP profile. so I'm using QSerialPortInfo to get the COM port and the name of the device. This is the code I use:
        QList<QSerialPortInfo> com_ports = QSerialPortInfo::availablePorts();
        QSerialPortInfo port;
    
        foreach(port, com_ports)
        {
            qDebug() << "Serial Port Info " << port.portName() << " " << port.description();
        }
    

    And this is the Output:
    Screenshot2.png

    As you can see there are multiple instances of the same devices and I don't know why and to which one I should connect. I'm not talking about choosing between tty and cu, but the device 'POPOV' that terminates with 01 is present 6 times. That's because every time I pair the device it creates a new connection and does not delete the previous connection?
    I suppose that this could be a reason why sometimes it doesn't work.

    Thats' how I pair the devices:

           QTableWidgetItem *pItem = ui->tableDevices->item(row, 0);
            for(int i = 0; i < listDevicesFound.size(); i++)
            {
                if(listDevicesFound.at(i).name().contains(pItem->data(Qt::DisplayRole).toString()))
                {
                    pItem = ui->tableDevices->item(row, 1);
                    pItem->setData(Qt::DisplayRole, "Pairing...");
                    localDevice->requestPairing(listDevicesFound.at(i).address(), QBluetoothLocalDevice::Paired);
                    break;
                }
            }
    

    And that's how I verify if they are already paired or not:

    for(QBluetoothDeviceInfo dev : listDevicesFound)
        {
            qDebug() << "Nome: " << dev.name() << " MAC: " << dev.address().toString();
            QTableWidgetItem *pItem = new QTableWidgetItem(dev.name());
            pItem->setTextAlignment(Qt::AlignCenter);
            ui->tableDevices->setItem(i, 0, pItem);
    
            QBluetoothLocalDevice::Pairing pairingStatus = localDevice->pairingStatus(dev.address());
            if (pairingStatus == QBluetoothLocalDevice::Paired || pairingStatus == QBluetoothLocalDevice::AuthorizedPaired )
            {
                         qDebug() << "Paired";
            }
    

    And just one more question. I'm pretty new to Mac and I didn't completely get the differences between tty and cu. If I have a bidirectional communication over bluetooth between Pc and a bluetooth device which one I should use? If I understood well I can use cu since the port is used by only one process.

    I'm using:

    • QT 5.12.2 clang 64bit
    • MacOs 10.13.6

    Thanks in advance for the help!



  • Hi,
    macOS has two instances of every serial device - tty.<devicename> for incoming connections and cu.<devicename> (cu stands for call out) for the outgoing so you need to use them respectively.



  • Thank you. In this case I will use cu.

    I still can't understand why I have multiple instances of the same device. Do you have any suggestion?



  • @davidesalvetti It is explained by Apple in official documentation - it all boils down to that this way you can have non-blocking access per channel, you can send and receive in the same time.



  • @artwaw I did not mean why Apple uses .tty and .cu , that's ok.

    If you take a look at my screenshot you will get the point: I have the same device "POPOV" for 6 times (considering .tty and .cu as a pair).

    I don't understand why, is it possible to pair the same device more than once?



  • @davidesalvetti I am not an expert with that but for example my headphones can be paired at the same time with my phone and my laptop. So I'd say - yes, it is possible, but I can't tell why you see what's on the screenshot, sadly that is beyond my knowledge.



  • The strange thing is that my device looks like it has been paired more than once on the same pc, as if it was not paired yet.

    Does somebody know why? or if I am missing something?


Log in to reply