|
@@ -17,19 +17,38 @@ public class Desktop_Bluetooth : IBluetooth, IDisposable
|
|
|
public Desktop_Bluetooth()
|
|
|
{
|
|
|
Devices.CollectionChanged += (_,_) => Changed?.Invoke(this, EventArgs.Empty);
|
|
|
-
|
|
|
+
|
|
|
+ ResetAdapter();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ResetAdapter()
|
|
|
+ {
|
|
|
_adapter = new BLE();
|
|
|
_adapter.Changed += (_,_) =>
|
|
|
{
|
|
|
- var found = _adapter.Devices.Select(x => x.MacAddress).ToArray();
|
|
|
- var cache = Devices.OfType<Desktop_BluetoothDevice>().Select(x => x.ID).ToArray();
|
|
|
- if (found.Except(cache).Any() || cache.Except(found).Any())
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var found = _adapter.Devices.Select(x => (x.MacAddress,x.ManufacturerData)).ToArray();
|
|
|
+ var cache = Devices.OfType<Desktop_BluetoothDevice>().Select(x => (x.ID,x.ManufacturerData)).ToArray();
|
|
|
+ if (found.Except(cache).Any() || cache.Except(found).Any())
|
|
|
+ {
|
|
|
+ var devices = _adapter.Devices
|
|
|
+ .ToArray()
|
|
|
+ .Select(x => new Desktop_BluetoothDevice(x))
|
|
|
+ .ToArray();
|
|
|
+ Console.WriteLine($"BLE:Found {devices.Length} devices");
|
|
|
+ Devices.ReplaceRange(devices);
|
|
|
+ Console.WriteLine($"BLE:Triggering Changed()");
|
|
|
+ Changed?.Invoke(this, EventArgs.Empty);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ Changed?.Invoke(this, EventArgs.Empty);
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
{
|
|
|
- var devices = _adapter.Devices
|
|
|
- .ToArray()
|
|
|
- .Select(x => new Desktop_BluetoothDevice(x));
|
|
|
- Devices.ReplaceRange(devices);
|
|
|
+ Logger?.Send(LogType.Error, "", $"{e.Message}\n{e.StackTrace}");
|
|
|
}
|
|
|
+
|
|
|
};
|
|
|
}
|
|
|
|
|
@@ -40,6 +59,7 @@ public class Desktop_Bluetooth : IBluetooth, IDisposable
|
|
|
|
|
|
public async Task<bool> StartScanningAsync(Guid configServiceId)
|
|
|
{
|
|
|
+ ResetAdapter();
|
|
|
if (await IsAvailable())
|
|
|
return await _adapter.StartScanningAsync(configServiceId);
|
|
|
return false;
|
|
@@ -74,6 +94,7 @@ public class Desktop_Bluetooth : IBluetooth, IDisposable
|
|
|
if (device is Desktop_BluetoothDevice { Device: not null } d)
|
|
|
{
|
|
|
_adapter.Disconnect(d.Device);
|
|
|
+ await Task.Delay(1000);
|
|
|
}
|
|
|
}
|
|
|
return true;
|