@@ -53,7 +53,7 @@ pub struct BluetoothState {
5353 adapter : Adapter ,
5454 handle_ble : Option < AdvertisementHandle > ,
5555 handle_aa : ProfileHandle ,
56- handle_hsp : JoinHandle < Result < ProfileHandle > > ,
56+ handle_hsp : Option < JoinHandle < Result < ProfileHandle > > > ,
5757 handle_agent : AgentHandle ,
5858 keepalive : bool ,
5959}
@@ -140,8 +140,19 @@ async fn power_up_and_wait_for_connection(
140140 require_authorization : Some ( false ) ,
141141 ..Default :: default ( )
142142 } ;
143- let mut handle_hsp = session. register_profile ( profile) . await ?;
144- info ! ( "{} 🎧 Headset Profile (HSP): registered" , NAME ) ;
143+ let handle_hsp = match session. register_profile ( profile) . await {
144+ Ok ( handle_hsp) => {
145+ info ! ( "{} 🎧 Headset Profile (HSP): registered" , NAME ) ;
146+ Some ( handle_hsp)
147+ }
148+ Err ( e) => {
149+ warn ! (
150+ "{} 🎧 Headset Profile (HSP) registering error: {}, ignoring" ,
151+ NAME , e
152+ ) ;
153+ None
154+ }
155+ } ;
145156
146157 info ! ( "{} ⏳ Waiting for phone to connect via bluetooth..." , NAME ) ;
147158
@@ -190,20 +201,26 @@ async fn power_up_and_wait_for_connection(
190201 } ;
191202
192203 // handling connection to headset profile in own task
193- let task_hsp: JoinHandle < Result < ProfileHandle > > = tokio:: spawn ( async move {
194- let req = handle_hsp
195- . next ( )
196- . await
197- . expect ( "received no connect request" ) ;
198- info ! (
199- "{} 🎧 Headset Profile (HSP): connect from: <b>{}</>" ,
200- NAME ,
201- req. device( )
202- ) ;
203- req. accept ( ) ?;
204-
205- Ok ( handle_hsp)
206- } ) ;
204+ let task_hsp = {
205+ if let Some ( mut handle_hsp) = handle_hsp {
206+ Some ( tokio:: spawn ( async move {
207+ let req = handle_hsp
208+ . next ( )
209+ . await
210+ . expect ( "received no connect request" ) ;
211+ info ! (
212+ "{} 🎧 Headset Profile (HSP): connect from: <b>{}</>" ,
213+ NAME ,
214+ req. device( )
215+ ) ;
216+ req. accept ( ) ?;
217+
218+ Ok ( handle_hsp)
219+ } ) )
220+ } else {
221+ None
222+ }
223+ } ;
207224
208225 let req = timeout ( Duration :: from_secs ( 10 ) , handle_aa. next ( ) )
209226 . await ?
@@ -320,19 +337,20 @@ pub async fn bluetooth_stop(state: BluetoothState) -> Result<()> {
320337 drop ( state. handle_aa ) ;
321338
322339 // HSP profile is/was running in own task
323- let retval = state. handle_hsp ;
324- match timeout ( Duration :: from_secs_f32 ( 2.5 ) , retval) . await {
325- Ok ( task_handle) => match task_handle? {
326- Ok ( handle_hsp) => {
327- info ! ( "{} 🎧 Removing HSP profile" , NAME ) ;
328- drop ( handle_hsp) ;
329- }
340+ if let Some ( handle) = state. handle_hsp {
341+ match timeout ( Duration :: from_secs_f32 ( 2.5 ) , handle) . await {
342+ Ok ( task_handle) => match task_handle? {
343+ Ok ( handle_hsp) => {
344+ info ! ( "{} 🎧 Removing HSP profile" , NAME ) ;
345+ drop ( handle_hsp) ;
346+ }
347+ Err ( e) => {
348+ warn ! ( "{} 🎧 HSP profile error: {}" , NAME , e) ;
349+ }
350+ } ,
330351 Err ( e) => {
331- warn ! ( "{} 🎧 HSP profile error : {}" , NAME , e) ;
352+ warn ! ( "{} 🎧 Error waiting for HSP profile task : {}" , NAME , e) ;
332353 }
333- } ,
334- Err ( e) => {
335- warn ! ( "{} 🎧 Error waiting for HSP profile task: {}" , NAME , e) ;
336354 }
337355 }
338356
0 commit comments