Browse Source

Added ManufacturerData to IBluetoothDevice

frankvandenbos 1 month ago
parent
commit
b41a707a00

+ 1 - 0
InABox.Avalonia.Platform.Android/Bluetooth/Android_BluetoothDevice.cs

@@ -9,6 +9,7 @@ public class Android_BluetoothDevice(ScanResult scan, Guid[] availableservices,
     public string Name { get; } = scan.ScanRecord?.DeviceName ?? "Unknown Device";
     public Guid[] AvailableServices { get; } = availableservices;
     public DateTime LastSeen { get; set; } = timestamp;
+    public byte[]? ManufacturerData { get; set; } = scan.ScanRecord?.GetManufacturerSpecificData(0xFF);
 
     public void Dispose()
     {

+ 1 - 0
InABox.Avalonia.Platform.Android/Bluetooth/Android_ConnectedBluetoothDevice.cs

@@ -10,6 +10,7 @@ public class Android_ConnectedBluetoothDevice : BluetoothGattCallback,IConnected
     public string ID { get; }
     public string Name { get; }
     public DateTime LastSeen { get; set; } = DateTime.Now;
+    public byte[]? ManufacturerData { get; set; }
 
     private Guid[] _availableServices = [];
     public Guid[] AvailableServices => _availableServices;

+ 29 - 8
InABox.Avalonia.Platform.Desktop/Bluetooth/Desktop_Bluetooth.cs

@@ -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;

+ 2 - 0
InABox.Avalonia.Platform.Desktop/Bluetooth/Desktop_BluetoothDevice.cs

@@ -8,6 +8,7 @@ public class Desktop_BluetoothDevice : IBluetoothDevice
     {
         Device = device;
         LastSeen = device.LastSeen;
+        ManufacturerData = device.ManufacturerData;
     }
 
     public BLEDevice? Device { get; private set; }
@@ -15,6 +16,7 @@ public class Desktop_BluetoothDevice : IBluetoothDevice
     public string Name => Device?.Native?.Name ?? "Unknown Device";
     public Guid[] AvailableServices  => Device?.AvailableServices ?? [];
     public DateTime LastSeen { get; set; }
+    public byte[]? ManufacturerData { get; set; }
 
     public void Dispose()
     {

+ 2 - 2
InABox.Avalonia.Platform/Bluetooth/IBluetooth.cs

@@ -13,6 +13,8 @@ public interface IBluetoothDevice : IDisposable
      Guid[] AvailableServices { get; }
      
      DateTime LastSeen { get; set; }
+     
+     byte[]? ManufacturerData { get; set; }
 }
 
 public interface IConnectedBluetoothDevice : IBluetoothDevice
@@ -32,8 +34,6 @@ public interface IBluetooth : ILoggable
      Logger? Logger { get; set; }
 
      Task<bool> IsAvailable();
-
-     //Task<IBluetoothDevice?> FindDevice(Guid serviceId, TimeSpan timeout);
      
      Task<bool> StartScanningAsync(Guid configServiceId);
      

+ 2 - 2
InABox.Core/CoreObservableCollection.cs

@@ -710,8 +710,8 @@ namespace InABox.Core
 
             public DeferredEventsCollection(CoreObservableCollection<T> collection)
             {
-                Debug.Assert(collection != null);
-                Debug.Assert(collection._deferredEvents == null);
+                //Debug.Assert(collection != null);
+                //Debug.Assert(collection._deferredEvents == null);
                 _collection = collection;
                 _collection._deferredEvents = this;
             }

+ 0 - 2
InABox.Core/Objects/Editors/Utils/EditorTypeUtils.cs

@@ -28,8 +28,6 @@ namespace InABox.Core
                 editor = new DoubleEditor();
             else if (type.IsOrdinal())
                 editor = new IntegerEditor();
-            else if (type == typeof(Address))
-                editor = new AddressEditor(false);
             else if (typeof(IPackable).IsAssignableFrom(type))
                 editor = new NullEditor();
             else if(type.IsEnum)