@@ -4,7 +4,7 @@ use std::time::Duration;
4
4
5
5
use anyhow:: { Context , Result } ;
6
6
use btleplug:: {
7
- api:: { Central as _, Manager as _, Peripheral as _, ScanFilter , WriteType } ,
7
+ api:: { bleuuid , Central as _, Manager as _, Peripheral as _, ScanFilter , WriteType } ,
8
8
platform:: { Manager , Peripheral } ,
9
9
} ;
10
10
use tokio:: time;
@@ -13,9 +13,9 @@ use uuid::Uuid;
13
13
use crate :: protocol:: PayloadBuffer ;
14
14
15
15
/// `0000fee0-0000-1000-8000-00805f9b34fb`
16
- const BADGE_SERVICE_UUID : Uuid = btleplug :: api :: bleuuid:: uuid_from_u16 ( 0xfee0 ) ;
16
+ const BADGE_SERVICE_UUID : Uuid = bleuuid:: uuid_from_u16 ( 0xfee0 ) ;
17
17
/// `0000fee1-0000-1000-8000-00805f9b34fb`
18
- const BADGE_CHAR_UUID : Uuid = btleplug :: api :: bleuuid:: uuid_from_u16 ( 0xfee1 ) ;
18
+ const BADGE_CHAR_UUID : Uuid = bleuuid:: uuid_from_u16 ( 0xfee1 ) ;
19
19
20
20
const BADGE_BLE_DEVICE_NAME : & str = "LSLED" ;
21
21
const BLE_CHAR_CHUNK_SIZE : usize = 16 ;
@@ -75,26 +75,19 @@ impl Device {
75
75
// and also the correct name.
76
76
// The service uuid is also by devices that are not LED badges, so
77
77
// the name check is also necessary.
78
- let props = peripheral. properties ( ) . await ;
79
- if props. is_err ( ) {
78
+ let props = peripheral. properties ( ) . await . ok ( ) ??;
79
+
80
+ let local_name = props. local_name . as_ref ( ) ?;
81
+ if local_name != BADGE_BLE_DEVICE_NAME {
80
82
return None ;
81
83
}
82
84
83
- if let Some ( props) = props. unwrap ( ) {
84
- let local_name = props. local_name . as_ref ( ) ?;
85
- if local_name != BADGE_BLE_DEVICE_NAME {
86
- return None ;
87
- }
88
-
89
- if props
90
- . services
91
- . iter ( )
92
- . any ( |uuid| * uuid == BADGE_SERVICE_UUID )
93
- {
94
- Some ( Self { peripheral } )
95
- } else {
96
- None
97
- }
85
+ if props
86
+ . services
87
+ . iter ( )
88
+ . any ( |uuid| * uuid == BADGE_SERVICE_UUID )
89
+ {
90
+ Some ( Self { peripheral } )
98
91
} else {
99
92
None
100
93
}
@@ -128,14 +121,15 @@ impl Device {
128
121
. context ( "bluetooth device connect" ) ?;
129
122
130
123
let result = self . write_connected ( payload) . await ;
124
+ let disconnect_result = self . peripheral . disconnect ( ) . await ;
125
+
131
126
if result. is_ok ( ) {
132
- self . peripheral
133
- . disconnect ( )
134
- . await
135
- . context ( "bluetooth device disconnect" ) ?;
127
+ // Write succesful, return disconnect result
128
+ Ok ( disconnect_result?)
129
+ } else {
130
+ // Write failed, return write result and ignore disconnect result
131
+ result
136
132
}
137
-
138
- result
139
133
}
140
134
141
135
async fn write_connected ( & self , payload : PayloadBuffer ) -> Result < ( ) > {
@@ -145,23 +139,19 @@ impl Device {
145
139
. await
146
140
. context ( "discovering services" ) ?;
147
141
let characteristics = self . peripheral . characteristics ( ) ;
148
- let badge_char = characteristics. iter ( ) . find ( |c| c. uuid == BADGE_CHAR_UUID ) ;
149
-
150
- if badge_char. is_none ( ) {
151
- return Err ( anyhow:: anyhow!( "Badge characteristic not found" ) ) ;
152
- }
153
- let badge_char = badge_char. unwrap ( ) ;
142
+ let badge_char = characteristics
143
+ . iter ( )
144
+ . find ( |c| c. uuid == BADGE_CHAR_UUID )
145
+ . context ( "badge characteristic not found" ) ?;
154
146
155
147
// Write payload
156
148
let bytes = payload. into_padded_bytes ( ) ;
157
149
let data = bytes. as_ref ( ) ;
158
150
159
151
anyhow:: ensure!(
160
152
data. len( ) % BLE_CHAR_CHUNK_SIZE == 0 ,
161
- format!(
162
- "Payload size must be a multiple of {} bytes" ,
163
- BLE_CHAR_CHUNK_SIZE
164
- )
153
+ "Payload size must be a multiple of {} bytes" ,
154
+ BLE_CHAR_CHUNK_SIZE
165
155
) ;
166
156
167
157
// the device will brick itself if the payload is too long (more then 8192 bytes)
0 commit comments