Hi, Just a follow up on Rui excellent tutorial on Bluetooth …
mainly c++/c# MFC Home Automation
- github.com/CrazyRobMiles/SimpleESP32BluetoothBLE
- github.com/nkolban/esp32-snippets/blob/master/Documentation/BLE%20C%2B%2B%20Guide.pdf
You can use Bluetoorh Serial Tool on PC from Microsoft Store
or c# code, this allows you send text instructions to esp32 from a Home Automation Dashboard,
alwell you could add Speech to text to control esp32
add 32feet.Net to your project
Note Scan() works on the main thread, just for testing:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Windows.Forms;
//
using InTheHand.Net.Sockets;
using InTheHand.Net.Bluetooth;
using System.Net.Sockets;
using System.Text;
using System.IO;
using InTheHand.Net;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace dwkbt2
{
public partial class Form1 : Form
{
//Gobal Variables
String deviceName;
String myPin = ""; //null for esp32
List<string> items;
bool ready = false;
byte[] message;
//Bluetooth
BluetoothDeviceInfo[] devices = null;
BluetoothDeviceInfo deviceInfo = null;
public Form1()
{
items = new List<string>();
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
scan();
}
private void scan()
{
listBox1.DataSource = null;
BluetoothClient client = new BluetoothClient();
devices = client.DiscoverDevices();
foreach (BluetoothDeviceInfo device in devices)
{
deviceName = device.DeviceName.ToString();
items.Add(device.DeviceName);
}
listBox1.DataSource = items;
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
deviceInfo = devices.ElementAt(listBox1.SelectedIndex);
if (pairDevice())
{
label2.Text = "connected to " + deviceName;
Thread bluetoothClientThread = new Thread(new ThreadStart(ClientConnectThread));
bluetoothClientThread.Start();
}
else
{
label2.Text = "failed";
}
}
private bool pairDevice()
{
if (!deviceInfo.Authenticated)
{
if (!BluetoothSecurity.PairRequest(deviceInfo.DeviceAddress, myPin))
{
return false;
}
}
return true;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
message = Encoding.ASCII.GetBytes(textBox1.Text);
ready = true;
textBox1.Clear();
}
}
private void ClientConnectThread()
{
BluetoothClient client = new BluetoothClient();
client.BeginConnect(deviceInfo.DeviceAddress, BluetoothService.SerialPort, this.BluetoothClientConnectCallback, client);
}
void BluetoothClientConnectCallback(IAsyncResult result)
{
BluetoothClient client = (BluetoothClient)result.AsyncState;
client.EndConnect(result);
Stream stream = client.GetStream();
stream.ReadTimeout = 10000;
while(true)
{
while(ready)
{
stream.Write(message, 0, message.Length);
ready = false;
}
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
Hope it helps