Struct Device

Source
pub struct Device { /* private fields */ }
Expand description

Represents a connected Aranet device.

§Note on Clone

This struct intentionally does not implement Clone. A Device represents an active BLE connection with associated state (services discovered, notification handlers, etc.). Cloning would create ambiguity about connection ownership and could lead to resource conflicts. If you need to share a device across multiple tasks, wrap it in Arc<Device>.

§Cleanup

You MUST call Device::disconnect before dropping the device to properly release BLE resources. If a Device is dropped without calling disconnect, a warning will be logged.

Implementations§

Source§

impl Device

Source

pub async fn connect(identifier: &str) -> Result<Self>

Connect to an Aranet device by name or MAC address.

§Example
use aranet_core::device::Device;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let device = Device::connect("Aranet4 12345").await?;
    println!("Connected to {:?}", device);
    Ok(())
}
Source

pub async fn connect_with_timeout( identifier: &str, timeout: Duration, ) -> Result<Self>

Connect to an Aranet device with a custom scan timeout.

Source

pub async fn from_peripheral( adapter: Adapter, peripheral: Peripheral, ) -> Result<Self>

Create a Device from an already-discovered peripheral.

Source

pub async fn is_connected(&self) -> bool

Check if the device is connected.

Source

pub async fn disconnect(&self) -> Result<()>

Disconnect from the device.

This will:

  1. Abort all active notification handlers
  2. Disconnect from the BLE peripheral

Important: You MUST call this method before dropping the Device to ensure proper cleanup of BLE resources.

Source

pub fn name(&self) -> Option<&str>

Get the device name.

Source

pub fn address(&self) -> &str

Get the device address or identifier.

On Linux and Windows, this returns the Bluetooth MAC address (e.g., “AA:BB:CC:DD:EE:FF”). On macOS, this returns a UUID identifier since MAC addresses are not exposed.

Source

pub fn device_type(&self) -> Option<DeviceType>

Get the detected device type.

Source

pub async fn read_rssi(&self) -> Result<i16>

Read the current RSSI (signal strength) of the connection.

Returns the RSSI in dBm. More negative values indicate weaker signals. Typical values range from -30 (strong) to -90 (weak).

Source

pub async fn read_characteristic(&self, uuid: Uuid) -> Result<Vec<u8>>

Read a characteristic value by UUID.

This method includes a timeout to prevent indefinite hangs on BLE operations. The default timeout is 10 seconds.

Source

pub async fn write_characteristic(&self, uuid: Uuid, data: &[u8]) -> Result<()>

Write a value to a characteristic.

This method includes a timeout to prevent indefinite hangs on BLE operations. The default timeout is 10 seconds.

Source

pub async fn read_current(&self) -> Result<CurrentReading>

Read current sensor measurements.

Automatically selects the correct characteristic UUID based on device type:

  • Aranet4 uses f0cd3001
  • Aranet2, Radon, Radiation use f0cd3003
Source

pub async fn read_battery(&self) -> Result<u8>

Read the battery level (0-100).

Source

pub async fn read_device_info(&self) -> Result<DeviceInfo>

Read device information.

This method reads all device info characteristics in parallel for better performance.

Source

pub async fn subscribe_to_notifications<F>( &self, uuid: Uuid, callback: F, ) -> Result<()>
where F: Fn(&[u8]) + Send + Sync + 'static,

Subscribe to notifications on a characteristic.

The callback will be invoked for each notification received. The notification handler task is tracked and will be aborted when disconnect() is called.

Source

pub async fn unsubscribe_from_notifications(&self, uuid: Uuid) -> Result<()>

Unsubscribe from notifications on a characteristic.

Source§

impl Device

Source

pub async fn get_history_info(&self) -> Result<HistoryInfo>

Get information about the stored history.

Source

pub async fn download_history(&self) -> Result<Vec<HistoryRecord>>

Download all historical readings from the device.

Source

