I am implementing Bluetooth feature into my app. I am able to scan the on bluetooth devices and show them on screen. and when i off the device it will remove the device from list and not display on screen. but while doing so. it showing the scanningForDeviceText(), after 5 sec then disappered then again display devices then again this text. so my devices list not display stable. after every after 5 sec it scan so it beacomes zero so it goes to my else condition how can i tackle this.
here is my code:
super.initState();
progressDialog = ProgressDialog(context: context);
flutterBlue = FlutterBluePlus();
_adapterStateStateSubscription =
FlutterBluePlus.adapterState.listen((state) {
_adapterState = state;
if (mounted) {
setState(() {});
}
_handleBluetoothStateChange();
});
_setupBluetoothSubscriptions();
startContinuousScan();
@override
void dispose() {
_adapterStateStateSubscription.cancel();
_scanResultsSubscription.cancel();
_isScanningSubscription.cancel();
FlutterBluePlus.stopScan(); // Stop scanning when disposing
super.dispose();
}
void _handleBluetoothStateChange() {
if (_adapterState != BluetoothAdapterState.on) {
setState(() {
scanResults.clear(); // Clear scan results when Bluetooth is off
});
}
_checkBluetoothPermission();
}
Future startContinuousScan() async {
while (true) {
try {
int divisor = Platform.isAndroid ? 8 : 1;
await FlutterBluePlus.startScan(
timeout: const Duration(seconds: 5),
continuousUpdates: true,
continuousDivisor: divisor);
await Future.delayed(const Duration(seconds: 5));
await FlutterBluePlus.stopScan();
} catch (e) {
// customToastMessage(toastMessage: e.toString());
}
}
}
void _setupBluetoothSubscriptions() {
_scanResultsSubscription = FlutterBluePlus.scanResults.listen((results) {
scanResults = results;
if (mounted) {
setState(() {});
}
}, onError: (e) {});
_isScanningSubscription = FlutterBluePlus.isScanning.listen((state) {
isScanning = state;
if (mounted) {
setState(() {});
}
});
}
scanResults.isNotEmpty
? SizedBox(
height: 300,
child: ListView.builder(
itemCount: scanResults.length,
itemBuilder: (context, index) {
final result = scanResults[index];
if (result.device.name.startsWith("NT") ||
result.device.name.startsWith("TW")) {
return ListTile(
onTap: () async {
await _checkLocationPermission();
if (doctorLinkedDeviceIdList.contains(result.device.name) ||
doctorLinkedDeviceIdList.contains("*")) {
Navigator.pushNamed(context, '/NadiDisplayScreen', arguments: {
'device': result.device,
'patientDetails': patientDetails,
'medicalId': medicalId,
'appointmentForPatient': appointmentForPatient,
});
} else {
showDialog(
barrierDismissible: false,
context: context,
builder: (_) => CustomAlertDialog(
content: SizedBox(
width: 310.0,
height: 220.0,
child: Column(
children: [
Center(
child: Image.asset(
'assets/logos/linke_device.gif',
width: 84.0,
height: 84.0,
),
),
const SizedBox(height: 8.0),
Text(
"Select linked device.",
style: (screenWidth > 600) ? TabPopupMainTextStyle() : PopupMainTextStyle(),
textAlign: TextAlign.center,
),
const SizedBox(height: 8.0),
Text(
"${result.device.name} is not linked with you.",
style: (screenWidth > 600) ? TabPopupSubTextStyle() : PopupSubTextStyle(),
textAlign: TextAlign.center,
),
Text(
"Select linked device & take nadi.",
style: (screenWidth > 600) ? TabPopupSubTextStyle() : PopupSubTextStyle(),
textAlign: TextAlign.center,
),
],
),
),
),
);
Future.delayed(const Duration(seconds: 3), () {
Navigator.pop(context);
});
}
},
leading: Image.asset(
'assets/bottom_navbar_icon/nadi.png',
width: 40.0,
height: 40.0,
color: ColorConstants.textgreen,
),
title: Text(
result.device.name,
style: (screenWidth > 600)
? TabDeviceNameForNadiPariksha()
: DeviceNameForNadiPariksha(),
),
subtitle: Flex(
direction: Axis.horizontal,
children: [
Expanded(
flex: 1,
child: Text(
result.device.id.toString(),
),
),
],
),
);
} else {
return Container();
}
},
),
)
: scanningForDeviceText(),