| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- using System.Collections.ObjectModel;
- using BluetoothLENet;
- using InABox.Core;
- namespace InABox.Avalonia.Platform.Desktop;
- public class Desktop_BluetoothDevice(BLEDevice device) : IBluetoothDevice
- {
- public BLEDevice Device { get; } = device;
- public string ID { get; } = device.MacAddress ?? string.Empty;
- public string Name { get; } = device.Native?.Name ?? "Unknown Device";
- }
- public class Desktop_ConnectedBluetoothDevice(BLEDevice device)
- : Desktop_BluetoothDevice(device), IConnectedBluetoothDevice
- {
- public async Task<bool> WriteAsync(Guid serviceid, Guid characteristicid, byte[] data)
- {
- var service = Device.Services.FirstOrDefault(x=>x.Native.Uuid == serviceid);
- if (service != null)
- {
- var characteristic = service.Characteristics.FirstOrDefault(x=>x.Native.Uuid == characteristicid);
- if (characteristic != null)
- return await characteristic.WriteAsync(data);
- }
- return false;
- }
- public async Task<byte[]?> ReadAsync(Guid serviceid, Guid characteristicid)
- {
- var service = Device.Services.FirstOrDefault(x=>x.Native.Uuid == serviceid);
- if (service != null)
- {
- var characteristic = service.Characteristics.FirstOrDefault(x=>x.Native.Uuid == characteristicid);
- if (characteristic != null)
- return await characteristic.ReadAsync();
- }
- return [];
- }
- }
- public class Desktop_Bluetooth : IBluetooth
- {
- 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);
-
- _adapter = new BLE();
- _adapter.Changed += (_,_) =>
- {
- var devices = _adapter.Devices.ToArray().Select(x=>new Desktop_BluetoothDevice(x));
- Devices.ReplaceRange(devices);
- };
-
-
- }
- public async Task<bool> IsAvailable()
- {
- return await Task.FromResult(true);
- }
-
- public async Task<bool> StartScanningAsync(Guid serviceId)
- {
- if (await IsAvailable())
- return await _adapter.StartScanningAsync([serviceId]);
- 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 d)
- _adapter.Disconnect(d.Device);
- }
- return true;
- }
- }
|