Skip to content

feat(mac): optimize BLE auto-connect, prioritize Wi-Fi/Hotspot, and fix scan timer spam#241

Open
Mudit200408 wants to merge 4 commits into
sameerasw:developfrom
Mudit200408:connect-fix
Open

feat(mac): optimize BLE auto-connect, prioritize Wi-Fi/Hotspot, and fix scan timer spam#241
Mudit200408 wants to merge 4 commits into
sameerasw:developfrom
Mudit200408:connect-fix

Conversation

@Mudit200408
Copy link
Copy Markdown
Contributor

  • BLE Central Manager Optimization:

    • Fix infinite connection and service discovery loop by tracking discovered service UUIDs in a Set instead of an integer counter.
    • Guard characteristics discovery and notification subscription to prevent duplicate calls and channel stalls.
    • Enforce single token write per connection attempt.
    • Prevent scan timer from restarting scans and spamming logs when connected or when Wi-Fi is active.
    • Delay BLE auto-connect by 3.0s if a local network is active to give Wi-Fi/Hotspot connection priority.
  • Hotspot Unicast Wake-up Fallback:

    • Implement default gateway (.1) fallback in QuickConnectManager when no exact network history matches the new Mac IP.
    • Trigger auto wake-up of last connected device immediately on network change/server restart.
    • This completely bypasses the hotspot UDP broadcast blockade and connects via hotspot unicast!
  • UDP Discovery & Reconnect Flow:

    • Keep UDP discovery active when only a BLE connection is active to allow seamless shift to Wi-Fi/Hotspot.
    • Guard network monitors to prevent tearing down active BLE connections or resetting the manual disconnect flag when Wi-Fi flaps.
    • enable UDP broadcasting and peer exchange during BLE fallback

…ix scan timer spam

- BLE Central Manager Optimization:
  - Fix infinite connection and service discovery loop by tracking discovered service UUIDs in a Set instead of an integer counter.
  - Guard characteristics discovery and notification subscription to prevent duplicate calls and channel stalls.
  - Enforce single token write per connection attempt.
  - Prevent scan timer from restarting scans and spamming logs when connected or when Wi-Fi is active.
  - Delay BLE auto-connect by 3.0s if a local network is active to give Wi-Fi/Hotspot connection priority.

- Hotspot Unicast Wake-up Fallback:
  - Implement default gateway (.1) fallback in QuickConnectManager when no exact network history matches the new Mac IP.
  - Trigger auto wake-up of last connected device immediately on network change/server restart.
  - This completely bypasses the hotspot UDP broadcast blockade and connects via hotspot unicast!

- UDP Discovery & Reconnect Flow:
  - Keep UDP discovery active when only a BLE connection is active to allow seamless shift to Wi-Fi/Hotspot.
  - Guard network monitors to prevent tearing down active BLE connections or resetting the manual disconnect flag when Wi-Fi flaps.
  - enable UDP broadcasting and peer exchange during BLE fallback
@sameerasw
Copy link
Copy Markdown
Owner

@Mudit200408 What is the reason for these implementations? Hotspot is the same as any WiFi connection so there is no difference.

Hotspot Unicast Wake-up Fallback:

  • Implement default gateway (.1) fallback in QuickConnectManager when no exact network history matches the new Mac IP.
  • Trigger auto wake-up of last connected device immediately on network change/server restart.
  • This completely bypasses the hotspot UDP broadcast blockade and connects via hotspot unicast!

@sameerasw
Copy link
Copy Markdown
Owner

Also check the reasoning for the intensive CPU use as well. It is quite consistent.

CleanShot-CleanShot-CleanShot-20260526-6  36 53@2x

@Mudit200408
Copy link
Copy Markdown
Contributor Author

@Mudit200408 What is the reason for these implementations? Hotspot is the same as any WiFi connection so there is no difference.

Hotspot Unicast Wake-up Fallback:

  • Implement default gateway (.1) fallback in QuickConnectManager when no exact network history matches the new Mac IP.
  • Trigger auto wake-up of last connected device immediately on network change/server restart.
  • This completely bypasses the hotspot UDP broadcast blockade and connects via hotspot unicast!

Here is a concise, professional reply you can paste directly into your Pull Request:


Although mobile hotspots look like standard Wi-Fi, they treat local peer-to-peer communication differently:

  1. AP Isolation Blocks Broadcasts: Mobile hotspots (iOS/Android) strictly filter out local multicast and UDP broadcast traffic to save battery. This completely blocks standard discovery protocols.
  2. Phone is the Gateway (.1): Because the phone hosts the hotspot, it acts as the router/DHCP server and always occupies the default gateway IP (the .1 address of the subnet, e.g., 172.20.10.1).
  3. Unicast is Allowed: Direct unicast traffic (TCP/HTTP) targeted directly at the gateway (.1) bypasses this isolation entirely.

These Implementation Allows us to bypass the broadcast blockade by falling back to a direct unicast connection attempt to the default gateway (.1) when no network history matches.
And also Immediate Auto Wake-Up so, it instantly probes the gateway IP (.1) on network changes or server restarts, re-establishing the connection seamlessly without waiting for a user action or a broken broadcast discovery.

@Mudit200408
Copy link
Copy Markdown
Contributor Author

Also check the reasoning for the intensive CPU use as well. It is quite consistent.

CleanShot-CleanShot-CleanShot-20260526-6  36 53@2x

I will Look into this 👍

@sameerasw sameerasw moved this from Backlog to In review in AirSync Features and Bugs May 26, 2026
@Mudit200408
Copy link
Copy Markdown
Contributor Author

I have fixed the auto-connect button, can u test it out if possible?

@Mudit200408
Copy link
Copy Markdown
Contributor Author

I was testing and i found out the airsync was working on steroids,even when the Macbook was not in use (lid down) it used to connect with android, these changes fixes that
Just Put the lid down and the airsync will stop as it should
This fixes the idle drain and improve the battery life

@sameerasw
Copy link
Copy Markdown
Owner

sameerasw commented May 28, 2026

I was testing and i found out the airsync was working on steroids,even when the Macbook was not in use (lid down) it used to connect with android, these changes fixes that Just Put the lid down and the airsync will stop as it should This fixes the idle drain and improve the battery life

Yes, I am working on a fix for this.. I think it might be the bluetooth causing that.

Edit: Fixed

@Mudit200408
Copy link
Copy Markdown
Contributor Author

I was testing and i found out the airsync was working on steroids,even when the Macbook was not in use (lid down) it used to connect with android, these changes fixes that Just Put the lid down and the airsync will stop as it should This fixes the idle drain and improve the battery life

Yes, I am working on a fix for this.. I think it might be the bluetooth causing that.

Edit: Fixed

Hey, any more bugs you found in the implementation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

2 participants