• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32 Bluetooth c# + PC talks to esp32

Q&A Forum › Category: ESP32 › ESP32 Bluetooth c# + PC talks to esp32
0 Vote Up Vote Down
DK asked 4 years ago

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

1 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Hi! Thank you so much for the suggestion.

That’s a great idea and use for a Bluetooth application.

You might also consider using Python to interact with the ESP via serial communication.

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2023 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.