|
@@ -57,17 +57,19 @@ namespace BluetoothLENet
|
|
|
Disconnected
|
|
|
}
|
|
|
|
|
|
- public class BLEDevice(BluetoothLEDevice native) : IDisposable
|
|
|
+ public class BLEDevice(BluetoothLEDevice native, Guid[] availableServices) : IDisposable
|
|
|
{
|
|
|
public BluetoothLEDevice? Native { get; private set; } = native;
|
|
|
|
|
|
public BLEDeviceStatus Status => Services.Any()
|
|
|
? BLEDeviceStatus.Connected
|
|
|
: BLEDeviceStatus.Disconnected;
|
|
|
+
|
|
|
+ public Guid[] AvailableServices { get; private set; } = availableServices;
|
|
|
|
|
|
public ObservableCollection<BLEService> Services { get; } = new();
|
|
|
|
|
|
- public DateTime TimeStamp { get; set; }
|
|
|
+ public DateTime LastSeen { get; set; }
|
|
|
|
|
|
public string MacAddress => ParseMacAddress(Native?.BluetoothAddress ?? 0);
|
|
|
|
|
@@ -104,15 +106,13 @@ namespace BluetoothLENet
|
|
|
|
|
|
private readonly BluetoothLEAdvertisementWatcher _scanner;
|
|
|
|
|
|
-
|
|
|
-
|
|
|
public BLE()
|
|
|
{
|
|
|
Task.Run(() =>
|
|
|
{
|
|
|
while (true)
|
|
|
{
|
|
|
- var stale = Devices.ToArray().Where(x => x.TimeStamp < DateTime.Now.Subtract(new TimeSpan(0, 0, 5)))
|
|
|
+ var stale = Devices.ToArray().Where(x => (x == null) || (x.LastSeen < DateTime.Now.Subtract(new TimeSpan(0, 0, 5))))
|
|
|
.ToArray();
|
|
|
if (stale.Any())
|
|
|
Devices.RemoveRange(stale);
|
|
@@ -147,12 +147,13 @@ namespace BluetoothLENet
|
|
|
|
|
|
public event EventHandler Changed;
|
|
|
|
|
|
- public async Task<bool> StartScanningAsync(Guid[] serviceUuids)
|
|
|
+ public async Task<bool> StartScanningAsync(Guid serviceUuid)
|
|
|
{
|
|
|
await StopScanningAsync();
|
|
|
+ Devices.Clear();
|
|
|
_scanner.AdvertisementFilter.Advertisement.ServiceUuids.Clear();
|
|
|
- foreach (var uuid in serviceUuids)
|
|
|
- _scanner.AdvertisementFilter.Advertisement.ServiceUuids.Add(uuid); ;
|
|
|
+// foreach (var uuid in serviceUuids)
|
|
|
+ _scanner.AdvertisementFilter.Advertisement.ServiceUuids.Add(serviceUuid); ;
|
|
|
_scanner.Start();
|
|
|
return true;
|
|
|
}
|
|
@@ -183,11 +184,18 @@ namespace BluetoothLENet
|
|
|
var bledevice = Devices.FirstOrDefault(x => Equals(x.Native?.BluetoothAddress, args.BluetoothAddress));
|
|
|
if (bledevice == null)
|
|
|
{
|
|
|
- bledevice = new BLEDevice(device) { TimeStamp = args.Timestamp.DateTime };
|
|
|
+ var configServiceIds = _scanner.AdvertisementFilter.Advertisement.ServiceUuids;
|
|
|
+ var services = args.Advertisement.ServiceUuids
|
|
|
+ .Where(x => !x.ToString().ToUpper().EndsWith("-0000-1000-8000-00805F9B34FB") && !configServiceIds.Contains(x))
|
|
|
+ .ToArray();
|
|
|
+ bledevice = new BLEDevice(device, services) { LastSeen = args.Timestamp.DateTime };
|
|
|
Devices.Add(bledevice);
|
|
|
}
|
|
|
else
|
|
|
- bledevice.TimeStamp = args.Timestamp.DateTime;
|
|
|
+ {
|
|
|
+ bledevice.LastSeen = args.Timestamp.DateTime;
|
|
|
+ Changed?.Invoke(this, EventArgs.Empty);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|