iOS_ConnectedBluetoothDevice.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Text;
  3. using System.Threading.Tasks;
  4. using Plugin.BLE.Abstractions;
  5. namespace InABox.Avalonia.Platform.iOS;
  6. public class iOS_ConnectedBluetoothDevice(IBluetoothDevice device)
  7. : IConnectedBluetoothDevice
  8. {
  9. public void Dispose()
  10. {
  11. Device.Dispose();
  12. }
  13. public iOS_BluetoothDevice Device { get; } = (iOS_BluetoothDevice)device;
  14. public string ID => Device.ID;
  15. public string Name => Device.Name;
  16. public Guid[] AvailableServices => Device.AvailableServices;
  17. public DateTime LastSeen
  18. {
  19. get => Device.LastSeen;
  20. set => device.LastSeen = value;
  21. }
  22. public byte[]? ManufacturerData
  23. {
  24. get => Device.ManufacturerData;
  25. set => Device.ManufacturerData = value;
  26. }
  27. public async Task<bool> WriteBytesAsync(Guid serviceid, Guid characteristicid, byte[] data)
  28. {
  29. System.Diagnostics.Debug.WriteLine($"{DateTime.Now:O} WriteBytesAsync::Getting Service {serviceid}...");
  30. using (var service = await Device.Native.GetServiceAsync(serviceid))
  31. {
  32. if (service == null)
  33. {
  34. System.Diagnostics.Debug.WriteLine($"{DateTime.Now:O} WriteBytesAsync::Failed to find service");
  35. return false;
  36. }
  37. System.Diagnostics.Debug.WriteLine($"{DateTime.Now:O} WriteBytesAsync::Getting Characteritsic {characteristicid}...");
  38. var characteristic = await service.GetCharacteristicAsync(characteristicid);
  39. if (characteristic == null)
  40. {
  41. System.Diagnostics.Debug.WriteLine($"{DateTime.Now:O} WriteBytesAsync::Failed to find characteristic");
  42. return false;
  43. }
  44. System.Diagnostics.Debug.WriteLine($"{DateTime.Now:O} WriteBytesAsync::Writing [{string.Join(" ",data.Select(x=>$"{x:X2}"))}]...");
  45. // for some reason the async call fails randomly
  46. //var result = await characteristic.WriteAsync(data);
  47. // So lets hack in a one-second timeout...
  48. var writeTask = characteristic.WriteAsync(data);
  49. var waitTask = Task.Delay(1000);
  50. await Task.WhenAny(writeTask, waitTask);
  51. if (writeTask.Status == TaskStatus.RanToCompletion)
  52. {
  53. System.Diagnostics.Debug.WriteLine($"{DateTime.Now:O} WriteBytesAsync::Wrote {writeTask.Result} bytes.");
  54. return writeTask.Result == data.Length;
  55. }
  56. System.Diagnostics.Debug.WriteLine($"{DateTime.Now:O} WriteBytesAsync::Timeout while writing!");
  57. return false;
  58. }
  59. }
  60. public async Task<bool> WriteStringAsync(Guid serviceid, Guid characteristicid, string data)
  61. {
  62. var encoded = Encoding.UTF8.GetBytes(data);
  63. return await WriteBytesAsync(serviceid, characteristicid, encoded);
  64. }
  65. public async Task<byte[]?> ReadBytesAsync(Guid serviceid, Guid characteristicid)
  66. {
  67. System.Diagnostics.Debug.WriteLine($"{DateTime.Now:O} ReadBytesAsync::Getting Service {serviceid}...");
  68. using (var service = await Device.Native.GetServiceAsync(serviceid))
  69. {
  70. if (service == null)
  71. {
  72. System.Diagnostics.Debug.WriteLine($"{DateTime.Now:O} ReadBytesAsync::Failed to find service");
  73. return [];
  74. }
  75. System.Diagnostics.Debug.WriteLine($"{DateTime.Now:O} ReadBytesAsync::Getting Characteritsic {characteristicid}...");
  76. var characteristic = await service.GetCharacteristicAsync(characteristicid);
  77. if (characteristic == null)
  78. {
  79. System.Diagnostics.Debug.WriteLine($"{DateTime.Now:O} ReadBytesAsync::Failed to find characteristic");
  80. return [];
  81. }
  82. System.Diagnostics.Debug.WriteLine($"{DateTime.Now:O} ReadBytesAsync::Reading data...");
  83. var result = await characteristic.ReadAsync();
  84. System.Diagnostics.Debug.WriteLine($"{DateTime.Now:O} ReadBytesAsync:: [{result.resultCode}] => [{string.Join(" ",result.data.Select(x=>$"{x:X2}"))}].");
  85. return result.data;
  86. }
  87. }
  88. public async Task<string?> ReadStringAsync(Guid serviceid, Guid characteristicid)
  89. {
  90. var data = await ReadBytesAsync(serviceid,characteristicid);
  91. return data != null ? Encoding.UTF8.GetString(data) : null;
  92. }
  93. }