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

异常如下

 

 

【尝试一】

百度搜索cc7b13ffcd2ddd51找到类似情况

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

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

问题并没有解决

 

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

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

 

 

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

 

 

【尝试二】

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

 

 

 

结果还是不行

 

 

 

【尝试三】

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

结果还是不行!!!

 

 

最后!!!!!!

我看到了这段话

 

 

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

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

 

 

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

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

 

 源代码如下:

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<LanYa> lanYaList = new List<LanYa>(); //搜索到的蓝牙的集合
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值