AntDevices.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace Sensors.ANT
  6. {
  7. public abstract class AwaitDevice
  8. {
  9. public abstract int DeviceId { get; }
  10. public abstract bool Connected { get; }
  11. public abstract void Connect(AntDevice device);
  12. }
  13. public class AntDevices
  14. {
  15. #region singleton
  16. private static readonly Lazy<AntDevices>
  17. Lazy =
  18. new Lazy<AntDevices>
  19. (() => new AntDevices());
  20. public static AntDevices Instance => Lazy.Value;
  21. #endregion
  22. public List<AntDevice> ScanResult { get; private set; }
  23. private List<AwaitDevice> awaitDevices;
  24. private AntChannel backgroundScanChannel;
  25. public void StartScan(List<AwaitDevice> devices)
  26. {
  27. awaitDevices = devices;
  28. if (backgroundScanChannel != null) backgroundScanChannel.Close();
  29. Debug.Log("Looking for ANT + Sensors");
  30. AntManager.Instance.Init();
  31. ScanResult = new List<AntDevice>();
  32. backgroundScanChannel = AntManager.Instance.OpenBackgroundScanChannel(0);
  33. backgroundScanChannel.onReceiveData += ReceivedBackgroundScanData;
  34. }
  35. private void ReceivedBackgroundScanData(byte[] data)
  36. {
  37. var deviceType = data[12]; // extended info Device Type byte
  38. var deviceNumber = data[10] | (data[11] << 8);
  39. var transType = data[13];
  40. int period = -1;
  41. int radioFreq = -1;
  42. String deviceTypeName ="";
  43. switch (deviceType)
  44. {
  45. case AntplusDeviceType.BikePower:
  46. period = 8182;
  47. radioFreq = 57;
  48. deviceTypeName = "Power";
  49. break;
  50. case AntplusDeviceType.BikeSpeed:
  51. period = 8118;
  52. radioFreq = 57;
  53. deviceTypeName = "Speed";
  54. break;
  55. case AntplusDeviceType.HeartRate:
  56. period = 8070;
  57. radioFreq = 57;
  58. deviceTypeName = "HR";
  59. break;
  60. }
  61. if (period < 0)
  62. {
  63. //Debug.Log($"Device type {deviceType} not supported");
  64. return;
  65. }
  66. if (ScanResult.Any(d => d.deviceNumber == deviceNumber && d.transType == transType))
  67. {
  68. return;
  69. }
  70. var foundDevice = new AntDevice
  71. {
  72. deviceType = deviceType,
  73. deviceNumber = deviceNumber,
  74. transType = transType,
  75. period = period,
  76. radiofreq = radioFreq
  77. };
  78. foundDevice.name = $"{deviceTypeName}(" + foundDevice.deviceNumber + ")";
  79. Debug.Log($"Found device: {foundDevice.name}");
  80. ScanResult.Add(foundDevice);
  81. awaitDevices.FirstOrDefault(d => d.DeviceId == foundDevice.deviceNumber)?.Connect(foundDevice);
  82. if (awaitDevices.TrueForAll(d => d.Connected))
  83. {
  84. Debug.Log("All desired devices found. Closing background scan channel;");
  85. backgroundScanChannel.Close();
  86. }
  87. }
  88. }
  89. }