Sunday, March 19, 2017


here is the coding for Android device connection to the Bluetooth Module

also see my first game on play Store without spending a penny
http://tecnologia5853.blogspot.in/2017/08/my-first-game-on-play-store-without.html




pro->

#-------------------------------------------------
#
# Project created by QtCreator 2017-03-19T09:50:06
#
#-------------------------------------------------

QT       += core gui
QT+= bluetooth

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = B_t
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

CONFIG += mobility
MOBILITY = 






mainwindow.h   ->


#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include <QMainWindow>
#include<QBluetoothDeviceDiscoveryAgent>
#include<QBluetoothSocket>
#include<QListWidget>




namespace Ui {
class MainWindow;
}


class MainWindow : public QMainWindow
{
    Q_OBJECT


public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();


private slots:
    void on_find_clicked();


    void on_on_clicked();


    void on_off_clicked();
    void deviceDiscovered(const QBluetoothDeviceInfo &device);




    void on_listWidget_itemClicked(QListWidgetItem *item);


private:
    Ui::MainWindow *ui;
    QBluetoothDeviceDiscoveryAgent *agent = new QBluetoothDeviceDiscoveryAgent;
    QBluetoothSocket *socket;
    QString string;
   bool k = false;
};


#endif // MAINWINDOW_H 






mainWindow.cpp    ->

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QtBluetooth>


//RX -> tx
//tx -> Rx
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(agent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
            this, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));
    agent->start();


   socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);






}


MainWindow::~MainWindow()
{
    delete ui;




}


void MainWindow::on_find_clicked()
{ ui->listWidget->clear();
    agent->stop();
   agent->start();


}


void MainWindow::on_on_clicked()
{
   socket->write("h");
}


void MainWindow::on_off_clicked()
{
   socket->write("b");
}


void MainWindow::deviceDiscovered(const QBluetoothDeviceInfo &device)
{
    ui->listWidget->addItem(device.address().toString());
}




void MainWindow::on_listWidget_itemClicked(QListWidgetItem *item)
{
 string =item->text();


 ui->listWidget->addItem(string);
 static const QString serviceUuid(QStringLiteral("00001101-0000-1000-8000-00805F9B34FB"));


   socket->connectToService(QBluetoothAddress(string), QBluetoothUuid(serviceUuid), QIODevice::ReadWrite);


} 





main.cpp   ->
 



#include "mainwindow.h"
#include <QApplication>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();


    return a.exec();
}






mainwindow.ui->
 
 
 
 
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>480</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QPushButton" name="find">
      <property name="text">
       <string>find</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="on">
      <property name="text">
       <string>on</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="off">
      <property name="text">
       <string>off</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QListWidget" name="listWidget"/>
    </item>
   </layout>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>
 
 
you can buy here

2 comments:

  1. Hello from 2019! I was just wondering if you had been able to figure out the bug that prevented you from communicating to the HC-05 module?

    ReplyDelete
  2. Thanks.Can you tell me How to receive data from HC-05?

    ReplyDelete