|
@@ -3,12 +3,12 @@ using Android.Bluetooth;
|
|
|
|
|
|
namespace InABox.Avalonia.Platform.Android;
|
|
|
|
|
|
-public class Android_ConnectedBluetoothDevice(BluetoothDevice device) : BluetoothGattCallback,IConnectedBluetoothDevice
|
|
|
+public class Android_ConnectedBluetoothDevice : BluetoothGattCallback,IConnectedBluetoothDevice
|
|
|
{
|
|
|
|
|
|
- private BluetoothDevice _device = device;
|
|
|
- public string ID { get; } = device?.Address ?? string.Empty;
|
|
|
- public string Name { get; } = device?.Name ?? "Unknown Device";
|
|
|
+ private BluetoothDevice? _device;
|
|
|
+ public string ID { get; }
|
|
|
+ public string Name { get; }
|
|
|
public DateTime LastSeen { get; set; } = DateTime.Now;
|
|
|
|
|
|
private Guid[] _availableServices = [];
|
|
@@ -16,15 +16,22 @@ public class Android_ConnectedBluetoothDevice(BluetoothDevice device) : Bluetoot
|
|
|
|
|
|
private BluetoothGatt? _bluetoothGatt;
|
|
|
|
|
|
- private TaskCompletionSource<bool> _connectionTaskCompletionSource;
|
|
|
- private TaskCompletionSource<bool> _serviceDiscoveryTaskCompletionSource;
|
|
|
- private TaskCompletionSource<byte[]> _readTaskCompletionSource;
|
|
|
- private TaskCompletionSource<bool> _writeTaskCompletionSource;
|
|
|
+ private TaskCompletionSource<bool>? _connectionTaskCompletionSource;
|
|
|
+ private TaskCompletionSource<bool>? _serviceDiscoveryTaskCompletionSource;
|
|
|
+ private TaskCompletionSource<byte[]>? _readTaskCompletionSource;
|
|
|
+ private TaskCompletionSource<bool>? _writeTaskCompletionSource;
|
|
|
+
|
|
|
+ public Android_ConnectedBluetoothDevice(BluetoothDevice? device)
|
|
|
+ {
|
|
|
+ _device = device;
|
|
|
+ ID = device?.Address ?? string.Empty;
|
|
|
+ Name = device?.Name ?? "Unknown Device";
|
|
|
+ }
|
|
|
|
|
|
public async Task<bool> ConnectAsync()
|
|
|
{
|
|
|
_connectionTaskCompletionSource = new TaskCompletionSource<bool>();
|
|
|
- _bluetoothGatt = _device.ConnectGatt(Application.Context, false, this);
|
|
|
+ _bluetoothGatt = _device?.ConnectGatt(Application.Context, false, this);
|
|
|
return await _connectionTaskCompletionSource.Task;
|
|
|
}
|
|
|
|
|
@@ -88,7 +95,7 @@ public class Android_ConnectedBluetoothDevice(BluetoothDevice device) : Bluetoot
|
|
|
public async Task<String?> ReadStringAsync(Guid serviceid, Guid characteristicid)
|
|
|
{
|
|
|
var data = await ReadBytesAsync(serviceid,characteristicid);
|
|
|
- return data != null ? System.Text.Encoding.UTF8.GetString(data) : null;
|
|
|
+ return data != null ? Encoding.UTF8.GetString(data) : null;
|
|
|
}
|
|
|
|
|
|
private async Task<byte[]?> ReadCharacteristicAsync(BluetoothGattCharacteristic characteristic)
|
|
@@ -174,8 +181,9 @@ public class Android_ConnectedBluetoothDevice(BluetoothDevice device) : Bluetoot
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void Dispose()
|
|
|
+ public new void Dispose()
|
|
|
{
|
|
|
+ base.Dispose();
|
|
|
try
|
|
|
{
|
|
|
_bluetoothGatt?.Disconnect();
|
|
@@ -185,12 +193,15 @@ public class Android_ConnectedBluetoothDevice(BluetoothDevice device) : Bluetoot
|
|
|
{
|
|
|
Console.WriteLine($"Error during disposal: {ex.Message}");
|
|
|
}
|
|
|
- finally
|
|
|
- {
|
|
|
- _bluetoothGatt = null;
|
|
|
- }
|
|
|
+
|
|
|
+ _bluetoothGatt?.Dispose();
|
|
|
+ _bluetoothGatt = null;
|
|
|
+
|
|
|
+ _device?.Dispose();
|
|
|
+ _device = null;
|
|
|
|
|
|
Console.WriteLine("Resources released.");
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|