How to connect BLE device on Win10 os?
-
void MainWindow::discoveryFinished()
{
qDebug()<<"discoveryFinished "+BTaddress;
//static QString serviceUuid("00001101-0000-1000-8000-00805F9B34FB");
static QString serviceUuid("6e400003-b5a3-f393-e0a9-e50e24dcca9e");
socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
socket->connectToService(QBluetoothAddress(BTaddress), QBluetoothUuid(serviceUuid),QIODevice::ReadWrite);
qDebug()<<"connectFinished ";
connect(socket,SIGNAL(readyRead()), this, SLOT(readBluetoothDataEvent()));
qDebug()<<"connectreadBluetoothDataFinished ";
connect(socket,SIGNAL(connected()), this, SLOT(bluetoothConnectedEvent()));
qDebug()<<"connectbluetoothConnectedEvent ";
}void MainWindow::bluetoothConnectedEvent()
{
qDebug()<<"bluetoothConnectedEvent";bluetoothDataSend("hellow bluetooth");
}
void MainWindow::readBluetoothDataEvent()
{
char data[100];
qDebug()<<BTaddress + "ReadEvent";
qint64 len = socket->read((char *)data, 100);QByteArray qa2((char*)data,len);
QString qstr(qa2.toHex());//
qDebug()<<"----"<<qstr.toUpper();
}output:
Starting E:\BLE_DEMO\build-Bluetooth_link-Desktop_Qt_5_11_1_MSVC2017_64bit-Debug\debug\Bluetooth_link.exe...
"12:58:56:7A:4E:E5 S9"
""
"00:02:5B:01:89:C7 CY5"
""
"DE:89:F5:DF:AB:5A Nordic_UART"
"DE:89:F5:DF:AB:5A"
"discoveryFinished DE:89:F5:DF:AB:5A"
connectFinished
connectreadBluetoothDataFinished
connectbluetoothConnectedEventWhy not excute void MainWindow::bluetoothConnectedEvent() & void MainWindow::readBluetoothDataEvent()?
-
@Zhang_Yubiao Connect a slot to https://doc.qt.io/qt-5/qbluetoothsocket.html#error-1 and print the error when it is called: https://doc.qt.io/qt-5/qbluetoothsocket.html#errorString
-
General recommendation for Windows: use newest version of OS (Microsoft keeps fixing BLE support. On older versions it is either not working at all or crashing a lot) and moderately new version of Qt (anything newer than Qt 5.10 should work OK).
-
Win10 1903 18362.53
Qt5.11.1\5.11.1\msvc2017_64Is it OK?
-
Yep, should be fine.
-
13:53:16: Starting E:\BLE_DEMO\build-Bluetooth_link-Desktop_Qt_5_14_0_MSVC2017_64bit-Debug\debug\Bluetooth_link.exe ...
"12:58:56:7A:4E:E5 S9"
""
"00:02:5B:01:89:C7 CY5"
""
"DE:89:F5:DF:AB:5A Nordic_UART"
"DE:89:F5:DF:AB:5A"
"discoveryFinished DE:89:F5:DF:AB:5A"
connectFinished
connectreadBluetoothDataFinished
connectbluetoothConnectedEventUse QT_5_14 same problem.
-
@Zhang_Yubiao
why do you useQBluetoothSocket
and notQBluetoothDeviceDiscoveryAgent
&QLowEnergyService
Or do you not target BlueTooth LowEnergy , but normal Bluetooth?
-
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);QBluetoothDeviceDiscoveryAgent *discoveryAgent; discoveryAgent = new QBluetoothDeviceDiscoveryAgent; connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(discoverBlueTooth(QBluetoothDeviceInfo))); connect(discoveryAgent, SIGNAL(finished()), this, SLOT(discoveryFinished())); discoveryAgent->start();
}
Module is LowEnergy, is there problem in the code?