Struct bluest::Service

source ·
pub struct Service(/* private fields */);
Expand description

A Bluetooth GATT service

Implementations§

source§

impl Service

source

pub fn uuid(&self) -> Uuid

The Uuid identifying the type of this GATT service

Panics

On Linux, this method will panic if there is a current Tokio runtime and it is single-threaded, if there is no current Tokio runtime and creating one fails, or if the underlying Service::uuid_async() method fails.

source

pub async fn uuid_async(&self) -> Result<Uuid>

The Uuid identifying the type of this GATT service

source

pub async fn is_primary(&self) -> Result<bool>

Whether this is a primary service of the device.

Platform specific

Returns NotSupported on Windows.

source

pub async fn discover_characteristics(&self) -> Result<Vec<Characteristic>>

Discover all characteristics associated with this service.

source

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

Discover the characteristic(s) with the given Uuid.

source

pub async fn characteristics(&self) -> Result<Vec<Characteristic>>

Get previously discovered characteristics.

If no characteristics have been discovered yet, this method will perform characteristic discovery.

Examples found in repository?
examples/connected.rs (line 32)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
async fn main() -> Result<(), Box<dyn Error>> {
    use tracing_subscriber::prelude::*;
    use tracing_subscriber::{fmt, EnvFilter};

    tracing_subscriber::registry()
        .with(fmt::layer())
        .with(
            EnvFilter::builder()
                .with_default_directive(LevelFilter::INFO.into())
                .from_env_lossy(),
        )
        .init();

    let adapter = Adapter::default().await.ok_or("Bluetooth adapter not found")?;
    adapter.wait_available().await?;

    info!("getting connected devices");
    let devices = adapter.connected_devices().await?;
    for device in devices {
        info!("found {:?}", device);
        adapter.connect_device(&device).await?;
        let services = device.services().await?;
        for service in services {
            info!("  {:?}", service);
            let characteristics = service.characteristics().await?;
            for characteristic in characteristics {
                info!("    {:?}", characteristic);
                let props = characteristic.properties().await?;
                info!("      props: {:?}", props);
                if props.read {
                    info!("      value: {:?}", characteristic.read().await);
                }
                if props.write_without_response {
                    info!("      max_write_len: {:?}", characteristic.max_write_len());
                }

                let descriptors = characteristic.descriptors().await?;
                for descriptor in descriptors {
                    info!("      {:?}: {:?}", descriptor, descriptor.read().await);
                }
            }
        }
    }
    info!("done");

    Ok(())
}
More examples
Hide additional examples
examples/blinky.rs (line 56)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
async fn main() -> Result<(), Box<dyn Error>> {
    use tracing_subscriber::prelude::*;
    use tracing_subscriber::{fmt, EnvFilter};

    tracing_subscriber::registry()
        .with(fmt::layer())
        .with(
            EnvFilter::builder()
                .with_default_directive(LevelFilter::INFO.into())
                .from_env_lossy(),
        )
        .init();

    let adapter = Adapter::default().await.ok_or("Bluetooth adapter not found")?;
    adapter.wait_available().await?;

    info!("looking for device");
    let device = adapter
        .discover_devices(&[NORDIC_LED_AND_BUTTON_SERVICE])
        .await?
        .next()
        .await
        .ok_or("Failed to discover device")??;
    info!(
        "found device: {} ({:?})",
        device.name().as_deref().unwrap_or("(unknown)"),
        device.id()
    );

    adapter.connect_device(&device).await?;
    info!("connected!");

    let service = match device
        .discover_services_with_uuid(NORDIC_LED_AND_BUTTON_SERVICE)
        .await?
        .get(0)
    {
        Some(service) => service.clone(),
        None => return Err("service not found".into()),
    };
    info!("found LED and button service");

    let characteristics = service.characteristics().await?;
    info!("discovered characteristics");

    let button_characteristic = characteristics
        .iter()
        .find(|x| x.uuid() == BLINKY_BUTTON_STATE_CHARACTERISTIC)
        .ok_or("button characteristic not found")?;

    let button_fut = async {
        info!("enabling button notifications");
        let mut updates = button_characteristic.notify().await?;
        info!("waiting for button changes");
        while let Some(val) = updates.next().await {
            info!("Button state changed: {:?}", val?);
        }
        Ok(())
    };

    let led_characteristic = characteristics
        .iter()
        .find(|x| x.uuid() == BLINKY_LED_STATE_CHARACTERISTIC)
        .ok_or("led characteristic not found")?;

    let blink_fut = async {
        info!("blinking LED");
        tokio::time::sleep(Duration::from_secs(1)).await;
        loop {
            led_characteristic.write(&[0x01]).await?;
            info!("LED on");
            tokio::time::sleep(Duration::from_secs(1)).await;
            led_characteristic.write(&[0x00]).await?;
            info!("LED off");
            tokio::time::sleep(Duration::from_secs(1)).await;
        }
    };

    type R = Result<(), Box<dyn Error>>;
    let button_fut = async move {
        let res: R = button_fut.await;
        error!("Button task exited: {:?}", res);
    };
    let blink_fut = async move {
        let res: R = blink_fut.await;
        error!("Blink task exited: {:?}", res);
    };

    future::zip(blink_fut, button_fut).await;

    Ok(())
}
source

pub async fn discover_included_services(&self) -> Result<Vec<Service>>

Discover the included services of this service.

source

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

Discover the included service(s) with the given Uuid.

source

pub async fn included_services(&self) -> Result<Vec<Service>>

Get previously discovered included services.

If no included services have been discovered yet, this method will perform included service discovery.

Trait Implementations§

source§

impl Clone for Service

source§

fn clone(&self) -> Service

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 Service

source§

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

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

impl Hash for Service

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for Service

source§

fn eq(&self, other: &Service) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Service

source§

impl StructuralEq for Service

source§

impl StructuralPartialEq for Service

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,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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.

source§

impl<T> Instrument for T

source§

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

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

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 Twhere 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> 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.
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.
source§

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

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

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
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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