2
0

HrReceiver.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using ANT_Managed_Library;
  4. using System;
  5. public struct HRSensorData
  6. {
  7. public float HeartRate;
  8. }
  9. public class HrReceiver
  10. {
  11. private AntChannel backgroundScanChannel;
  12. private AntChannel deviceChannel;
  13. private HRSensorData sensorData;
  14. public int DeviceID { get; } = 0;
  15. public bool Connected { get; private set; } = false;
  16. public List<AntDevice> ScanResult { get; private set; }
  17. public HRSensorData SensorData => sensorData;
  18. public HrReceiver()
  19. {
  20. }
  21. public HrReceiver(int deviceID) => this.DeviceID = deviceID;
  22. //Start a background Scan to find the device
  23. public void StartScan()
  24. {
  25. Debug.Log("Looking for ANT + HeartRate sensor");
  26. AntManager.Instance.Init();
  27. ScanResult = new List<AntDevice>();
  28. backgroundScanChannel = AntManager.Instance.OpenBackgroundScanChannel(0);
  29. backgroundScanChannel.onReceiveData += ReceivedBackgroundScanData;
  30. }
  31. void ReceivedBackgroundScanData(Byte[] data)
  32. {
  33. byte deviceType = (data[12]); // extended info Device Type byte
  34. switch (deviceType)
  35. {
  36. case AntplusDeviceType.HeartRate:
  37. {
  38. int deviceNumber = (data[10]) | data[11] << 8;
  39. byte transType = data[13];
  40. foreach (AntDevice d in ScanResult)
  41. {
  42. if (d.deviceNumber == deviceNumber && d.transType == transType) //device already found
  43. return;
  44. }
  45. AntDevice foundDevice = new AntDevice();
  46. foundDevice.deviceType = deviceType;
  47. foundDevice.deviceNumber = deviceNumber;
  48. foundDevice.transType = transType;
  49. foundDevice.period = 8070;
  50. foundDevice.radiofreq = 57;
  51. foundDevice.name = "heartrate(" + foundDevice.deviceNumber + ")";
  52. ScanResult.Add(foundDevice);
  53. if (deviceNumber == DeviceID)
  54. {
  55. Debug.Log($"Desired HR Sensor with id {deviceNumber} found!");
  56. ConnectToDevice(foundDevice);
  57. }
  58. else
  59. {
  60. Debug.Log($"HR sensor ({deviceNumber}) found and added to ScanResult");
  61. }
  62. break;
  63. }
  64. }
  65. }
  66. void ConnectToDevice(AntDevice device)
  67. {
  68. AntManager.Instance.CloseBackgroundScanChannel();
  69. byte channelID = AntManager.Instance.GetFreeChannelID();
  70. deviceChannel = AntManager.Instance.OpenChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00,
  71. channelID, (ushort) device.deviceNumber, device.deviceType, device.transType, (byte) device.radiofreq,
  72. (ushort) device.period, false);
  73. Connected = true;
  74. deviceChannel.onReceiveData += Data;
  75. deviceChannel.onChannelResponse += ChannelResponse;
  76. deviceChannel.hideRXFAIL = true;
  77. }
  78. //Deal with the received Data
  79. private void Data(Byte[] data)
  80. {
  81. sensorData.HeartRate = (data[7]);
  82. }
  83. void ChannelResponse(ANT_Response response)
  84. {
  85. }
  86. }