diff --git a/src/corebluetooth/internal.rs b/src/corebluetooth/internal.rs index 3ab2906e..6e58a9c7 100644 --- a/src/corebluetooth/internal.rs +++ b/src/corebluetooth/internal.rs @@ -327,6 +327,32 @@ impl PeripheralInternal { if let Some(future) = self.disconnected_future_state.take() { future.lock().unwrap().set_reply(CoreBluetoothReply::Ok) } + + // Fulfill all pending futures + let error = CoreBluetoothReply::Err(String::from("Device disconnected")); + self.services.iter().for_each(|(_, service)| { + service + .characteristics + .iter() + .for_each(|(_, characteristic)| { + let CharacteristicInternal { + read_future_state, + write_future_state, + subscribe_future_state, + unsubscribe_future_state, + .. + } = characteristic; + + let futures = read_future_state + .into_iter() + .chain(write_future_state.into_iter()) + .chain(subscribe_future_state.into_iter()) + .chain(unsubscribe_future_state.into_iter()); + for state in futures { + state.lock().unwrap().set_reply(error.clone()); + } + }); + }); } } @@ -734,8 +760,9 @@ impl CoreBluetoothInternal { self.get_characteristic(peripheral_uuid, service_uuid, characteristic_uuid) { trace!("Got subscribed event!"); - let state = characteristic.subscribe_future_state.pop_back().unwrap(); - state.lock().unwrap().set_reply(CoreBluetoothReply::Ok); + if let Some(state) = characteristic.subscribe_future_state.pop_back() { + state.lock().unwrap().set_reply(CoreBluetoothReply::Ok); + } } } @@ -749,8 +776,9 @@ impl CoreBluetoothInternal { self.get_characteristic(peripheral_uuid, service_uuid, characteristic_uuid) { trace!("Got unsubscribed event!"); - let state = characteristic.unsubscribe_future_state.pop_back().unwrap(); - state.lock().unwrap().set_reply(CoreBluetoothReply::Ok); + if let Some(state) = characteristic.unsubscribe_future_state.pop_back() { + state.lock().unwrap().set_reply(CoreBluetoothReply::Ok); + } } } diff --git a/src/corebluetooth/peripheral.rs b/src/corebluetooth/peripheral.rs index a57c1730..5597429d 100644 --- a/src/corebluetooth/peripheral.rs +++ b/src/corebluetooth/peripheral.rs @@ -314,6 +314,7 @@ impl api::Peripheral for Peripheral { .await?; match fut.await { CoreBluetoothReply::Ok => {} + CoreBluetoothReply::Err(msg) => return Err(Error::RuntimeError(msg)), reply => panic!("Unexpected reply: {:?}", reply), } Ok(()) @@ -333,6 +334,7 @@ impl api::Peripheral for Peripheral { .await?; match fut.await { CoreBluetoothReply::ReadResult(chars) => Ok(chars), + CoreBluetoothReply::Err(msg) => return Err(Error::RuntimeError(msg)), _ => { panic!("Shouldn't get anything but read result!"); } @@ -353,6 +355,7 @@ impl api::Peripheral for Peripheral { .await?; match fut.await { CoreBluetoothReply::Ok => trace!("subscribed!"), + CoreBluetoothReply::Err(msg) => return Err(Error::RuntimeError(msg)), _ => panic!("Didn't subscribe!"), } Ok(()) @@ -372,6 +375,7 @@ impl api::Peripheral for Peripheral { .await?; match fut.await { CoreBluetoothReply::Ok => {} + CoreBluetoothReply::Err(msg) => return Err(Error::RuntimeError(msg)), _ => panic!("Didn't unsubscribe!"), } Ok(())