derdilla / blood-pressure-monitor-fl

A cross platform app to save blood pressure values with export function
MIT License
72 stars 15 forks source link

fbp lastKnownState assertion failure #486

Open derdilla opened 6 days ago

derdilla commented 6 days ago

App version

main

Bug Description

Assertion failure on android, the last known adapter state is not the same as the current adapter state in the flutter blue plus backend. The assertion in question:

assert(_adapterStateParser.parse(backend.adapterStateNow) == _adapterStateParser.lastKnownState);

https://github.com/derdilla/blood-pressure-monitor-fl/blob/main/app/lib/features/bluetooth/backend/flutter_blue_plus/fbp_manager.dart#L36-L37

@pimlie, sorry to bother you about this (and please tell me if you don't care), but what was the purpose of that assert. I also wonder whether parsing is expensive enough that we have to cache it, or why we do it. The assert at least looks like we could use backend.adapterStateNow directly.

Steps to Reproduce

  1. Launch app in an emulator
  2. disable device bluetooth
  3. open add measurement dialoge
  4. close add measurement dialoge
  5. open add measurement dialoge

Probably has somthing todo with multiple creates of FlutterBluePlusManager and some hidden caching.

Details (Screenshots, Debug Info, Configurations)

Attachments
pimlie commented 5 days ago

The issue is that adapter's only update/send their state on the connect stream on first init. Hence why FBP implemented their own internal cache for this state, as when you close the adapter fully and then re-initiate then the adapter might not send a state on the stream as that is depending on the underlying (native) library (and underlying libs could remain active even though FBP is disposed of).

As the BLE package is more low-level and doesn't provide such a cached prop I ran into issues when you opened the bluetooth measurements dialog for the second time, hence why I added this caching to the bluetooth abstraction layer too. And this value should behave the same as the FBP's, hence the assert cause when they are different then either me or FBP (probably me) missed some use-case causing one of the cached states to be wrong