BluetoothLEAdvertisementWatcher未返回服务数据UUID

时间:2018-11-22 16:18:41

标签: bluetooth-lowenergy

我正在编写一个UWP应用程序,我正在使用BluetoothLEAdvertisementWatcher方法捕获来自BLE设备的广告。一切正常,我可以通过捕获BluetoothLEAdvertisementReceivedEventArgs .....来构建设备列表,如下所示

private async void LockerAdv_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{

    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => 
    {
        ServiceUuidsFound += args.Advertisement.ServiceUuids.Count;

        Adverts.Add(args);

但是,我想捕获广告数据中携带的ServiceData UUID(在我们的示例中,电池服务数据为0x180f,我们自己的服务数据为0xb991)。

如果我捕获了广告并检查了上图所示的Advertisement.ServiceUuids.Count,则该计数始终为零,即使我知道存在两个ServiceData UUID,并且像Nordic nRF应用这样的应用也会找到并显示它们。

此外,如果我使用调试器停止执行并检查Advertisement.ServiceUuid,则它们似乎没有被捕获,并且肯定无法访问,如下所示: Link to screenshot

我尝试使用

ScanningMode = BluetoothLEScanningMode.Active; 

ScanningMode = BluetoothLEScanningMode.Passive; 

而且没有区别。

最终,我想要的是能够从广告数据中提取ServiceData,因为其中包含对我们的应用程序有用的数据,但是如果Windows甚至不报告UUID的存在,那么我确定我无法获得与之相关的数据!

那么,我需要知道的是我做错了什么吗? Windows 10的限制(我使用的是最新版本)吗?还是我正在使用的Dell Optiflex问题?

将不胜感激地收到任何帮助

1 个答案:

答案 0 :(得分:0)

您正在注意错误。调试器和监视程序只是不允许您更深入地研究,并且不显示任何本机视图。 首先将所需的项目放在列表中,然后再查找列表中是否包含该项目,甚至还包含其他包含更多项目的集合。 下面的示例向您展示如何。我认为它不能涵盖所有项目,这取决于您:

    private async void OnAdvertisementReceivedAsync(BluetoothLEAdvertisementWatcher watcher,
                                               BluetoothLEAdvertisementReceivedEventArgs eventArgs)
    {
        //we have to stop the watcher to get the data from one advertising device only.           
        var device = await BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress);
        if (device != null)
        /* Check all advertisement items for null!
         * Not all of them are present!
         * Null check is not done in this example!*/
        {
            var TimeStamp = eventArgs.Timestamp.DateTime;
            var LocalName = eventArgs.Advertisement.LocalName;
            var Name = device.Name;
            var BleAdress = eventArgs.BluetoothAddress;//ulong
            var Rssi = eventArgs.RawSignalStrengthInDBm.ToString();
            var ConnectionStatus = device.ConnectionStatus;
            var Access = device.DeviceAccessInformation.CurrentStatus;
            /* Shows advertising flags:
                  LimitedDiscoverableMode = 1,
                  GeneralDiscoverableMode = 2,
                  ClassicNotSupported = 4,
                  DualModeControllerCapable = 8,
                  DualModeHostCapable = 16.
             */
            var flags = eventArgs.Advertisement.Flags.ToString();
            var AdvNumberOfDataSections = eventArgs.Advertisement.DataSections.Count;
            /*AdvDataSections contains the advertisement data */
            List<BluetoothLEAdvertisementDataSection> AdvDataSections = new List<BluetoothLEAdvertisementDataSection>();
            foreach (var item in eventArgs.Advertisement.DataSections)
            {
                AdvDataSections.Add(item);
            }
            List<BluetoothLEManufacturerData> AdvManufacturerData = new List<BluetoothLEManufacturerData>();
            foreach (var item in eventArgs.Advertisement.ManufacturerData)
            {
                AdvManufacturerData.Add(item);
            }
            List<GattDeviceService> ServicesList = new List<GattDeviceService>();
            var services = await device.GetGattServicesAsync(BluetoothCacheMode.Uncached);
            if (services != null)
            {
                foreach (var item in services.Services)
                {
                    ServicesList.Add(item);
                }
            }
        }
      /* Start the watcher again to get other devices or missing services or data */
    }