解决连接过程中断开蓝牙产生的问题

This commit is contained in:
闫守旺
2021-07-02 11:34:28 +08:00
parent d4e80d7232
commit 92daec7450
2 changed files with 44 additions and 48 deletions

View File

@ -40,14 +40,17 @@ class _HomeViewState extends State<HomeView> with WidgetsBindingObserver {
void initState() {
super.initState();
WidgetsBinding.instance!.addObserver(this);
stateSubscription = central.stateChanged.listen((state) {
if (state) {
startDiscovery();
} else {
discoveries.value = {};
discovering.value = false;
}
});
stateSubscription = central.stateChanged.listen(
(state) {
final invisible = !ModalRoute.of(context)!.isCurrent;
if (invisible) return;
if (state) {
startDiscovery();
} else {
discovering.value = false;
}
},
);
discoverySubscription = central.discovered.listen(
(discovery) {
discoveries.value[discovery.address] = discovery;
@ -85,16 +88,15 @@ class _HomeViewState extends State<HomeView> with WidgetsBindingObserver {
}
void startDiscovery() async {
final state = await central.state;
if (!state) return;
if (discovering.value || !await central.state) return;
await central.startDiscovery();
discovering.value = true;
}
void stopDiscovery() async {
final state = await central.state;
if (!state) return;
if (!discovering.value || !await central.state) return;
await central.stopDiscovery();
discoveries.value = {};
discovering.value = false;
}