简体   繁体   中英

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. This page says

Once you have the address, you can call BluetoothLEDevice.FromBluetoothAddressAsync to get a reference to the device.

so I did and this gave me a BluetoothLEDevice object with a Name property. 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). 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. 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.

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.

If you use the advertisement watcher you can check the BluetoothLEAdvertisement.LocalName property.

For your second question, maybe your phone is discovered through Bluetooth Classic?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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