编程工具:Visual Studio 2022社区版
创建C# Form项目
需要在__工具__>>__ NuGet包__下载InTheHand.Net.Bluetooth
蓝牙连接部分
program.cs中的代码如下
这部分只负责蓝牙连接
using System;
using InTheHand.Net.Sockets;
using InTheHand;
using InTheHand.Net.Bluetooth;
using System.Threading;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
namespace blueTooth1
{
internal static class Program
{
public static BluetoothClient bluetoothClient;
public static bool isConnected;
public static bool getDevice()
{
Trace.WriteLine("Finding Devides...");
//BluetoothRadio.PrimaryRadio.Mode = InTheHand.Net.Bluetooth.RadioMode.Connectable;
BluetoothClient cli = new BluetoothClient();
IReadOnlyCollection<BluetoothDeviceInfo> devices = cli.DiscoverDevices();
foreach (BluetoothDeviceInfo device in devices) //设备搜寻
{
//device.Update();
device.Refresh();
Trace.WriteLine(device.DeviceName);
if (device.DeviceName == "JDY-31-SPP")//蓝牙名称,需要自己修改,连接前请打开蓝牙
{
Trace.WriteLine("Connecting...");
try
{
if (!device.Connected)
{
Debug.WriteLine(device.DeviceAddress);
Debug.WriteLine(BluetoothService.SerialPort);
cli.Connect(device.DeviceAddress, BluetoothService.Handsfree);//BluetoothService.Handsfree
Trace.WriteLine("Connected!");
bluetoothClient = cli;
ReceiveData();
}
else
Trace.WriteLine("Has Been Connected!");
}
catch (Exception e)
{ Trace.WriteLine("Failed:" + e);
try
{
cli.Connect(device.DeviceAddress, BluetoothService.SerialPort);//BluetoothService.Handsfree
Trace.WriteLine("Connected!");
bluetoothClient = cli;
ReceiveData();
}
catch (Exception e0)
{
Trace.WriteLine("Failed:" + e0);
}
}
break;
}
}
//Thread ReceiveThread = new Thread(ReceiveData);
//ReceiveThread.Start();
return true;
}
private static void ReceiveData()
{
isConnected = bluetoothClient.Connected;
//下面这部分是获取所连接的蓝牙设备发送的信息
//while (isConnected)
//{
// try
// {
// string receive = string.Empty;
// Stream peerStream = bluetoothClient.GetStream();
// byte[] buffer = new byte[255];
// peerStream.Read(buffer, 0, 255);
// receive = Encoding.UTF8.GetString(buffer).ToString().Replace("\0", "");
// //Trace.ReadKey();
// Trace.Write(receive);
// }
// catch (Exception e)
// {
// Trace.Write("Error:" + e);
// break;
// }
//}
}
}
}
namespace blueTooth
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Trace.WriteLine("hello");
blueTooth1.Program.getDevice();
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
}
蓝牙消息发送部分
发送的是ConnectPacket 数据类型需要自己定义
在51单片机C语言程序里用SBUF 接收蓝牙发送的消息(注意大写)
private void send()
{
if (ConnectPacket != null)
{
if (blueTooth1.Program.isConnected)
{
try
{
string receive = string.Empty;
BluetoothClient BC = blueTooth1.Program.bluetoothClient;
Stream peerStream = BC.GetStream();
//byte[] buffer = new byte[255];
peerStream.WriteByte((byte)ConnectPacket);//发送ConnectPacket消息
//Trace.ReadKey();
// Trace.Write(ConnectPacket);
}
catch (Exception er)
{
Trace.Write("Error:" + er);
}
}
Thread.Sleep(100);
}
}