首次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();

}
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM