disconnect method
Implementation
Future<void> disconnect() async {
await _connectionStateSubscription?.cancel();
_connectionStateSubscription = null;
if (_connectedDevice != null) {
try {
// Stop any active notifications before disconnecting
final services = await _connectedDevice!.discoverServices();
for (final service in services) {
for (final characteristic in service.characteristics) {
if (characteristic.isNotifying) {
try {
await characteristic.setNotifyValue(false);
} catch (e) {
// Ignore errors when stopping notifications
}
}
}
}
await _connectedDevice!.disconnect();
} catch (e) {
// Ignore disconnect errors
}
_connectedDevice = null;
}
_updateConnectionState(DeviceConnectionState.disconnected);
}