Desktop.Bluetooth.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections.ObjectModel;
  2. using BluetoothLENet;
  3. using InABox.Core;
  4. namespace InABox.Avalonia.Platform.Desktop;
  5. public class Desktop_BluetoothDevice(BLEDevice device) : IBluetoothDevice
  6. {
  7. public BLEDevice Device { get; } = device;
  8. public string ID { get; } = device.MacAddress ?? string.Empty;
  9. public string Name { get; } = device.Native?.Name ?? "Unknown Device";
  10. }
  11. public class Desktop_ConnectedBluetoothDevice(BLEDevice device)
  12. : Desktop_BluetoothDevice(device), IConnectedBluetoothDevice
  13. {
  14. public async Task<bool> WriteAsync(Guid serviceid, Guid characteristicid, byte[] data)
  15. {
  16. var service = Device.Services.FirstOrDefault(x=>x.Native.Uuid == serviceid);
  17. if (service != null)
  18. {
  19. var characteristic = service.Characteristics.FirstOrDefault(x=>x.Native.Uuid == characteristicid);
  20. if (characteristic != null)
  21. return await characteristic.WriteAsync(data);
  22. }
  23. return false;
  24. }
  25. public async Task<byte[]?> ReadAsync(Guid serviceid, Guid characteristicid)
  26. {
  27. var service = Device.Services.FirstOrDefault(x=>x.Native.Uuid == serviceid);
  28. if (service != null)
  29. {
  30. var characteristic = service.Characteristics.FirstOrDefault(x=>x.Native.Uuid == characteristicid);
  31. if (characteristic != null)
  32. return await characteristic.ReadAsync();
  33. }
  34. return [];
  35. }
  36. }
  37. public class Desktop_Bluetooth : IBluetooth
  38. {
  39. public Logger? Logger { get; set; }
  40. private BLE _adapter;
  41. public Action<IBluetoothDevice>? DeviceFound { get; set; }
  42. public CoreObservableCollection<IBluetoothDevice> Devices { get; } = new();
  43. public event EventHandler? Changed;
  44. public Desktop_Bluetooth()
  45. {
  46. Devices.CollectionChanged += (_,_) => Changed?.Invoke(this, EventArgs.Empty);
  47. _adapter = new BLE();
  48. _adapter.Changed += (_,_) =>
  49. {
  50. var devices = _adapter.Devices.ToArray().Select(x=>new Desktop_BluetoothDevice(x));
  51. Devices.ReplaceRange(devices);
  52. };
  53. }
  54. public async Task<bool> IsAvailable()
  55. {
  56. return await Task.FromResult(true);
  57. }
  58. public async Task<bool> StartScanningAsync(Guid serviceId)
  59. {
  60. if (await IsAvailable())
  61. return await _adapter.StartScanningAsync([serviceId]);
  62. return false;
  63. }
  64. public async Task<bool> StopScanningAsync()
  65. {
  66. if (await IsAvailable())
  67. return await _adapter.StopScanningAsync();
  68. return false;
  69. }
  70. public async Task<IConnectedBluetoothDevice?> Connect(IBluetoothDevice device)
  71. {
  72. if (await IsAvailable())
  73. {
  74. if (device is Desktop_BluetoothDevice d)
  75. {
  76. var result = await _adapter.Connect(d.Device);
  77. if (result == ConnectDeviceResult.Ok)
  78. return new Desktop_ConnectedBluetoothDevice(d.Device);
  79. }
  80. }
  81. return null;
  82. }
  83. public async Task<bool> Disconnect(IConnectedBluetoothDevice device)
  84. {
  85. if (await IsAvailable())
  86. {
  87. if (device is Desktop_BluetoothDevice d)
  88. _adapter.Disconnect(d.Device);
  89. }
  90. return true;
  91. }
  92. }