2
0

HeartRateDisplay.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using ANT_Managed_Library;
  5. using System;
  6. /*
  7. Frequencies such as 4.06 Hz (ANT+ Heart
  8. Rate) and 4.005 Hz (ANT+ Bike Power) will
  9. periodically “drift” into each other, or into
  10. other channel periods that may be present
  11. in the vicinity. During this overlap, channel
  12. collisions may occur as the radio can only
  13. service channel at a time.
  14. */
  15. public class HeartRateDisplay : MonoBehaviour {
  16. public bool autoStartScan = true; //start scan on play
  17. public bool connected = false; //will be set to true once connected
  18. //windows and mac settings
  19. public bool autoConnectToFirstSensorFound = true; //for windows and mac, either connect to the first sensor found or let you pick a sensor manually in the scanResult list with your own UI and call ConnectToDevice(AntDevice device)
  20. public List<AntDevice> scanResult;
  21. //android settings
  22. public bool useAndroidUI = true; //will open the unified ant+ UI on the android app if set to true, otherwise will connect to the first found device
  23. public bool skipPreferredSearch = true; //- True = Don't automatically connect to user's preferred device, but always go to search for other devices.
  24. //the sensor values we receive fron the onReceiveData event
  25. public float heartRate; // the computed HR count in BPM
  26. private AntChannel backgroundScanChannel;
  27. private AntChannel deviceChannel;
  28. public int deviceID = 0; //set this to connect to a specific device ID
  29. void Start() {
  30. if (autoStartScan)
  31. StartScan();
  32. }
  33. //Start a background Scan to find the device
  34. public void StartScan() {
  35. Debug.Log("Looking for ANT + HeartRate sensor");
  36. #if UNITY_ANDROID && !UNITY_EDITOR
  37. //java: connect_heartrate(String gameobjectName, boolean useAndroidUI, boolean skipPreferredSearch, int deviceID)
  38. AndroidJNI.AttachCurrentThread();
  39. using (AndroidJavaClass javaClass = new AndroidJavaClass("com.ant.plugin.Ant_Connector")) {
  40. using (AndroidJavaObject activity = javaClass.GetStatic<AndroidJavaObject>("mContext")) {
  41. activity.Call("connect_heartrate", this.gameObject.name,useAndroidUI,skipPreferredSearch,deviceID);
  42. }
  43. }
  44. #else
  45. AntManager.Instance.Init();
  46. scanResult = new List<AntDevice>();
  47. backgroundScanChannel = AntManager.Instance.OpenBackgroundScanChannel(0);
  48. backgroundScanChannel.onReceiveData += ReceivedBackgroundScanData;
  49. #endif
  50. }
  51. //Android functions
  52. void ANTPLUG_ConnectEvent(string resultCode) {
  53. switch (resultCode) {
  54. case "SUCCESS":
  55. connected = true;
  56. Debug.Log("HR MONITOR IS CONMNNECTED");
  57. break;
  58. case "CHANNEL_NOT_AVAILABLE":
  59. //Channel Not Available
  60. break;
  61. case "ADAPTER_NOT_DETECTED":
  62. //ANT Adapter Not Available. Built-in ANT hardware or external adapter required.
  63. Debug.Log("ANT Adapter Not Available. Built-in ANT hardware or external adapter required.");
  64. break;
  65. case "BAD_PARAMS":
  66. //Bad request parameters.
  67. break;
  68. case "OTHER_FAILUR":
  69. //RequestAccess failed. See logcat for details.
  70. break;
  71. case "DEPENDENCY_NOT_INSTALLED":
  72. //You need to install the ANT+ Plugins service or you may need to update your existing version if you already have it.
  73. case "USER_CANCELLED":
  74. //USER_CANCELLED
  75. break;
  76. case "UNRECOGNIZED":
  77. //UNRECOGNIZED. PluginLib Upgrade Required?",
  78. break;
  79. default:
  80. //UNRECOGNIZED
  81. break;
  82. }
  83. }
  84. void ANTPLUG_StateChange(string newDeviceState) {
  85. switch (newDeviceState) {
  86. case "DEAD":
  87. connected = false;
  88. Debug.Log("HR MONITOR IS DEAD");
  89. break;
  90. case "CLOSED":
  91. break;
  92. case "SEARCHING":
  93. //searching
  94. break;
  95. case "TRACKING":
  96. //tracking
  97. break;
  98. case "PROCESSING_REQUEST":
  99. break;
  100. default:
  101. //UNRECOGNIZED
  102. break;
  103. }
  104. }
  105. void ANTPLUG_Receive_computedHeartRate(string s) {
  106. heartRate = (int)float.Parse(s);
  107. }
  108. //Windows and mac
  109. //If the device is found
  110. void ReceivedBackgroundScanData(Byte[] data) {
  111. byte deviceType = (data[12]); // extended info Device Type byte
  112. switch (deviceType) {
  113. case AntplusDeviceType.HeartRate: {
  114. int deviceNumber = (data[10]) | data[11] << 8;
  115. byte transType = data[13];
  116. foreach (AntDevice d in scanResult) {
  117. if (d.deviceNumber == deviceNumber && d.transType == transType) //device already found
  118. return;
  119. }
  120. Debug.Log("Heart rate sensor found " + deviceNumber);
  121. AntDevice foundDevice = new AntDevice();
  122. foundDevice.deviceType = deviceType;
  123. foundDevice.deviceNumber = deviceNumber;
  124. foundDevice.transType = transType;
  125. foundDevice.period = 8070;
  126. foundDevice.radiofreq = 57;
  127. foundDevice.name = "heartrate(" + foundDevice.deviceNumber + ")";
  128. scanResult.Add(foundDevice);
  129. if (autoConnectToFirstSensorFound) {
  130. ConnectToDevice(foundDevice);
  131. }
  132. break;
  133. }
  134. default: {
  135. break;
  136. }
  137. }
  138. }
  139. void ConnectToDevice(AntDevice device) {
  140. AntManager.Instance.CloseBackgroundScanChannel();
  141. byte channelID = AntManager.Instance.GetFreeChannelID();
  142. deviceChannel = AntManager.Instance.OpenChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00, channelID, (ushort)device.deviceNumber, device.deviceType, device.transType, (byte)device.radiofreq, (ushort)device.period, false);
  143. connected = true;
  144. deviceChannel.onReceiveData += Data;
  145. deviceChannel.onChannelResponse += ChannelResponse;
  146. deviceChannel.hideRXFAIL = true;
  147. }
  148. //Deal with the received Data
  149. public void Data(Byte[] data) {
  150. //HR
  151. heartRate = (data[7]);
  152. }
  153. void ChannelResponse(ANT_Response response) {
  154. }
  155. }