I was building a bluetooth controlled display using Qt and raspberrypi.The raspberrypi will have a bluetooth service running on it and when I connect to the BluetoothServer. The user can choose to either a display a message or upload and display an image. I use QBluetoothSocket to send the message or Image file to be displayed to the server. So how do I detect which kind of data is being sent? if it's a plain text or image file and act on the data accordingly.
For sending Files
Код: Выделить всё
// sendText to the service
void BSocket::sendMessage(const QString &message)
{
QByteArray text = message.toUtf8() + '\n';
socket->write(text);
}
// sendImage to the service
void BSocket::sendFile(const QString &fileName)
{
QImage img(fileName);
img.save(socket);
}
Код: Выделить всё
void trialSocket::read_data()
{
if (socket->bytesAvailable()) {
QByteArray ba = socket->readAll();
QMimeType dataType = db.mimeTypeForData(ba);
if (dataType.inherits(QString("text/plain"))) {
emit displayMessage(QString(ba));
} else if (QImageReader::supportedMimeTypes().contains(dataType.name().toUtf8())) {
QImage img = QImage::fromData(ba);
emit displayImage(img);
} else {
qDebug()
Источник: [url]https://stackoverflow.com/questions/78132913/how-to-detect-the-type-of-data-or-file-being-sent-in-qbluetoothsocket[/url]