pub struct BluetoothSession { /* private fields */ }
Expand description

A connection to the Bluetooth daemon. This can be cheaply cloned and passed around to be used from different places. It is the main entry point to the library.

Implementations§

source§

impl BluetoothSession

source

pub async fn new( ) -> Result<(impl Future<Output = Result<(), SpawnError>>, Self), BluetoothError>

Establish a new D-Bus connection to communicate with BlueZ.

Returns a tuple of (join handle, Self). If the join handle ever completes then you’re in trouble and should probably restart the process.

source

pub async fn start_discovery(&self) -> Result<(), BluetoothError>

Power on all Bluetooth adapters, remove any discovery filter, and then start scanning for devices.

This is equivalent to calling start_discovery_with_filter(&DiscoveryFilter::default()).

source

pub async fn start_discovery_on_adapter( &self, adapter: &AdapterId ) -> Result<(), BluetoothError>

Power on the given Bluetooth adapter, remove any discovery filter, and then start scanning for devices.

This is equivalent to calling start_discovery_on_adapter_with_filter(adapter, &DiscoveryFilter::default()).

source

pub async fn start_discovery_with_filter( &self, discovery_filter: &DiscoveryFilter ) -> Result<(), BluetoothError>

Power on all Bluetooth adapters, set the given discovery filter, and then start scanning for devices.

Note that BlueZ combines discovery filters from all clients and sends events matching any filter to all clients, so you may receive unexpected discovery events if there are other clients on the system using Bluetooth as well.

In most common cases, DiscoveryFilter::default() is fine.

source

pub async fn start_discovery_on_adapter_with_filter( &self, adapter_id: &AdapterId, discovery_filter: &DiscoveryFilter ) -> Result<(), BluetoothError>

Power on the given Bluetooth adapter, set the given discovery filter, and then start scanning for devices.

Note that BlueZ combines discovery filters from all clients and sends events matching any filter to all clients, so you may receive unexpected discovery events if there are other clients on the system using Bluetooth as well.

In most common cases, DiscoveryFilter::default() is fine.

source

pub async fn stop_discovery(&self) -> Result<(), BluetoothError>

Stop scanning for devices on all Bluetooth adapters.

source

pub async fn stop_discovery_on_adapter( &self, adapter_id: &AdapterId ) -> Result<(), BluetoothError>

Stop scanning for devices on the given Bluetooth adapters.

source

pub async fn get_adapters(&self) -> Result<Vec<AdapterInfo>, BluetoothError>

Get a list of all Bluetooth adapters on the system.

source

pub async fn get_devices(&self) -> Result<Vec<DeviceInfo>, BluetoothError>

Get a list of all Bluetooth devices which have been discovered so far.

source

pub async fn get_devices_on_adapter( &self, adapter: &AdapterId ) -> Result<Vec<DeviceInfo>, BluetoothError>

Get a list of all Bluetooth devices which have been discovered so far on a given adapter.

source

pub async fn get_services( &self, device: &DeviceId ) -> Result<Vec<ServiceInfo>, BluetoothError>

Get a list of all GATT services which the given Bluetooth device offers.

Note that this won’t be filled in until the device is connected.

source

pub async fn get_characteristics( &self, service: &ServiceId ) -> Result<Vec<CharacteristicInfo>, BluetoothError>

Get a list of all characteristics on the given GATT service.

source

pub async fn get_descriptors( &self, characteristic: &CharacteristicId ) -> Result<Vec<DescriptorInfo>, BluetoothError>

Get a list of all descriptors on the given GATT characteristic.

source

pub async fn get_service_by_uuid( &self, device: &DeviceId, uuid: Uuid ) -> Result<ServiceInfo, BluetoothError>

Find a GATT service with the given UUID advertised by the given device, if any.

Note that this generally won’t work until the device is connected.

source

pub async fn get_characteristic_by_uuid( &self, service: &ServiceId, uuid: Uuid ) -> Result<CharacteristicInfo, BluetoothError>

Find a characteristic with the given UUID as part of the given GATT service advertised by a device, if there is any.

source

pub async fn get_service_characteristic_by_uuid( &self, device: &DeviceId, service_uuid: Uuid, characteristic_uuid: Uuid ) -> Result<CharacteristicInfo, BluetoothError>

Convenience method to get a GATT charactacteristic with the given UUID advertised by a device as part of the given service.

This is equivalent to calling get_service_by_uuid and then get_characteristic_by_uuid.

source

pub async fn get_device_info( &self, id: &DeviceId ) -> Result<DeviceInfo, BluetoothError>

