PC上面的蓝牙的通信(C#)

添加引用InTheHand.Net.Personal.dll

首先创建一个蓝牙类

1 class LanYa {
2 public string blueName { get; set; }                                   //l蓝牙名字
3 public BluetoothAddress blueAddress { get; set; }               //蓝牙的唯一标识符
4 public ClassOfDevice blueClassOfDevice { get; set; }            //蓝牙是何种类型
5 public bool IsBlueAuth { get; set; }                                    //指定设备通过验证
6 public bool IsBlueRemembered { get; set; }                        //记住设备
7 public DateTime blueLastSeen { get; set; }
8 public DateTime blueLastUsed { get; set; }
9 }

 

然后就是搜索设备

1 List<LanYa> lanYaList = new List<LanYa>();                //搜索到的蓝牙的集合
2 BluetoothClient client = new BluetoothClient();            
3 BluetoothRadio radio = BluetoothRadio.PrimaryRadio;  //获取蓝牙适配器
4 radio.Mode = RadioMode.Connectable;                        
5 BluetoothDeviceInfo[] devices = client.DiscoverDevices();//搜索蓝牙  10秒钟
6 foreach (var item in devices) {
7 lanYaList.Add(new LanYa { blueName = item.DeviceName, blueAddress = item.DeviceAddress, blueClassOfDevice = item.ClassOfDevice, IsBlueAuth = item.Authenticated, IsBlueRemembered = item.Remembered, blueLastSeen = item.LastSeen, blueLastUsed = item.LastUsed });//把搜索到的蓝牙添加到集合中
8 }

 

蓝牙的配对

1 BluetoothClient blueclient = new BluetoothClient();
2 Guid mGUID1 = BluetoothService.Handsfree;       //蓝牙服务的uuid
3 
4  blueclient.Connect(s.blueAddress, mGUID)      //开始配对  蓝牙4.0不需要setpin

 

客户端

 1 BluetoothClient bl = new BluetoothClient();//
 2 Guid mGUID2 = Guid.Parse("00001101-0000-1000-8000-00805F9B34FB");//蓝牙串口服务的uuiid
 3 
 4 try
 5 {
 6 bl.Connect(s.blue_address, mGUID);
 7 //"连接成功";
 8 }
 9 catch(Exception x)
10 {
11 //异常
12 }
13 
14 var v = bl.GetStream();
15 byte[] sendData = Encoding.Default.GetBytes(“人生苦短,我用python”);
16 v.Write(sendData, 0, sendData.Length);              //发送

 

 

服务器端

 1 bluetoothListener = new BluetoothListener(mGUID2);
 2 bluetoothListener.Start();//开始监听
 3 
 4 bl = bluetoothListener.AcceptBluetoothClient();//接收
 5 
 6 
 7 while (true)
 8 {
 9 byte[] buffer = new byte[100];
10 Stream peerStream = bl.GetStream();
11 
12 peerStream.Read(buffer, 0, buffer.Length);
13 
14 string data= Encoding.UTF8.GetString(buffer).ToString().Replace("\0", "");//去掉后面的\0字节
15 }

 

基本上就是这些吧!

 

转载于:https://www.cnblogs.com/xiaobowudi/p/5915746.html

Unity可以使用C#脚本来实现与蓝牙设备的通信。首先需要使用蓝牙设备的SDK或API来连接蓝牙设备,并获取设备的ID或地址。 在Unity中,可以使用C#的SerialPort类来实现串口通信,因为蓝牙设备在计算机上被识别为串口设备。以下是一个示例代码,用于连接蓝牙设备并发送数据: ```csharp using System.IO.Ports; SerialPort bluetooth = new SerialPort("COM1", 9600); // Replace COM1 with your Bluetooth device port and 9600 with your device's baud rate bluetooth.Open(); // Open the port if (bluetooth.IsOpen) // Check if the port is open { bluetooth.Write("Hello, World!"); // Send data to the device } bluetooth.Close(); // Close the port ``` 请注意,此示例代码将COM1用作蓝牙设备端口。您需要将其替换为您的蓝牙设备端口。还需要将9600替换为您的设备的波特率。 为了接收数据,您可以在SerialPort对象上注册一个DataReceived事件,然后在事件处理程序中处理数据。以下是一个示例代码: ```csharp using System.IO.Ports; SerialPort bluetooth = new SerialPort("COM1", 9600); // Replace COM1 with your Bluetooth device port and 9600 with your device's baud rate bluetooth.DataReceived += Bluetooth_DataReceived; // Register the DataReceived event void Bluetooth_DataReceived(object sender, SerialDataReceivedEventArgs e) { string data = bluetooth.ReadLine(); // Read the incoming data Debug.Log(data); // Output the data to the console } bluetooth.Open(); // Open the port ``` 请注意,这里使用了ReadLine()方法来读取接收到的数据。如果您的蓝牙设备使用不同的数据格式,请相应地更改代码。 总的来说,使用C#的SerialPort类与蓝牙设备通信是相对简单的,并且可以在Unity中轻松实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值