Windows系统蓝牙扫描和Wifi扫描的区别

windows蓝牙设备

使用场景:使用windows.md和windows.Devices.md的c++文件库扫描蓝牙,通常只需要打开监听即可自动扫描蓝牙设备。
出现的问题:蓝牙较多的时候扫描过于频繁会造成系统服务占用cpu使用率过高;
解决办法:蓝牙设备扫描间隔可以扫描过一次后关闭监听并间隔一段时间后再次打开监听;

        public Task StartWifiCheck(Action init, Action<List<WifiCheckInfo>> action,Action conAct,AutoResetEvent auto, int waitTime = 5000)
        {
            return Task.Run(() =>
            {
                while (true)
                {
                    var list = new List<WifiCheckInfo>();
                    if (auto.WaitOne(waitTime))
                    {
                        try
                        {
                            init?.Invoke();
                            WlanClient client = new WlanClient();
                            var r = Parallel.ForEach(client.Interfaces, bssworks =>
                            {
                                lock (list)
                                {
                                    bssworks.Scan();
                                    Wlan.WlanBssEntry[] bwks = bssworks.GetNetworkBssList();
                                    foreach (var bss in bwks)
                                    {
                                        var lkQ = bss.linkQuality;
                                        var ssid = Encoding.UTF8.GetString(bss.dot11Ssid.SSID, 0, (int)bss.dot11Ssid.SSIDLength);
                                        var mac = BitConverter.ToString(bss.dot11Bssid).Replace("-", "").Replace(":", "");
                                        string rssi = bss.rssi.ToString();
                                        if (!list.Exists(p => p.MacCode == mac))
                                        {
                                            list.Add(new WifiCheckInfo
                                            {
                                                RSSI = rssi,
                                                SearchTime = GlobalStaticClass.CurrentServTime,
                                                WifiSSID = ssid,
                                                MacCode = mac,
                                                Type = "SSID",
                                                LinkQuality = lkQ
                                            });
                                        }
                                    }
                                }

                            });

                            if (r.IsCompleted)
                            {
                                action?.Invoke(list);
                            }

                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            auto.Set();
                        }
                    }
                    else
                    {
                        auto.Set();
                    }
                    conAct?.Invoke();
                };
            });
        }

windows WiFi设备

使用场景:使用c#的wlan库扫描设备时候,需要循环所有的interface接口。
出现的问题:仅通过循环interface获取wifi导致扫描到的设备不够实时,会有很大的延迟。
原因:未调用主动扫描的方法时,wifi是处于低功耗模式下运行,会间隔一段时间扫描设备,以降低系统cpu的使用率。
解决办法:需要调用ScanWlan(c++)或者interface.Scan(c#)方法进行主动扫描。

/// <summary>
/// 搜索蓝牙设备
/// </summary>
public void StartBleDeviceWatcher()
{
    Watcher = new BluetoothLEAdvertisementWatcher();

    Watcher.ScanningMode = BluetoothLEScanningMode.Passive;
    // only activate the watcher when we're recieving values >= -80
    //Watcher.SignalStrengthFilter.InRangeThresholdInDBm = -80;

    // stop watching if the value drops below -90 (user walked away)
    //Watcher.SignalStrengthFilter.OutOfRangeThresholdInDBm = -90;

    // register callback for when we see an advertisements
    Watcher.Received += OnAdvertisementReceived;

    // wait 5 seconds to make sure the device is really out of range
    Watcher.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(5000);
    Watcher.SignalStrengthFilter.SamplingInterval = TimeSpan.FromMilliseconds(2000);

    // starting watching for advertisements
    Watcher.Start();

    Console.WriteLine("自动发现设备中..");
}
//回调的得到的蓝牙设备信息可通过此action获取到数据
 public Action<BluetoothCheckInfo> DeviceWatcherChanged { get; set; }
 private List<string> tmpNames = new List<string>();
 /// <summary>
/// 蓝牙搜索回调,
/// </summary>
 private void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
 {
     BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress,BluetoothAddressType.Public).Completed = (asyncInfo, asyncStatus) =>
     {
         if (asyncStatus == AsyncStatus.Completed)
         {
             BluetoothLEDevice currentDevice = asyncInfo.GetResults();
             //Console.WriteLine("BleDev"+currentDevice);
             if (currentDevice!=null && !tmpNames.Contains(currentDevice?.Name.ToUpper()))
             {
                 tmpNames.Add(currentDevice?.Name.ToUpper());
                 DeviceWatcherChanged?.Invoke(new BluetoothCheckInfo
                 {
                     Name = currentDevice.Name,
                 });
             }
         }
     };
 }
/// <summary>
/// 停止搜索蓝牙
/// </summary>
public void StopBleDeviceWatcher()
{
    if (Watcher != null)
        this.Watcher.Stop();
}

总结

对于蓝牙和wifi设备的使用,可以通过相关的方法进行设备操作。记录下遇见的问题,方便下次查阅。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cathedra

谢谢老板的飞机,老板大气!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值