C# 蓝牙通讯

2021-05-19更新

蓝牙通讯要调用  第三方DLL文件  文件名为InTheHand.Net.Personal.dll, 下载链接: https://pan.baidu.com/s/1fHIWGPVC9UxO2QH2qnxdJw 提取码: y2ia

using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

namespace KHPEC.DAL
{
    public class BluetoothDAL
    {
        /// <summary>
        /// 搜索蓝牙
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, BluetoothAddress> GetClientBluetooth()
        {
            Dictionary<string, BluetoothAddress> dicBluetooth = new Dictionary<string, BluetoothAddress>();
            BluetoothClient client = new BluetoothClient();
            BluetoothDeviceInfo[] devices = client.DiscoverDevices();//搜索蓝牙 10秒钟
            foreach (BluetoothDeviceInfo d in devices)
            {
                dicBluetooth[d.DeviceName] = d.DeviceAddress;
            }
            return dicBluetooth;
        }

        /// <summary>
        /// 连接蓝牙
        /// </summary>
        /// <param name="address">蓝牙地址</param>
        /// <returns></returns>
        public bool ConnBluetooth(BluetoothClient blueclient, BluetoothAddress address)
        {
            try
            {
                if (blueclient.Connected)
                {
                    //已连蓝牙
                    return true;
                }
                else
                {
                    blueclient.Connect(address, BluetoothService.SerialPort);// BluetoothService.Handsfree
                    //蓝牙配对   蓝牙4.0以上不用PIN
                    return true;
                }

            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                //配对失败
                return false;
            }
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="blueclient"></param>
        /// <param name="data"></param>
        public void SendData(BluetoothClient blueclient, byte[] data)
        {
            blueclient.GetStream().Write(data, 0, data.Length);
        }

        /// <summary>
        /// 断开蓝牙
        /// </summary>
        /// <param name="blueclient"></param>
        public void BluetoothClose(BluetoothClient blueclient)
        {
            blueclient.Close();
        }

        /// <summary>
        /// 接收数据----注意 接收数据  一般要校验
        /// </summary>
        /// <param name="blueclient"></param>
        /// <param name="outTime">超时时间 单位毫秒</param>
        public List<byte> ReceiveData(BluetoothClient blueclient, int outTime)
        {
            DateTime dt1 = DateTime.Now;

            List<byte> temp = new List<byte>();
            while (true)
            {
                Stream peerStream = blueclient.GetStream();

                byte[] recvBytes = new byte[1024];
                int bytes = 0;

                if (peerStream.CanRead)
                {
                    bytes = peerStream.Read(recvBytes, 0, recvBytes.Length);
                    if (bytes > 0)
                    {
                        if (bytes <= 1024)
                        {
                            temp.AddRange(recvBytes.Take(bytes));
                            break;
                        }
                        else
                        {
                            temp.AddRange(recvBytes.Take(bytes));
                        }
                    }
                }

                DateTime dt2 = DateTime.Now;
                TimeSpan ts = dt2 - dt1;
                if (ts.TotalMilliseconds> outTime) break;
                //蓝牙传输速度限制
                Thread.Sleep(200);
            }

            return temp;
        }

    }
}