C# 蓝牙通讯

首次发布:2018-08-20
2021-05-19更新

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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;
        }
 
    }
}