Function peripheral_name

Source
pub fn peripheral_name(properties: &PeripheralProperties) -> Option<&str>
Expand description

The name of a peripheral as aranet uses it: the name aranet shows, matches a device query against and detects the sensor type from.

It is the name the device advertises, which btleplug reports as advertisement_name. On macOS only, a peripheral that has advertised no name gets the name CoreBluetooth holds for it, which btleplug reports as local_name.

Call this function and do not read either field yourself. advertisement_name alone leaves a macOS peripheral that has advertised no name without one. local_name means something different on each operating system in btleplug 0.13.5 (the files named below are that version’s):

  • Linux: BlueZ’s Alias (src/bluez/peripheral.rs:155): the name the user gave the device, else the device’s own name, else its address with dashes, such as 60-C0-BF-48-CB-8D (BlueZ 5.82 src/device.c:959-980). Read as a name, it would take a renamed sensor’s Aranet name away, and with it the sensor type that is detected from the name, and it would give a name to a device that has none. advertisement_name is BlueZ’s Name (src/bluez/peripheral.rs:156), which is what local_name was in btleplug 0.11.8.
  • Windows: the advertised name, until a connect replaces it with the system’s name for the device (src/winrtble/peripheral.rs:523-531). advertisement_name is the name from the last advertisement that carried one (src/winrtble/peripheral.rs:173-180), which is what local_name was in btleplug 0.11.8.
  • macOS: the advertised name once one has been heard, and until then the name CoreBluetooth holds for the peripheral (src/corebluetooth/internal.rs:1003-1008, src/corebluetooth/peripheral.rs:221-235). On macOS this function returns what local_name holds.

§Example

use aranet_core::peripheral_name;
use btleplug::api::PeripheralProperties;

// A sensor as BlueZ reports it after the user has renamed it "Kitchen".
let properties = PeripheralProperties {
    local_name: Some("Kitchen".to_string()),
    advertisement_name: Some("Aranet4 12345".to_string()),
    ..Default::default()
};
assert_eq!(peripheral_name(&properties), Some("Aranet4 12345"));