1- use std:: borrow:: Cow ;
21use std:: io;
32use std:: io:: Cursor ;
43
54use bluer:: Address ;
65use byteorder:: { LittleEndian , ReadBytesExt } ;
76use futures:: StreamExt ;
87use thiserror:: Error ;
9- use uuid:: { uuid , Uuid } ;
8+ use uuid:: { Uuid , uuid } ;
109
1110#[ derive( Debug , Error ) ]
1211pub enum Error {
1312 #[ error( "sensor with address {0} not found" ) ]
1413 SensorNotFound ( bluer:: Address ) ,
15- #[ error( "sensor cannot be used: {0} " ) ]
16- SensorInvalid ( Cow < ' static , str > ) ,
14+ #[ error( "sensor is not paired " ) ]
15+ SensorNotPaired ,
1716 #[ error( "GATT attribute '{0}' not found" ) ]
1817 GattAttributeNotFound ( Uuid ) ,
1918 #[ error( "sensor is not connected" ) ]
@@ -45,9 +44,6 @@ pub struct Sensor {
4544}
4645
4746impl Sensor {
48- // Characteristic Presentation Format
49- const CPF_UUID : & ' static str = "00002904-0000-1000-8000-00805f9b34fb" ;
50-
5147 // Battery Service
5248 const BAS_UUID : Uuid = uuid ! ( "0000180f-0000-1000-8000-00805f9b34fb" ) ;
5349 const BAS_BATTERY_LEVEL_UUID : Uuid = uuid ! ( "00002a19-0000-1000-8000-00805f9b34fb" ) ;
@@ -70,7 +66,7 @@ impl Sensor {
7066
7167 async fn new ( device : bluer:: Device ) -> Result < Self , Error > {
7268 if !device. is_paired ( ) . await ? {
73- return Err ( Error :: SensorInvalid ( "not paired" . into ( ) ) ) ;
69+ return Err ( Error :: SensorNotPaired ) ;
7470 }
7571
7672 Ok ( Sensor { device, gatt : None } )
@@ -80,12 +76,18 @@ impl Sensor {
8076 adapter : & bluer:: Adapter ,
8177 address : bluer:: Address ,
8278 ) -> Result < Sensor , Error > {
83- Self :: new (
84- adapter
85- . device ( address)
86- . map_err ( |_| Error :: SensorNotFound ( address) ) ?,
87- )
88- . await
79+ // Creating the device does not actually check the DBus object exists
80+ let device = adapter. device ( address) ?;
81+ // Therefore, try to get the name to confirm device exists
82+ device. name ( ) . await . map_err ( |e| match e {
83+ bluer:: Error {
84+ kind : bluer:: ErrorKind :: NotFound | bluer:: ErrorKind :: DoesNotExist ,
85+ ..
86+ } => Error :: SensorNotFound ( address) ,
87+ _ => e. into ( ) ,
88+ } ) ?;
89+
90+ Self :: new ( device) . await
8991 }
9092
9193 async fn find_service (
@@ -189,6 +191,7 @@ impl Sensor {
189191 monitor_type : bluer:: monitor:: Type :: OrPatterns ,
190192 patterns : Some ( vec ! [ bluer:: monitor:: Pattern {
191193 start_position: 0 ,
194+ // Service Data
192195 data_type: 0x21 ,
193196 content: vec![
194197 0xa1 , 0xf8 , 0x20 , 0x0f , 0xa2 , 0xc0 , 0x4e , 0x72 , 0x8e , 0x88 , 0x61 , 0x96 ,
0 commit comments