123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using BluetoothLENet;
- using InABox.Core;
- namespace InABox.Avalonia.Platform.Desktop;
- public class Desktop_Bluetooth : IBluetooth, IDisposable
- {
- public Logger? Logger { get; set; }
- private BLE _adapter;
-
- public Action<IBluetoothDevice>? DeviceFound { get; set; }
- public CoreObservableCollection<IBluetoothDevice> Devices { get; } = new();
- public event EventHandler? Changed;
-
- public Desktop_Bluetooth()
- {
- Devices.CollectionChanged += (_,_) => Changed?.Invoke(this, EventArgs.Empty);
- ResetAdapter();
- }
- private void ResetAdapter()
- {
- _adapter = new BLE();
- _adapter.Changed += (_,_) =>
- {
- 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)
- {
- Logger?.Send(LogType.Error, "", $"{e.Message}\n{e.StackTrace}");
- }
-
- };
- }
- public async Task<bool> IsAvailable()
- {
- return await Task.FromResult(true);
- }
-
- public async Task<bool> StartScanningAsync(Guid configServiceId)
- {
- ResetAdapter();
- if (await IsAvailable())
- return await _adapter.StartScanningAsync(configServiceId);
- return false;
- }
- public async Task<bool> StopScanningAsync()
- {
- if (await IsAvailable())
- return await _adapter.StopScanningAsync();
- return false;
- }
- public async Task<IConnectedBluetoothDevice?> Connect(IBluetoothDevice device)
- {
- if (await IsAvailable())
- {
- if (device is Desktop_BluetoothDevice d)
- {
- var result = await _adapter.Connect(d.Device);
- if (result == ConnectDeviceResult.Ok)
- return new Desktop_ConnectedBluetoothDevice(d.Device);
- }
- }
- return null;
-
- }
- public async Task<bool> Disconnect(IConnectedBluetoothDevice device)
- {
- if (await IsAvailable())
- {
- if (device is Desktop_BluetoothDevice { Device: not null } d)
- {
- _adapter.Disconnect(d.Device);
- await Task.Delay(1000);
- }
- }
- return true;
- }
- public void Dispose()
- {
- foreach (var device in Devices)
- device.Dispose();
- Devices.Clear();
- }
- }
|