Reputation:
(third time writing this one, let's see who give up first. I wish I knew where do you live, to send you a coupon for a trip to some nice place where you can steam off.)
This is a question about how to start with Bluetooth development on Windows.
Problem: You have a micro controller and some sensors, and you want to communicate with a computer. My approach is to use a serial to Bluetooth adapter.
Once this is set up; I can connect from my Windows machine to the micro controller to send data; and receive it too. Now I would like to write an application that run on Windows and create the connection.
This can be accomplished in various languages, but I picked up C#. What is your solution to write an application that is able to read and write via Bluetooth, to the microcontroller?
(is that a question? Yes! And no answer in the question!)
Upvotes: 2
Views: 14373
Reputation: 21
Use Windows forms project template and simply drag a serial port from the toolbar. You can initialize the port with the baud rate and port ID like COM1, COM2 etc. You have to know what commands the serial device responds to from the PC. Not to difficult to handle. Bluetooth is a bit more involved. There is a nice video on this topic. https://youtu.be/RVasdDtgLKY
Upvotes: 0
Reputation:
So, this is the solution found so far. there are others for sure but this is a start.
Bluetooth uses serial ports on Windows; which means that when paired to the computer, the Bluetooth device will couple with COM ports on Windows. So far I found that in my case, COM5 was coupled with the Bluetooth device.
Knowing this, if you want to write an app in C# that talk to the BT device (and with the micro controller of course), you need to implement the serial protocol in your application.
As example, create a new form based application with Visual Studio, and then you can import from the tools section, the serial component and drop it on the form. If you want to write a non visual application; you need to include
System.IO.Ports
This will allow you to query ports on Windows; in particular the serial COM ports.
Then it is just a matter to set up the correct port for transmitting and receiving, and open the connection. The object uses a very simple API that allow you to dump output from serial in a rich text field for example, and read commands to send in the same way, from a regular text field.
As example if you have a serial object called serial1 (System.IO.Ports.SerialPort), a button, a text box and a rich textbox on a form :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Windows.Forms;
namespace serialexample
{
public partial class Form1 : Form
{
// variables area
private string rxstring;
public Form1()
{
InitializeComponent();
}
private void btn_send_Click(object sender, EventArgs e)
{
if (!serial1.IsOpen)
{
serial1.Open();
rtb_receive.Text = "Port Opened";
serial1.Write(txt_send.Text);
}
else
{
serial1.Write(txt_send.Text);
}
}
private void serial1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
rxstring = serial1.ReadExisting();
this.Invoke(new EventHandler(displayText));
}
private void displayText(object o, EventArgs e)
{
rtb_receive.AppendText(rxstring);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
serial1.Close();
}
}
This is what I used as starting point to communicate with the bluetooth module. Thanks to this, the application that I wrote is able to send and receive data to the remove micro controller via bluetooth.
Since this is the first time that I tried this, I am not aware of other ways to do so; but I am sure that there are more robust ways to accomplish the same (in other languages too).
This is a useful video that helped to get to the solution: https://www.youtube.com/watch?v=NkyLErxr3ZA
Upvotes: 1