pub async fn download_history_with_options( &self, options: HistoryOptions, ) -> Result<Vec<HistoryRecord>>

Download historical readings with custom options.

§Device Support
  • Aranet4: Downloads CO₂, temperature, pressure, humidity
  • Aranet2: Downloads temperature, humidity
  • AranetRn+ (Radon): Downloads radon, temperature, pressure, humidity
  • Aranet Radiation: Not yet supported - will return Aranet4-style records with placeholder values. The radiation_rate and radiation_total fields in the returned records will be None.
Source

pub async fn download_history_v1(&self) -> Result<Vec<HistoryRecord>>

Download history using V1 protocol (notification-based).

This is used for older devices that don’t support the V2 read-based protocol. V1 uses notifications on the HISTORY_V1 characteristic.

Source§

impl Device

Source

pub async fn get_interval(&self) -> Result<MeasurementInterval>

Get the current measurement interval.

Source

pub async fn set_interval(&self, interval: MeasurementInterval) -> Result<()>

Set the measurement interval.

The device will start using the new interval after the current measurement cycle completes.

Source

pub async fn set_smart_home(&self, enabled: bool) -> Result<()>

Enable or disable Smart Home integration.

When enabled, the device advertises sensor data that can be read without connecting (passive scanning).

Source

pub async fn set_bluetooth_range(&self, range: BluetoothRange) -> Result<()>

Set the Bluetooth range.

Source

pub async fn get_calibration(&self) -> Result<CalibrationData>

Read calibration data from the device.

Source

pub async fn get_settings(&self) -> Result<DeviceSettings>

Read device settings from the SENSOR_STATE characteristic.

This reads the device configuration including:

  • Smart Home integration status
  • Bluetooth range setting
  • Temperature display unit
  • Radon display unit (for Aranet Radon devices)
  • Buzzer settings
  • Calibration settings

Trait Implementations§

Source§

impl AranetDevice for Device

Source§

fn is_connected<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Check if the device is connected.
Source§

fn disconnect<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Disconnect from the device.
Source§

fn name(&self) -> Option<&str>

Get the device name, if available.
Source§

fn address(&self) -> &str

Get the device address or identifier. Read more
Source§

fn device_type(&self) -> Option<DeviceType>

Get the detected device type, if available.
Source§

fn read_current<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<CurrentReading>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Read the current sensor values.
Source§

fn read_device_info<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<DeviceInfo>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Read device information (model, serial, firmware version, etc.).
Source§

fn read_rssi<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<i16>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Read the current RSSI (signal strength) in dBm. Read more
Source§

fn read_battery<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<u8>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Read the battery level (0-100).
Source§

fn get_history_info<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<HistoryInfo>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Get information about stored history.
Source§

fn download_history<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<HistoryRecord>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Download all historical readings.
Source§

fn download_history_with_options<'life0, 'async_trait>( &'life0 self, options: HistoryOptions, ) -> Pin<Box<dyn Future<Output = Result<Vec<HistoryRecord>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Download historical readings with custom options.
Source§

fn get_interval<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<MeasurementInterval>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Get the current measurement interval.
Source§

fn set_interval<'life0, 'async_trait>( &'life0 self, interval: MeasurementInterval, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Set the measurement interval.
Source§

fn get_calibration<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<CalibrationData>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Read calibration data from the device.
Source§

fn connect<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Connect to the device. Read more
Source§

impl Debug for Device

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl DeviceStreamExt for Device

Source§

fn stream(self: Arc<Self>) -> ReadingStream

Create a reading stream with default options. Read more
Source§

fn stream_with_options(self: Arc<Self>, options: StreamOptions) -> ReadingStream

Create a reading stream with custom options.
Source§

impl Drop for Device

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl !Freeze for Device

§

impl !RefUnwindSafe for Device

§

impl Send for Device

§

impl Sync for Device

§

impl Unpin for Device

§

impl !UnwindSafe for Device

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more