首次c#蓝牙开发踩坑记录

异常如下

 首次c#蓝牙开发踩坑记录_第1张图片

 

【尝试一】

百度搜索cc7b13ffcd2ddd51找到类似情况

https://learningintheopen.org/tag/publickeytokencc7b13ffcd2ddd51/

重新下载安装最新的.Net Framework

问题并没有解决

 

仔细一看我和上面这个博客的问题不一样!!!

我复制了异常的详细信息如下:

 

 

异常提示我找不到System.Configuration.configurationManager这个程序集,但是我的SDK里面明明有这个包。

 首次c#蓝牙开发踩坑记录_第2张图片

 

【尝试二】

但是.NETCore的版本只有2.1比官网最新的2.2要旧,所以我想重装.NET core试试

 首次c#蓝牙开发踩坑记录_第3张图片

 

 

结果还是不行

 

 

 

【尝试三】

打开手动找到system.configuration的dll文件并加入引用

结果还是不行!!!

 

 

最后!!!!!!

我看到了这段话

 首次c#蓝牙开发踩坑记录_第4张图片

 

原来我最初创建的项目类型就出了问题。

应该创建这个.NET Framework控制台应用

 首次c#蓝牙开发踩坑记录_第5张图片

 

成功跑通蓝牙的第一个程序

获取蓝牙适配器并输出本地计算机蓝牙相关信息。

 首次c#蓝牙开发踩坑记录_第6张图片

 源代码如下:

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

namespace ConsoleApp2
{
class LanYa
{
public string blueName { get; set; } //蓝牙名字
public BluetoothAddress blueAddress { get; set; } //蓝牙的唯一标识符
public ClassOfDevice blueClassOfDevice { get; set; } //蓝牙是何种类型
public bool IsBlueAuth { get; set; } //指定设备通过验证
public bool IsBlueRemembered { get; set; } //记住设备
public DateTime blueLastSeen { get; set; }
public DateTime blueLastUsed { get; set; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world!");
BluetoothRadio radio = BluetoothRadio.PrimaryRadio;//获取蓝牙适配器
if (radio == null)
{
Console.WriteLine("没有找到本机蓝牙设备!");
}
else
{
Console.WriteLine("ClassOfDevice: " + radio.ClassOfDevice);
Console.WriteLine("HardwareStatus: " + radio.HardwareStatus);
Console.WriteLine("HciRevision: " + radio.HciRevision);
Console.WriteLine("HciVersion: " + radio.HciVersion);
Console.WriteLine("LmpSubversion: " + radio.LmpSubversion);
Console.WriteLine("LmpVersion: " + radio.LmpVersion);
Console.WriteLine("LocalAddress: " + radio.LocalAddress);
Console.WriteLine("Manufacturer: " + radio.Manufacturer);
Console.WriteLine("Mode: " + radio.Mode);
Console.WriteLine("Name: " + radio.Name);
Console.WriteLine("Remote:" + radio.Remote);
Console.WriteLine("SoftwareManufacturer: " + radio.SoftwareManufacturer);
Console.WriteLine("StackFactory: " + radio.StackFactory);
}
//Console.ReadKey();


List lanYaList = new List(); //搜索到的蓝牙的集合
BluetoothClient client = new BluetoothClient();
radio.Mode = RadioMode.Connectable;
BluetoothDeviceInfo[] devices = client.DiscoverDevices();//搜索蓝牙 10秒钟
int count = 0;
foreach (var item in devices)
{
count++;
Console.WriteLine("===========蓝牙设备"+count+"================");
Console.WriteLine("device name:" + item.DeviceName);//输出每个蓝牙设备的名字
Console.WriteLine("device address:" + item.DeviceAddress);//输出每个蓝牙设备的名字
Console.WriteLine("ClassOfDevice:" + item.ClassOfDevice);
Console.WriteLine("Authenticated:" + item.Authenticated);
Console.WriteLine("Remembered:" + item.Remembered);
Console.WriteLine("LastSeen:" + item.LastSeen);
Console.WriteLine("LastUsed:" + item.LastUsed);
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 });//把搜索到的蓝牙添加到集合中
}
Console.WriteLine("device count:" + devices.Length);//输出搜索到的蓝牙设备个数


蓝牙的配对
//BluetoothClient blueclient = new BluetoothClient();
//Guid mGUID1 = BluetoothService.Handsfree; //蓝牙服务的uuid

//blueclient.Connect(s.blueAddress, mGUID) //开始配对 蓝牙4.0不需要setpin


BluetoothDeviceInfo dev = devices[0];

//客户端
BluetoothClient bl = new BluetoothClient();//
Guid mGUID = Guid.Parse("0000fff4-0000-1000-8000-00805F9B34FB");//蓝牙串口服务的uuiid

//byte[] def = { 0x13, 0x00, 0x05, 0x15, 0x11, 0x08, 0x01, 0x10, 0x02, 0x01, 0x12, 0x14 };

//string s = "D05FB81A21CE";
//byte[] def = Encoding.Default.GetBytes(s);
//string str2 = BitConverter.ToString(def);
//Console.WriteLine(str2);

//BluetoothAddress abc = new BluetoothAddress(def);

try+
{
//bl.Connect(abc, mGUID);
bl.Connect(dev.DeviceAddress, mGUID);
Console.WriteLine("连接成功");
//"连接成功";
}
catch (Exception x)
{
Console.WriteLine("连接异常");
//异常
}

//var v = bl.GetStream();
//byte[] sendData = Encoding.Default.GetBytes(“人生苦短,我用python”);
//v.Write(sendData, 0, sendData.Length); //发送


//服务器端
BluetoothListener bluetoothListener = new BluetoothListener(mGUID);
bluetoothListener.Start();//开始监听

bl = bluetoothListener.AcceptBluetoothClient();//接收


while (true)
{
Console.WriteLine("111111111");
byte[] buffer = new byte[100];
Stream peerStream = bl.GetStream();

peerStream.Read(buffer, 0, buffer.Length);

string data = Encoding.UTF8.GetString(buffer).ToString().Replace("\0", "");//去掉后面的\0字节
}
Console.ReadKey();

}
}
}

转载于:https://www.cnblogs.com/nan424164292/p/10683160.html

你可能感兴趣的:(首次c#蓝牙开发踩坑记录)