pub struct DeviceGuard { /* private fields */ }Expand description
A guard that automatically disconnects from the device when dropped.
This provides RAII-style management of BLE connections. When the guard is dropped, it will attempt to disconnect from the device.
§Example
use aranet_core::{Device, DeviceGuard};
async fn read_with_guard() -> Result<(), Box<dyn std::error::Error>> {
let device = Device::connect("Aranet4 12345").await?;
let guard = DeviceGuard::new(device);
// Use the device through the guard
let reading = guard.read_current().await?;
println!("CO2: {}", reading.co2);
// Device is automatically disconnected when guard goes out of scope
Ok(())
}Implementations§
Source§impl DeviceGuard
impl DeviceGuard
Sourcepub fn into_inner(self) -> Device
pub fn into_inner(self) -> Device
Take ownership of the device, preventing automatic disconnect.
After calling this, you are responsible for disconnecting the device.
Sourcepub fn device_mut(&mut self) -> &mut Device
pub fn device_mut(&mut self) -> &mut Device
Get a mutable reference to the device.
Methods from Deref<Target = Device>§
Sourcepub async fn is_connected(&self) -> bool
pub async fn is_connected(&self) -> bool
Check if the device is connected.
Sourcepub async fn disconnect(&self) -> Result<()>
pub async fn disconnect(&self) -> Result<()>
Disconnect from the device.
This will:
- Abort all active notification handlers
- Disconnect from the BLE peripheral
Important: You MUST call this method before dropping the Device to ensure proper cleanup of BLE resources.
Sourcepub fn address(&self) -> &str
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.
Sourcepub fn device_type(&self) -> Option<DeviceType>
pub fn device_type(&self) -> Option<DeviceType>
Get the detected device type.
Sourcepub async fn read_rssi(&self) -> Result<i16>
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).
Sourcepub async fn read_characteristic(&self, uuid: Uuid) -> Result<Vec<u8>>
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.
Sourcepub async fn write_characteristic(&self, uuid: Uuid, data: &[u8]) -> Result<()>
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.
Sourcepub async fn read_current(&self) -> Result<CurrentReading>
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
Sourcepub async fn read_battery(&self) -> Result<u8>
pub async fn read_battery(&self) -> Result<u8>
Read the battery level (0-100).
Sourcepub async fn read_device_info(&self) -> Result<DeviceInfo>
pub async fn read_device_info(&self) -> Result<DeviceInfo>
Read device information.
This method reads all device info characteristics in parallel for better performance.
Sourcepub async fn subscribe_to_notifications<F>(
&self,
uuid: Uuid,
callback: F,
) -> Result<()>
pub async fn subscribe_to_notifications<F>( &self, uuid: Uuid, callback: F, ) -> Result<()>
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.
Sourcepub async fn unsubscribe_from_notifications(&self, uuid: Uuid) -> Result<()>
pub async fn unsubscribe_from_notifications(&self, uuid: Uuid) -> Result<()>
Unsubscribe from notifications on a characteristic.
Sourcepub async fn get_history_info(&self) -> Result<HistoryInfo>
pub async fn get_history_info(&self) -> Result<HistoryInfo>
Get information about the stored history.
Sourcepub async fn download_history(&self) -> Result<Vec<HistoryRecord>>
pub async fn download_history(&self) -> Result<Vec<HistoryRecord>>
Download all historical readings from the device.
Sourcepub async fn download_history_with_options(
&self,
options: HistoryOptions,
) -> Result<Vec<HistoryRecord>>
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_rateandradiation_totalfields in the returned records will beNone.
Sourcepub async fn download_history_v1(&self) -> Result<Vec<HistoryRecord>>
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.
Sourcepub async fn get_interval(&self) -> Result<MeasurementInterval>
pub async fn get_interval(&self) -> Result<MeasurementInterval>
Get the current measurement interval.
Sourcepub async fn set_interval(&self, interval: MeasurementInterval) -> Result<()>
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.
Sourcepub async fn set_smart_home(&self, enabled: bool) -> Result<()>
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).
Sourcepub async fn set_bluetooth_range(&self, range: BluetoothRange) -> Result<()>
pub async fn set_bluetooth_range(&self, range: BluetoothRange) -> Result<()>
Set the Bluetooth range.
Sourcepub async fn get_calibration(&self) -> Result<CalibrationData>
pub async fn get_calibration(&self) -> Result<CalibrationData>
Read calibration data from the device.
Sourcepub async fn get_settings(&self) -> Result<DeviceSettings>
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