xiaolanchong |
2018-05-22 21:29 |
楼主,我的为什么连蓝牙设备都找不到,代码看来看去都没问题 #include "Bluetooth.h" #include "ui_Bluetooth.h"
static const QLatin1String serviceUuid("00001101-0000-1000-8000-00805F9B34FB");
Bluetooth::Bluetooth(QWidget *parent) : QWidget(parent), localDevice(new QBluetoothLocalDevice), ui(new Ui::Bluetooth) { ui->setupUi(this);
// localDevice = new QBluetoothLocalDevice(); discoveryAgent = new QBluetoothDeviceDiscoveryAgent(); /* 判断蓝牙是否开启,若开启则不可被选中 扫描周围蓝牙设备 */ if( localDevice->hostMode() == QBluetoothLocalDevice::HostPoweredOff) { ui->pushButton_openBluetooth->setEnabled(true); ui->pushButton_upDate->setEnabled(false); } else { ui->pushButton_openBluetooth->setEnabled(false); discoveryAgent->start(); } /* 给socket分配内存,限定套接字协议 */ socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
/* 发现设备时会触发deviceDiscovered信号,转到槽显示设备 */ connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(addBlueToothDevicesToList(QBluetoothDeviceInfo)));
connect(discoveryAgent, SIGNAL(finished()), this, SLOT(upDateFinish())); /* 点击显示的蓝牙转到连接槽 */ connect(ui->listWidget_displayBluetoothDevice, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(bluetoothConnect(QlistWidgetItem*))); }
Bluetooth::~Bluetooth() { delete ui; } /* 打开蓝牙按钮 点击 */ void Bluetooth::on_pushButton_openBluetooth_clicked() { localDevice->powerOn(); ui->pushButton_openBluetooth->setEnabled(false); ui->pushButton_upDate->setEnabled(true); /* 设置扫描蓝牙方式 GeneralUnlimitedInquiry:无限调查发现所有可见蓝牙设备 */ //discoveryAgent->setInquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry); /* 开始扫描蓝牙设备 */ discoveryAgent->start(); } /* 显示发现的蓝牙设备 */ void Bluetooth::displayBluetoothDevice(const QBluetoothDeviceInfo &info) { QString str = QString("%1 %2").arg(info.address().toString()).arg(info.name()); QList<QListWidgetItem *> items = ui->listWidget_displayBluetoothDevice->findItems(str, Qt::MatchExactly); if (items.empty()) { QListWidgetItem *item = new QListWidgetItem(str); /* 蓝牙状态pairingStatus,Pairing枚举类型 0:Unpaired没配对 1:Paired配对但没授权 2:AuthorizedPaired配对且授权 */ 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->listWidget_displayBluetoothDevice->addItem(item); } } /* 连接蓝牙 */ void Bluetooth::bluetoothConnect(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)); /* 输入配对密码 */
QMessageBox::information(this,tr("Info"),tr("The device is connecting...")); socket->connectToService(address, QBluetoothUuid(serviceUuid) ,QIODevice::ReadWrite); connect(socket, SIGNAL(connected()), this, SLOT(bluetoothConnectOk())); connect(socket, SIGNAL(disconnected()), this, SLOT(bluetoothConnectNot())); } /* 连接成功 */ void Bluetooth::bluetoothConnectOk() { discoveryAgent->stop(); //停止搜索设备 QMessageBox::information(this, tr("成功"), tr("连接成功!")); } /* 连接失败 */ void Bluetooth::bluetoothConnectNot() { QMessageBox::information(this, tr("错误"), tr("连接失败!")); } /* 外部调用socket */ QBluetoothSocket *Bluetooth::getSocket() { return socket; } /* 刷新 重新扫描周围蓝牙设备 */ void Bluetooth::on_pushButton_upDate_clicked() { // discoveryAgent->setInquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry); discoveryAgent->start(); ui->pushButton_upDate->setEnabled(false); }
void Bluetooth::upDateFinish() { ui->pushButton_upDate->setEnabled(true); }
|
|