HrReceiver.cs 3.3 KB

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