From bdfdd8d6e3666d5355119eb9036c098321f1af8a Mon Sep 17 00:00:00 2001 From: Ry2X <45420571+ry2x@users.noreply.github.com> Date: Thu, 3 Sep 2026 22:02:11 +0900 Subject: [PATCH] fix: handle powered state and Wi-Fi connection failures --- src/stores/connectivity/bluetooth.ts | 20 ++++++++---- src/stores/connectivity/wifiPage.ts | 47 +++++++++++++++++++++------- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/stores/connectivity/bluetooth.ts b/src/stores/connectivity/bluetooth.ts index 593c716..75e083b 100644 --- a/src/stores/connectivity/bluetooth.ts +++ b/src/stores/connectivity/bluetooth.ts @@ -141,7 +141,7 @@ export function createBluetoothPageState(active: Accessor): BluetoothPa }; const discover = () => { - if (!adapter) return; + if (!adapter || !bluetooth.is_powered) return; updateError(''); stopDiscovery(); @@ -156,6 +156,16 @@ export function createBluetoothPageState(active: Accessor): BluetoothPa } }; + const syncDiscovery = () => { + if (active.peek() && bluetooth.is_powered) { + discover(); + return; + } + + updateError(''); + stopDiscovery(); + }; + const connectDevice = async (device: Bluetooth.Device) => { updateError(''); try { @@ -199,13 +209,11 @@ export function createBluetoothPageState(active: Accessor): BluetoothPa bluetooth.connect('notify::devices', refreshDevices), bluetooth.connect('device-added', refreshDevices), bluetooth.connect('device-removed', refreshDevices), + bluetooth.connect('notify::is-powered', syncDiscovery), ]; - const unsubscribeActive = active.subscribe(() => { - if (active.peek()) discover(); - else stopDiscovery(); - }); + const unsubscribeActive = active.subscribe(syncDiscovery); - if (active.peek()) discover(); + syncDiscovery(); refreshDevices(); onCleanup(() => { diff --git a/src/stores/connectivity/wifiPage.ts b/src/stores/connectivity/wifiPage.ts index d206fb4..a16e2c7 100644 --- a/src/stores/connectivity/wifiPage.ts +++ b/src/stores/connectivity/wifiPage.ts @@ -35,6 +35,16 @@ export interface WifiPageState { setError: (message: string) => void; } +function isAuthenticationFailure(reason: NM.DeviceStateReason) { + return [ + NM.DeviceStateReason.NO_SECRETS, + NM.DeviceStateReason.SUPPLICANT_DISCONNECT, + NM.DeviceStateReason.SUPPLICANT_CONFIG_FAILED, + NM.DeviceStateReason.SUPPLICANT_FAILED, + NM.DeviceStateReason.SUPPLICANT_TIMEOUT, + ].includes(reason); +} + export function createWifiPageState(monitorConnector: string): WifiPageState { const network = Network.get_default(); const wifi = network.wifi; @@ -181,19 +191,32 @@ export function createWifiPageState(monitorConnector: string): WifiPageState { ] : []; const deviceHook = wifi - ? wifi.device.connect('state-changed', (_device, state: NM.DeviceState) => { - const pending = pendingAccessPoint; - if (!pending) return; - if (state === NM.DeviceState.ACTIVATED) { - pendingAccessPoint = null; - } else if (state === NM.DeviceState.NEED_AUTH || state === NM.DeviceState.FAILED) { - pendingAccessPoint = null; - retryAccessPoint = pending; - setError( - `Could not connect to ${pending.ssid || 'this network'}. Enter the password and retry.` - ); + ? wifi.device.connect( + 'state-changed', + ( + _device, + state: NM.DeviceState, + _oldState: NM.DeviceState, + reason: NM.DeviceStateReason + ) => { + const pending = pendingAccessPoint; + if (!pending) return; + if (state === NM.DeviceState.ACTIVATED) { + pendingAccessPoint = null; + } else if (state === NM.DeviceState.FAILED) { + pendingAccessPoint = null; + if (pending.requires_password && isAuthenticationFailure(reason)) { + retryAccessPoint = pending; + setError( + `Could not connect to ${pending.ssid || 'this network'}. Enter the password and retry.` + ); + } else { + retryAccessPoint = null; + setError(`Could not connect to ${pending.ssid || 'this network'}.`); + } + } } - }) + ) : null; if (wifi) scan();