All your computers and your phone/iPhone, working as one desk. Screens, apps, files, clipboard, calls, texts, notifications, mouse and keyboard, shared between Mac, Windows, Linux , Android, Web and ios / ipados . Free and open source. No account, no cloud, and no shared network needed.
JavaScript androidapple-ecosystemconnectelectronfile-sharingintel-unison-alternativeiosios-windowskde-connect-alternativekvmlinuxmacbookmacosmouse-sharingphone-link-alternativeproductivityscreen-sharingsidecar-alternativesynergy-alternativewindows
Windows helper: Bluetooth advertising state as Windows reports it, and again after an abort
Inuka Asith4c7b776be23f
diff --git a/windows-helper/src/Ble.cs b/windows-helper/src/Ble.csindex eaa087ac..299674f5 100644--- a/windows-helper/src/Ble.cs+++ b/windows-helper/src/Ble.cs@@ -52,6 +52,7 @@ namespace MacShare// quiet, this PC's Bluetooth takes the service down with the advertising: advertising stays on.bool holding, holdBroken, holdAds; // holdAds: bleHold {advertise}, the session's iPhone not linked yetstring infoNow = "";+ int aborts; // advertising aborted by Windows in a row (each new service waits longer)GattServiceProviderAdvertisingParameters adParams;DateTime quietAt = DateTime.MinValue;@@ -62,7 +63,18 @@ namespace MacShareNet.Event("ble.state", null, "state", s);}+ // One Start at a time: two at once (the helper ready, then the app's apply; the info changing)+ // each made a service for the same UUID, and Windows aborted the advertising of the new one.+ readonly SemaphoreSlim starting = new SemaphoreSlim(1, 1);+public async Task Start(string info)+ {+ await starting.WaitAsync();+ try { await StartOne(info); }+ finally { starting.Release(); }+ }++ async Task StartOne(string info){GattServiceProvider held = provider;if (held != null && holding && info == infoNow)@@ -74,6 +86,19 @@ namespace MacSharereturn;}Stop();+ // The old service gone first: one for the same UUID still advertising (stopping is+ // asynchronous) got the new one aborted at once.+ if (held != null)+ {+ for (int i = 0; i < 30; i++)+ {+ GattServiceProviderAdvertisementStatus st;+ try { st = held.AdvertisementStatus; } catch (Exception) { break; }+ if (st != GattServiceProviderAdvertisementStatus.Started && st != GattServiceProviderAdvertisementStatus.StartedWithoutAllAdvertisementData) break;+ await Task.Delay(100);+ }+ await Task.Delay(300);+ }BluetoothAdapter a = await BluetoothAdapter.GetDefaultAsync();if (a == null) { State("unsupported"); throw new Exception("no Bluetooth"); }if (!a.IsPeripheralRoleSupported) { State("unsupported"); throw new Exception("this PC's Bluetooth can't advertise"); }@@ -88,32 +113,8 @@ namespace MacShareGattLocalCharacteristicParameters ip = new GattLocalCharacteristicParameters();ip.CharacteristicProperties = GattCharacteristicProperties.Read;ip.ReadProtectionLevel = GattProtectionLevel.Plain;- // Answered here, not as a static value: each read is a device that reached this service, in the- // log (an iPhone that never linked: did it get this far?).- byte[] infoBytes = Encoding.UTF8.GetBytes(info);- GattLocalCharacteristic ic = (await p.Service.CreateCharacteristicAsync(BleIds.Info, ip)).Characteristic;- ic.ReadRequested += (s, e) =>- {- Windows.Foundation.Deferral rd = e.GetDeferral();- string who = e.Session.DeviceId.Id;- Task.Run(async () =>- {- try- {- GattReadRequest req = await e.GetRequestAsync();- if (req != null)- {- int off = (int)Math.Min(req.Offset, (uint)infoBytes.Length);- byte[] part = new byte[infoBytes.Length - off];- Buffer.BlockCopy(infoBytes, off, part, 0, part.Length);- req.RespondWithValue(part.AsBuffer());- if (off == 0) Net.Log("info read by " + who);- }- }- catch (Exception x) { Net.Log("info read: " + x.GetBaseException().Message); }- finally { rd.Complete(); }- });- };+ ip.StaticValue = Encoding.UTF8.GetBytes(info).AsBuffer();+ await p.Service.CreateCharacteristicAsync(BleIds.Info, ip);GattLocalCharacteristicParameters cp = new GattLocalCharacteristicParameters();cp.CharacteristicProperties = GattCharacteristicProperties.Write | GattCharacteristicProperties.Notify;@@ -134,8 +135,27 @@ namespace MacShared.SubscribedClientsChanged += OnSubscribers;p.AdvertisementStatusChanged += (s, e) =>{- if (e.Status == GattServiceProviderAdvertisementStatus.Aborted) State("off");- else if (e.Status == GattServiceProviderAdvertisementStatus.Started) State("on");+ // A provider Start replaced (Start right after Start: the helper ready, then the app's+ // apply) reports its end too: that isn't ours any more, and took the state off while the+ // new one advertised.+ if (s != provider) return;+ Net.Log("advertising: " + e.Status + (e.Error != BluetoothError.Success ? " (" + e.Error + ")" : ""));+ if (e.Status == GattServiceProviderAdvertisementStatus.Aborted)+ {+ State("off");+ // Windows gave up (this PC's Realtek adapter now and then; the same provider can't+ // start again: 0x8000000E): a new service in 5 s, 10, 20… up to a minute, while+ // this is still the one and nothing replaced or stopped it.+ int wait = Math.Min(60000, 5000 << Math.Min(4, aborts++));+ string again = infoNow;+ Task.Delay(wait).ContinueWith(async _ =>+ {+ if (s != provider || holding) return;+ try { await Start(again); Net.Log("advertising: a new service after " + wait / 1000 + " s"); }+ catch (Exception x) { Net.Log("advertising again: " + x.GetBaseException().Message); }+ });+ }+ else if (e.Status == GattServiceProviderAdvertisementStatus.Started || e.Status == GattServiceProviderAdvertisementStatus.StartedWithoutAllAdvertisementData) { aborts = 0; State("on"); }};GattServiceProviderAdvertisingParameters ap = new GattServiceProviderAdvertisingParameters();ap.IsConnectable = true;@@ -146,7 +166,14 @@ namespace MacShareinfoNow = info;adParams = ap;p.StartAdvertising(ap);- State("on");+ // "on" only once Windows says it advertises (AdvertisementStatusChanged): an adapter that+ // aborts at once (this PC's Realtek) looked on while nothing could find it.+ try+ {+ GattServiceProviderAdvertisementStatus st = p.AdvertisementStatus;+ if (st == GattServiceProviderAdvertisementStatus.Started || st == GattServiceProviderAdvertisementStatus.StartedWithoutAllAdvertisementData) State("on");+ }+ catch (Exception) { }}void Advertise(bool on)