简体   繁体   English

BLE广告 - 获取设备显示名称

[英]BLE advert — get the device display name

It's pretty straightforward to listen for BLE advertisements and get the BluetoothAddress, signal strength etc from the advert. 听取BLE广告并从广告中获取BluetoothAddress,信号强度等非常简单。 This page says 这个页面

Once you have the address, you can call BluetoothLEDevice.FromBluetoothAddressAsync to get a reference to the device. 获得地址后,可以调用BluetoothLEDevice.FromBluetoothAddressAsync来获取对设备的引用。

so I did and this gave me a BluetoothLEDevice object with a Name property. 所以我做了,这给了我一个带有Name属性的BluetoothLEDevice对象。 Unfortunately the name is frequently - but not always - an empty string. 不幸的是,这个名字经常 - 但不总是 - 是一个空字符串。

When I interactively scan for Bluetooth devices, Windows shows me a list of names for devices it can see (excluding already paired devices). 当我以交互方式扫描蓝牙设备时,Windows会向我显示它可以看到的设备的名称列表(不包括已配对的设备)。 It does this very quickly and it shows a number of devices that don't ever show up in the names accumulated from advertising. 它很快就能做到这一点,它显示了许多设备,这些设备从广告中积累的名称中都没有出现

Is there a reliable strategy for quickly obtaining the name normally shown when computers and phones list unpaired devices? 是否有可靠的策略来快速获取通常在计算机和电话列出未配对设备时显示的名称?


I tried the suggestion from Emil, and while this simplifies my code by eliminating the need to obtain a BluetoothLEDevice object, the fundamental problem remains: advertisements cannot be relied upon to supply a name. 我尝试了Emil的建议,虽然通过消除获取BluetoothLEDevice对象的需要简化了我的代码,但基本问题仍然存在:不能依赖广告来提供名称。 They don't even contain meaningful manufacturer data. 它们甚至不包含有意义的制造商数据。

The code currently looks like this 代码目前看起来像这样

    BluetoothLEAdvertisementWatcher watcher = new BluetoothLEAdvertisementWatcher();
    ...
        watcher.Received += OnAdvertisementReceived;
        watcher.Start();
    ...
    private void BleAdvertHandlerAsync(BluetoothLEAdvertisementReceivedEventArgs args)
    {
      var localName = args.Advertisement.LocalName;
      ...
    }

Fishing the local name into a variable sidesteps the fact that resolving the value entails a COM call which is not allowed in a breakpoint expression. 将本地名称捕获到变量中可以避免解析该值需要在断点表达式中不允许的COM调用这一事实。

Playing with the switches on my mouse and keyboard, which can be paired with three different hosts and switched between them, I notice that when I tell them to connect to another host I immediately get advertisements containing names. 玩我的鼠标和键盘上的开关,可以与三个不同的主机配对,并在它们之间切换,我注意到当我告诉他们连接到另一个主机时,我立即得到包含名字的广告。 If the host isn't present there is a steady stream of them that ceases when I switch back to my computer and a session is established. 如果主机不存在,则当我切换回计算机并建立会话时,会有稳定的主机停止。

This suggests that advertisements are not the way Windows populates its list of unpaired hosts. 这表明广告不是Windows填充其未配对主机列表的方式。

If you use the advertisement watcher you can check the BluetoothLEAdvertisement.LocalName property. 如果您使用广告观察程序,则可以检查BluetoothLEAdvertisement.LocalName属性。

For your second question, maybe your phone is discovered through Bluetooth Classic? 对于您的第二个问题,也许您的手机是通过蓝牙经典发现的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM