WinForm低功耗蓝牙通信BlueToothLE C# - mycls - 博客园
1.创建一个.net framework 4.6.1的wpf项目,4.6的不行,win7不支持蓝牙,网上很多方法都搞不定突然发现自己是4.6,改成4.6.1就成了,可能其他方法也是可以的
搜索到的蓝牙信号都是很奇怪的名字,也不支持搜索到手机蓝牙,后面再说吧
2.nuget安装 microsoft.windows.sdk.contracts,可以任意版本
3.将packages.config 迁移到 PackageReference
4.
using Windows.Devices.Enumeration;
using System.Threading;
上代码
public class BluetoothLEDev { private string _SearchBTName; /// <summary> /// 要查找的蓝牙名称 /// </summary> public string SearchBTName { get { return _SearchBTName; } set { _SearchBTName = value; } } private int _SearchBTRssi; /// <summary> /// 查找的蓝牙名称对应的信号强度 /// </summary> public int SearchBTRssi { get { return _SearchBTRssi; } set { _SearchBTRssi = value; } } /// <summary> /// 是否搜索停止了 /// </summary> private bool _SearchStopped; public bool SearchStopped { get { return _SearchStopped; } set { _SearchStopped = value; } } public void ScanBluetooth(string BTName, int timeOut = 10) { SearchBTName = BTName; SearchStopped = false; string[] requestedProperties = { "System.Devices.Aep.IsConnected", "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.Bluetooth.Le.IsConnectable", "System.Devices.Aep.SignalStrength", "System.Devices.Aep.IsPresent" }; string aqsAllBluetoothLEDevices = "(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")"; DeviceWatcher deviceWatcher = DeviceInformation.CreateWatcher(aqsAllBluetoothLEDevices, requestedProperties, DeviceInformationKind.AssociationEndpoint); deviceWatcher.Added += DeviceWatcher_Added; deviceWatcher.Updated += DeviceWatcher_Updated; deviceWatcher.Stopped += DeviceWatcher_Stopped; long TickTime = Environment.TickCount + timeOut * 1000;//系统经过的毫秒数 Task.Factory.StartNew(() => { while (true) { if (Environment.TickCount > TickTime || SearchStopped)//找n秒或已经找到 { string strTemp; if (!SearchStopped) { strTemp = string.Format("时间到了呀-->{0}", timeOut); //LogHelper.WriteLogInfo(strTemp); } strTemp = string.Format("结束搜索蓝牙"); //LogHelper.WriteLogInfo(strTemp); deviceWatcher.Stop(); return; } else { Thread.Sleep(50); } } }); deviceWatcher.Start(); //LogHelper.WriteLogInfo("开始搜索蓝牙"); } private void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args) { if (string.IsNullOrEmpty(args.Name)) { return; } var rssi = args.Properties.Single(d => d.Key == "System.Devices.Aep.SignalStrength").Value; if (rssi == null) { return; } int IRssi = int.Parse(rssi.ToString()); if (IRssi < -80) { return; } string strBTInfo = $"信号强度:[{IRssi}] 蓝牙名称:{(args.Name).PadRight(30, ' ')} 地址:{args.Id}"; Console.WriteLine(strBTInfo); //LogHelper.WriteLogInfo(strBTInfo); if (args.Name.Contains(SearchBTName)) { string strTemp = string.Format("搜索到蓝牙:{0},要查找的蓝牙名称:{1},信号强度:{2}找到了呀", args.Name, SearchBTName, IRssi); //LogHelper.WriteLogInfo(strTemp); SearchBTRssi = IRssi; SearchStopped = true; } } private void DeviceWatcher_Stopped(DeviceWatcher sender, object args) { SearchStopped = true; } private void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args) { } }
调用
public static int SearchBluetoothGetRssi(string strBTName, int OutTime) { BluetoothLEDev BT = new BluetoothLEDev(); BT.ScanBluetooth(strBTName, OutTime); long TickTime = Environment.TickCount + ((OutTime + 3) * 1000);//多3秒,防止另一个死机了这个退不出来 系统经过的毫秒数 while (true) { if (Environment.TickCount > TickTime || BT.SearchStopped)//找n秒或已经找到 { LogHelper.WriteLogInfo("返回值:" + BT.SearchBTRssi.ToString()); return BT.SearchBTRssi; } else { Thread.Sleep(50); } } }
运行结果如图
标签:SearchStopped,DeviceWatcher,return,string,c#,args,蓝牙,信号强度 From: https://www.cnblogs.com/ckrgd/p/18613059