Get information about the given Bluetooth device.

source

pub async fn get_adapter_info( &self, id: &AdapterId ) -> Result<AdapterInfo, BluetoothError>

Get information about the given Bluetooth adapter.

source

pub async fn get_service_info( &self, id: &ServiceId ) -> Result<ServiceInfo, BluetoothError>

Get information about the given GATT service.

source

pub async fn get_characteristic_info( &self, id: &CharacteristicId ) -> Result<CharacteristicInfo, BluetoothError>

Get information about the given GATT characteristic.

source

pub async fn get_descriptor_info( &self, id: &DescriptorId ) -> Result<DescriptorInfo, BluetoothError>

Get information about the given GATT descriptor.

source

pub async fn connect(&self, id: &DeviceId) -> Result<(), BluetoothError>

Connect to the given Bluetooth device.

source

pub async fn connect_with_timeout( &self, id: &DeviceId, timeout: Duration ) -> Result<(), BluetoothError>

Connect to the given Bluetooth device with specified timeout.

source

pub async fn disconnect(&self, id: &DeviceId) -> Result<(), BluetoothError>

Disconnect from the given Bluetooth device.

source

pub async fn read_characteristic_value( &self, id: &CharacteristicId ) -> Result<Vec<u8>, BluetoothError>

Read the value of the given GATT characteristic.

This is equivalent to calling read_characteristic_value_with_offset(0).

source

pub async fn read_characteristic_value_with_offset( &self, id: &CharacteristicId, offset: usize ) -> Result<Vec<u8>, BluetoothError>

Read the value of the given GATT characteristic, starting from the given offset.

source

pub async fn write_characteristic_value( &self, id: &CharacteristicId, value: impl Into<Vec<u8>> ) -> Result<(), BluetoothError>

Write the given value to the given GATT characteristic, with default options.

This is equivalent to calling write_characteristic_value_with_options(WriteOptions::default()).

source

pub async fn write_characteristic_value_with_options( &self, id: &CharacteristicId, value: impl Into<Vec<u8>>, options: WriteOptions ) -> Result<(), BluetoothError>

Write the given value to the given GATT characteristic, with the given options.

source

pub async fn read_descriptor_value( &self, id: &DescriptorId ) -> Result<Vec<u8>, BluetoothError>

Read the value of the given GATT descriptor.

This is equivalent to calling read_descriptor_value_with_offset(0).

source

pub async fn read_descriptor_value_with_offset( &self, id: &DescriptorId, offset: usize ) -> Result<Vec<u8>, BluetoothError>

Read the value of the given GATT descriptor, starting from the given offset.

source

pub async fn write_descriptor_value( &self, id: &DescriptorId, value: impl Into<Vec<u8>> ) -> Result<(), BluetoothError>

Write the given value to the given GATT descriptor.

This is equivalent to calling write_descriptor_value_with_offset(0).

source

pub async fn write_descriptor_value_with_offset( &self, id: &DescriptorId, value: impl Into<Vec<u8>>, offset: usize ) -> Result<(), BluetoothError>

Write the given value to the given GATT descriptor, starting from the given offset.

source

pub async fn start_notify( &self, id: &CharacteristicId ) -> Result<(), BluetoothError>

Start notifications on the given GATT characteristic.

source

pub async fn stop_notify( &self, id: &CharacteristicId ) -> Result<(), BluetoothError>

Stop notifications on the given GATT characteristic.

source

pub async fn event_stream( &self ) -> Result<impl Stream<Item = BluetoothEvent>, BluetoothError>

Get a stream of events for all devices.

source

pub async fn adapter_event_stream( &self, adapter: &AdapterId ) -> Result<impl Stream<Item = BluetoothEvent>, BluetoothError>

Get a stream of events for a particular adapter. This includes events for all devices it discovers or connects to.

source

pub async fn device_event_stream( &self, device: &DeviceId ) -> Result<impl Stream<Item = BluetoothEvent>, BluetoothError>

Get a stream of events for a particular device. This includes events for all its characteristics.

Note that this will not include the device discovered event for that device, as it is considered an event for the adapter rather than the device itself.

source

pub async fn characteristic_event_stream( &self, characteristic: &CharacteristicId ) -> Result<impl Stream<Item = BluetoothEvent>, BluetoothError>

Get a stream of events for a particular characteristic of a device.

Trait Implementations§

source§

impl Clone for BluetoothSession

source§

fn clone(&self) -> BluetoothSession

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for BluetoothSession

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.