Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using serial port for bluetooth app on Mac

Using serial port for bluetooth app on Mac

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    davidesalvetti
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by artwaw
      #2

      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.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      3
      • D Offline
        D Offline
        davidesalvetti
        wrote on last edited by
        #3

        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?

        artwawA 1 Reply Last reply
        0
        • D davidesalvetti

          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?

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @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.

          For more information please re-read.

          Kind Regards,
          Artur

          D 1 Reply Last reply
          0
          • artwawA artwaw

            @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.

            D Offline
            D Offline
            davidesalvetti
            wrote on last edited by
            #5

            @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?

            artwawA 1 Reply Last reply
            0
            • D davidesalvetti

              @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?

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #6

              @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.

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              2
              • D Offline
                D Offline
                davidesalvetti
                wrote on last edited by
                #7

                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?

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved