C#蓝牙通讯 蓝牙模块HC-06

编程工具: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);
            }
        }
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值