查看完整版本: [-- 关于BLE开发的问题 --]

QTCN开发网 -> Qt移动平台开发 -> 关于BLE开发的问题 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

nikonice 2018-02-23 08:47

关于BLE开发的问题

关于Qt 开发BLE的资料很少,目前有个问题,已经连上蓝牙设备,但是连接服务总没反应。求解!
connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(onFindBLEMachine(QBluetoothDeviceInfo)));
连接设备后出发槽函数:
void CBLEService::onFindBLEMachine(const QBluetoothDeviceInfo &info){          
          m_controller = new QLowEnergyController(m_bleDevInfo,this);            
          connect(m_controller, SIGNAL(connected()),                     this, SLOT(onBLEConnected()));        
         connect(m_controller, SIGNAL(error(QLowEnergyController::Error)),                     this, SLOT(errorReceived(QLowEnergyController::Error)));  
          connect(m_controller, SIGNAL(disconnected()),                     this, SLOT(deviceDisconnected()));            
         connect(m_controller, SIGNAL(serviceDiscovered(QBluetoothUuid)),                     this, SLOT(onServiceDiscovered(QBluetoothUuid)));        
        m_controller->connectToDevice();
}
这里connect调试过程都没有问题,但是不能触发上面任何槽的函数。不知道是为什么!

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);
}

nikonice 2018-05-28 08:26
    discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);

    connect(discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
            this, SLOT(addDevice(const QBluetoothDeviceInfo&)));
    connect(discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)),
            this, SLOT(onDeviceScanError(QBluetoothDeviceDiscoveryAgent::Error)));
    connect(discoveryAgent, SIGNAL(finished()), this, SLOT(onScanFinished()));

可以试试,我的没问题的

大千世界mcq 2019-10-09 11:06
Qt for Android BLE连接上蓝牙后,会去搜寻service,根据搜索到的Service uuid,选择你需要的创建service。然后根据你的发送和接收uuid,确定写和读特征,才能完成正常的蓝牙通信。


查看完整版本: [-- 关于BLE开发的问题 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled