[Interest] Serial port / Bluetooth port to Android

Alex Blasche alexander.blasche at qt.io
Wed Aug 30 08:21:43 CEST 2017

> -----Original Message-----
> From: Murphy, Sean [mailto:smurphy at walbro.com]
> Sent: Tuesday, 29 August 2017 17:16
> To: Alex Blasche <alexander.blasche at qt.io>; interest at qt-project.org
> Subject: RE: Serial port / Bluetooth port to Android
> > > On the Android device, we really only care about connected to the
> > > external device via Bluetooth. But does this same behavior, where
> > > Bluetooth looks like a serial device, exist under Android and I can
> > > still use QSerialPort? Or do I need some other device (i.e.
> QBluetoothSocket)?
> >
> > You have to use QBluetoothSocket. There is no other choice. At the end
> > of the day both interfaces are derived from QIODevice. "Merely" the
> > path to your QIoDevice is different.
> Thanks for the response.
> Is this just a difference of Android vs. Windows that our external BT device
> doesn't show up as a serial device under Android like it does under Windows?

The Android way of working is the same for Bluez/Linux (although with rfcomm you can achieve the same), WinRT and the Apple platforms. 

> I don't think it will be a huge deal to rewrite, but 4 years ago when I started
> writing the affected class we were "only ever going to run this application on
> Windows using a USB to Serial adapter". So back then I created a class that
> inherits from QSerialPort and started coding. I think based on what you're saying
> about needing QBluetoothSocket under Android, and to make it more flexible in
> general, I should probably just change that class to inherit from QObject and
> take a
> QIODevice* so that I can swap out QSerialPort, QBluetoothSocket, or even
> QTcpSocket if needed?

Your suggestion sound like a reasonable approach. However please be aware that although most of the QIODevice instances in Qt have a common interface there are slight differences anyway. One might not support buffered reads, the next may have a different spin on what's a proper line read etc. It's not the end of the world but you want to make sure to test your code with all the various QIODevice types.


More information about the Interest mailing list