版權聲明:本文為MULTIBEANS ORG研發跟隨文章,未經MLT ORG允許不得轉載。
最近做項目,需要開發安卓應用,實現串口的收發,目測CH340G在安卓手機上非常麻煩,而且驅動都是Java版本的, 就沒選擇,博主在大二的時候學習過Java SE基本的語法,寫過一些小程序就放棄了Java的道路。最后選擇了藍牙無線透傳模塊,實現串口通信。現在Qt跨平台支持安卓,是在是令人欣喜。在網上找資料,用Qt on Android做藍牙驅動的幾乎沒有,也沒有相關例程,所以准備撰寫此文,獻給廣大嵌入式程序員們
2018/6/27更新:
增加Java版本的藍牙通信,文章地址:https://www.cnblogs.com/sigma0/p/9234478.html
一、軟硬件平台
1.1 硬件平台
1. 藍牙:HC-05,(淘寶上有賣),它的接口就是跟串口一樣的,我們用到了TX,RX,GND,VCC四個引腳。跟下位機或者用CH340G TTL轉USB模塊接到PC機上。藍牙工作在串口模式可以通過AT指令調節。具體參考藍牙配套的說明文檔,最主要的就是請將藍牙設定為從機模式,否則安卓手機搜尋鏈接不上。
2.安卓手機:我這里測試用了2台安卓手機,一台是小米4移動版,安卓版本6.0.1;一台是MOTO MT887,安卓版本4.1.2。
1.2 軟件平台
本項目Qt版本是5.7,系統是windows 8.1 x64
二、軟件基本介紹
因為第一次做藍牙,就做一個非常簡單的雛形,實現藍牙狀態檢測、藍牙的開關、藍牙的掃描和藍牙配對鏈接,並且能像串口助手一樣完成數據收發。如圖,就是本一開始做的最簡單的軟件界面,本軟件基於QWidget控件制作,當然你可以選擇mainwinodw,更可以自己定義類。
軟件界面
我不用介紹每個部位是什么了,都會明白吧?藍牙打開后通過掃描,會將藍牙的MAC地址還有名字顯示在List中,我們雙擊List列表中的藍牙,就會進入actived信號連接的槽函數,執行藍牙的配對連接。建立連接之后,就類似串口一樣可以進行數據通信了。另外,點擊send按鈕之后會發送一堆字符串。
三、 藍牙開發
3.1 項目文件准備
需要用到藍牙就需要在.pro文件中引入庫,我沒有用Qt quick,用的是純C++寫的代碼,你需要在.pro文件中加入這句話:
QT += bluetooth
#include <QtBluetooth/qbluetoothglobal.h> #include <QtBluetooth/qbluetoothlocaldevice.h> #include <qbluetoothaddress.h> #include <qbluetoothdevicediscoveryagent.h> #include <qbluetoothlocaldevice.h> #include <qbluetoothsocket.h>
QBluetoothDeviceDiscoveryAgent *discoveryAgent; QBluetoothLocalDevice *localDevice; QBluetoothSocket *socket;
3.2 藍牙開關和可見性設定
localDevice = new QBluetoothLocalDevice();
1) 藍牙開關
if( localDevice->hostMode() == QBluetoothLocalDevice::HostPoweredOff ) { ui->pushButton_openBluetooth->setEnabled(true); ui->pushButton_closeDevice->setEnabled(false); }else { ui->pushButton_openBluetooth->setEnabled(false); ui->pushButton_closeDevice->setEnabled(true); }
在構造函數中
那么,我們如何來對藍牙進行打開和關閉呢?我在open按鈕和close按鈕的槽函數中對藍牙進行開關操作。
open按鈕的槽函數:
void Widget::on_pushButton_openBluetooth_clicked() { localDevice->powerOn(); ui->pushButton_closeDevice->setEnabled(true); ui->pushButton_openBluetooth->setEnabled(false); ui->pushButton_scan->setEnabled(true); }
void Widget::on_pushButton_closeDevice_clicked() { localDevice->setHostMode(QBluetoothLocalDevice::HostPoweredOff); ui->pushButton_closeDevice->setEnabled(false); ui->pushButton_openBluetooth->setEnabled(true); ui->pushButton_scan->setEnabled(false); }
2) 藍牙可見性
if( localDevice->hostMode() == QBluetoothLocalDevice::HostDiscoverable ) { ui->checkBox_discoverable->setChecked(true); }else { ui->checkBox_discoverable->setChecked(false); }
localDevice->setHostMode( QBluetoothLocalDevice::HostDiscoverable);
3.3 藍牙設備的查找
connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(addBlueToothDevicesToList(QBluetoothDeviceInfo)) );
void Widget::addBlueToothDevicesToList( const QBluetoothDeviceInfo &info ) { QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name()); QList<QListWidgetItem *> items = ui->list->findItems(label, Qt::MatchExactly); if (items.empty()) { QListWidgetItem *item = new QListWidgetItem(label); QBluetoothLocalDevice::Pairing pairingStatus = localDevice->pairingStatus(info.address()); if (pairingStatus == QBluetoothLocalDevice::Paired || pairingStatus == QBluetoothLocalDevice::AuthorizedPaired ) item->setTextColor(QColor(Qt::green)); else item->setTextColor(QColor(Qt::black)); ui->list->addItem(item); } }
3.4 藍牙設備的建立連接
在Linux下你用一個命令uuidgen -t可以生成一個UUID值;在Windows下則執行命令uuidgen 。UUID看起來就像如下的這個形式:2d266186-01fb-47c2-8d9f-10b8ec891363。當使用生成的UUID去創建一個UUID對象,你可以去掉連字符。
static const QLatin1String serviceUuid("00001101-0000-1000-8000-00805F9B34FB");
socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
void Widget::itemActivated(QListWidgetItem *item) { QString text = item->text(); int index = text.indexOf(' '); if (index == -1) return; QBluetoothAddress address(text.left(index)); QString name(text.mid(index + 1)); qDebug() << "You has choice the bluetooth address is " << address; qDebug() << "The device is connneting.... "; QMessageBox::information(this,tr("Info"),tr("The device is connecting...")); socket->connectToService(address, QBluetoothUuid(serviceUuid) ,QIODevice::ReadWrite); }
我們通過對字符串的處理,將得到address信息。通過socket->connectToService(....),把地址,Uuid,和藍牙模式傳遞進去,當執行完這句話的時候,安卓手機開始和你
選擇的藍牙設備進行鏈接。
同樣在socket中也提供了豐富的槽函數,比如成功建立連接信號,成功斷開信號,這里在槽函數中可以做一些例子,這里給出例子:
connect(socket, SIGNAL(connected()), this, SLOT(bluetoothConnectedEvent()) ); connect(socket, SIGNAL(disconnected()), this, SLOT(bluetoothDisconnectedEvent()) );
void Widget::bluetoothConnectedEvent() {
// 2017/10/8 更新一下,請在這里插入關閉藍牙查找服務,否則數據會斷。
// 具體語句是什么我忘記了,反正使用discoveryAgent的一個什么close,或者stop的方法
qDebug() << "The android device has been connected successfully!"; QMessageBox::information(this,tr("Info"),tr("successful connection!")); } void Widget::bluetoothDisconnectedEvent() { qDebug() << "The android device has been disconnected successfully!"; QMessageBox::information(this,tr("Info"),tr("successful disconnection!")); }
最后,還有一個斷開連接函數。通過斷開連接按鈕的槽函數實現。
void Widget::on_pushButton_disconnect_clicked() { socket->disconnectFromService(); }
3.5 發送和接收數據
void Widget::on_pushButton_send_clicked() { QByteArray arrayData; QString s("Hello Windows!!!\nThis message is sended via bluetooth of android device!\n"); arrayData = s.toUtf8(); socket->write(arrayData); }

connect(socket, SIGNAL(readyRead()), this, SLOT(readBluetoothDataEvent()) );
readyRead()信號觸發,跳進readBluetoothDataEvent中。
void Widget::readBluetoothDataEvent() { QByteArray line = socket->readAll(); QString strData = line.toHex(); comStr.append(strData); qDebug() <<"rec data is: "<< comStr; qDebug() <<"The comStr length is: " << comStr.length(); if(comStr.length() >= 30) { ui->textBrowser_info->append(comStr + "\n"); comStr.clear(); } }
我這里是這樣處理的,當然了,你有你自己的處理方法,意思就是那么個意思。