ソースを参照

Support for Digital Keys in Mobile Devices

frankvandenbos 6 ヶ月 前
コミット
478a88ecba

+ 86 - 0
InABox.Avalonia.Platform.Android/Bluetooth.Android.cs

@@ -0,0 +1,86 @@
+using InABox.Avalonia.Platform;
+using InABox.Core;
+using Microsoft.Maui.ApplicationModel;
+using Plugin.BLE;
+using Plugin.BLE.Abstractions;
+using Plugin.BLE.Abstractions.Contracts;
+
+namespace InABox.Avalonia.Platform.Android;
+
+public class Android_Bluetooth : IBluetooth
+{
+    public Logger? Logger { get; set; }
+    
+    public async Task<bool> IsAvailable()
+    {
+        try
+        {
+            PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.Bluetooth>();
+            if (status != PermissionStatus.Granted)
+                status = await Permissions.RequestAsync<Permissions.Bluetooth>();
+            return status == PermissionStatus.Granted;
+        }
+        catch (Exception e)
+        {
+            Console.WriteLine(e);
+        }
+
+        return false;
+
+
+    }
+
+    public async Task<bool> WriteAsync(string macaddress, Guid serviceid, Guid characteristicid, byte[] data)
+    {
+        if (await IsAvailable() != true)
+            return false;
+        
+        IDevice? _device = null;
+        var adapter = CrossBluetoothLE.Current.Adapter;
+        adapter.DeviceDiscovered += (s, e) => _device = e.Device;
+        try
+        {
+            var options = new ScanFilterOptions();
+            options.DeviceAddresses = [macaddress];
+            options.ServiceUuids = [serviceid];
+            await adapter.StartScanningForDevicesAsync(options, x=> true, false);
+        }
+        catch (Exception e)
+        {
+            Console.WriteLine(e);
+            Logger?.Error(e.Message);
+        }
+        
+        if (_device != null)
+        {
+            try
+            {
+                //await adapter.StopScanningForDevicesAsync();
+                //ConnectParameters connectParameters = new ConnectParameters(true, true);
+                await adapter.ConnectToDeviceAsync(_device);
+                try
+                {
+                    var service = await _device.GetServiceAsync(serviceid);
+                    var characteristic = await service.GetCharacteristicAsync(characteristicid);
+                    var bytes = await characteristic.ReadAsync();
+                    await characteristic.WriteAsync(data);
+                }
+                catch (Exception e)
+                {
+                    Logger?.Error(e.Message);
+                    return false;
+                }
+                await adapter.DisconnectDeviceAsync(_device);
+                return true;
+            }
+            catch (Exception e)
+            {
+                Logger?.Error(e.Message);
+                return false;
+            }
+           
+            
+        }
+        return false;
+    }
+}

+ 1 - 0
InABox.Avalonia.Platform.Android/InABox.Avalonia.Platform.Android.csproj

@@ -25,6 +25,7 @@
       <PackageReference Include="bblanchon.PDFium.Android" Version="134.0.6968" />
       <PackageReference Include="Microsoft.Maui.Essentials" />
       <PackageReference Include="PDFtoImage" Version="5.0.0" />
+      <PackageReference Include="Plugin.BLE" Version="3.1.0" />
     </ItemGroup>
 
 </Project>

+ 52 - 0
InABox.Avalonia.Platform.Desktop/Bluetooth.Desktop.cs

@@ -0,0 +1,52 @@
+using System.Security.Principal;
+using InABox.Avalonia.Platform;
+using InABox.Core;
+using InTheHand.Bluetooth;
+
+namespace InABox.Avalonia.Platform.Desktop;
+
+public class Desktop_Bluetooth : IBluetooth
+
+{
+    public Logger? Logger { get; set; }
+
+
+    public async Task<bool> IsAvailable()
+    {
+        return await Task.Run(() => false);
+    }
+
+    public async Task<bool> WriteAsync(string macaddress, Guid serviceid, Guid characteristicid, byte[] data)
+    {
+        var available = await InTheHand.Bluetooth.Bluetooth.GetAvailabilityAsync();
+        if (available)
+        {
+            var filter = new BluetoothLEScanFilter();
+            filter.Services.Add(serviceid);
+            var options = new RequestDeviceOptions()
+            {
+                AcceptAllDevices = false
+            };
+            options.Filters.Add(filter);
+            var devices = await InTheHand.Bluetooth.Bluetooth.ScanForDevicesAsync(options);
+            if (devices.Any())
+            {
+                return true;    
+            }
+        }
+        return false;
+        // IDevice? _device = null;
+        // var adapter = CrossBluetoothLE.Current.Adapter;
+        // adapter.DeviceDiscovered += (s, e) => _device = e.Device;
+        // await adapter.StartScanningForDevicesAsync([serviceid], null);
+        // if (_device != null)
+        // {
+        //     await adapter.ConnectToDeviceAsync(_device);
+        //     var service = await _device.GetServiceAsync(serviceid);
+        //     var characteristic = await service.GetCharacteristicAsync(characteristicid);
+        //     characteristic.WriteAsync(data);
+        //     return true;
+        // }
+        // return false;
+    }
+}

