windows BLE编程 net winform 连接蓝牙4.0--新手补充
本文是对一些新手想通过pc访问ble蓝牙的一些补充帮助新手跳坑。
首先 我用的是win10 vs2019 .net4.6
重点文章先看–转自: https://www.cnblogs.com/webtojs/p/9675956.html#!comments
这里写的很详细,可以引用他的dll,也可以自己编写,(如果自己编写using Windows.Devices.Bluetooth;提示做错误的话请仔细看下面的评论),下面是我的问from调用方式:
public partial class Form1 : Form
{
BluetoothLECode bluetooth;
string BluetoothLE = "";
public Form1()
{
InitializeComponent();
BluetoothLE= System.Configuration.ConfigurationManager.AppSettings["BluetoothLE"];
string _serviceGuid = "0000ffe0-0000-1000-8000-00805f9b34fb";//蓝牙服务的uuid;
string _writeCharacteristicGuid = "0000ffe1-0000-1000-8000-00805f9b34fb";//蓝牙写的uuid;
string _notifyCharacteristicGuid = "0000ffe1-0000-1000-8000-00805f9b34fb";//蓝牙读的uuid;
bluetooth = new BluetoothLECode(_serviceGuid, _writeCharacteristicGuid, _notifyCharacteristicGuid);
bluetooth.ValueChanged += Bluetooth_ValueChanged;
}
private void Bluetooth_ValueChanged(MsgType type, string str, byte[] data)
{
Console.WriteLine(str);
}
private void button1_Click(object sender, EventArgs e)
{
bluetooth.StartBleDeviceWatcher();
}
private async void OnBLEAdded()
{
await bluetooth.SelectDeviceFromIdAsync("a8:e2:c1:63:46:de");//蓝牙设备MAC地址
}
}
其中那三个uuid不知道也没有关系可以通过安卓的一个小程序获取,具体可以看这个网址:https://www.jianshu.com/p/d991f0fdec63
用Android Studio创一个小软件(如果不会写文章的最后有一个git地址可以参考),文章中会提到怎么获取uuid,将uuid放入对应的代码中程序就可以运行读取数据了。