+ 1 - 0
InABox.Avalonia.Platform.Desktop/InABox.Avalonia.Platform.Desktop.csproj

@@ -11,6 +11,7 @@
     </ItemGroup>
 
     <ItemGroup>
+      <PackageReference Include="InTheHand.BluetoothLE" Version="4.0.37" />
       <PackageReference Include="PDFtoImage" Version="5.0.0" />
       <PackageReference Include="System.ComponentModel.Composition" Version="9.0.1" />
       <PackageReference Include="System.ComponentModel.Composition.Registration" Version="9.0.1" />

+ 18 - 0
InABox.Avalonia.Platform/Bluetooth/DefaultBluetooth.cs

@@ -0,0 +1,18 @@
+using InABox.Core;
+
+namespace InABox.Avalonia.Platform;
+
+public class DefaultBluetooth : IBluetooth
+{
+    public Logger? Logger { get; set; }
+
+    public async Task<bool> IsAvailable()
+    {
+        return await Task.Run(() => false);
+    }
+
+    public async Task<bool> WriteAsync(string macaddress, Guid serviceid, Guid characteristicid, byte[] data)
+    {
+        return await Task.Run(() => false);
+    }
+}

+ 12 - 0
InABox.Avalonia.Platform/Bluetooth/IBluetooth.cs

@@ -0,0 +1,12 @@
+using InABox.Core;
+
+namespace InABox.Avalonia.Platform;
+
+public interface IBluetooth : ILoggable
+{
+     Logger? Logger { get; set; }
+
+     Task<bool> IsAvailable();
+     
+     Task<bool> WriteAsync(string macaddress, Guid serviceid, Guid characteristicid, byte[] data);
+}

+ 1 - 1
InABox.Avalonia.Platform/InABox.Avalonia.Platform.csproj

@@ -9,7 +9,7 @@
     </PropertyGroup>
 
     <ItemGroup>
-      <PackageReference Include="Autofac" />
+      <PackageReference Include="Autofac" Version="8.2.0" />
       <PackageReference Include="Microsoft.Maui.Essentials" />
     </ItemGroup>
 

+ 11 - 0
InABox.Avalonia.Platform/PlatformTools.cs

@@ -1,4 +1,5 @@
 using Autofac;
+using InABox.Avalonia.Platform;
 using InABox.Core;
 
 namespace InABox.Avalonia.Platform;
@@ -49,6 +50,16 @@ public static class PlatformTools
         }
     }
     
+    private static IBluetooth? _bluetooth;
+    public static IBluetooth Bluetooth
+    {
+        get
+        {
+            _bluetooth ??= Resolve<IBluetooth, DefaultBluetooth>();
+            return _bluetooth;
+        }
+    }
+    
     private static TInterface Resolve<TInterface, TDefault>() where TInterface : notnull, ILoggable where TDefault : TInterface, new() 
     {
         _container ??= _builder.Build();

+ 4 - 2
InABox.Mobile/InABox.Mobile.Shared/Bluetooth.cs

@@ -67,8 +67,10 @@ namespace InABox.Mobile
         public async Task UnlockDigitalKey(String macaddress, Guid serviceid, Guid characteristicid, String key)
         {
 
-            if (!_disabled)
-                throw new Exception("BT Scanning must be disabled!");
+            
+            
+            //if (!_disabled)
+            //    throw new Exception("BT Scanning must be disabled!");
 
             _devicemap.TryGetValue(macaddress, out IDevice device);
             if (device == null)