|
@@ -0,0 +1,106 @@
|
|
|
+using UnityEngine;
|
|
|
+using System.Collections.Generic;
|
|
|
+using ANT_Managed_Library;
|
|
|
+using System;
|
|
|
+
|
|
|
+public class HrReceiver
|
|
|
+{
|
|
|
+ private int deviceID = 0; //set this to connect to a specific device ID
|
|
|
+ private bool connected = false; //will be set to true once connected
|
|
|
+ private List<AntDevice> scanResult;
|
|
|
+
|
|
|
+ private float heartRate; // the computed HR count in BPM
|
|
|
+
|
|
|
+ private AntChannel backgroundScanChannel;
|
|
|
+ private AntChannel deviceChannel;
|
|
|
+
|
|
|
+ public int DeviceID => deviceID;
|
|
|
+
|
|
|
+ public bool Connected => connected;
|
|
|
+
|
|
|
+ public List<AntDevice> ScanResult => scanResult;
|
|
|
+
|
|
|
+ public float HeartRate => heartRate;
|
|
|
+
|
|
|
+
|
|
|
+ public HrReceiver()
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public HrReceiver(int deviceID) => this.deviceID = deviceID;
|
|
|
+
|
|
|
+ //Start a background Scan to find the device
|
|
|
+ public void StartScan()
|
|
|
+ {
|
|
|
+ Debug.Log("Looking for ANT + HeartRate sensor");
|
|
|
+ AntManager.Instance.Init();
|
|
|
+ scanResult = new List<AntDevice>();
|
|
|
+ backgroundScanChannel = AntManager.Instance.OpenBackgroundScanChannel(0);
|
|
|
+ backgroundScanChannel.onReceiveData += ReceivedBackgroundScanData;
|
|
|
+ }
|
|
|
+
|
|
|
+ void ReceivedBackgroundScanData(Byte[] data)
|
|
|
+ {
|
|
|
+ byte deviceType = (data[12]); // extended info Device Type byte
|
|
|
+
|
|
|
+ switch (deviceType)
|
|
|
+ {
|
|
|
+ case AntplusDeviceType.HeartRate:
|
|
|
+ {
|
|
|
+ int deviceNumber = (data[10]) | data[11] << 8;
|
|
|
+ byte transType = data[13];
|
|
|
+ foreach (AntDevice d in scanResult)
|
|
|
+ {
|
|
|
+ if (d.deviceNumber == deviceNumber && d.transType == transType) //device already found
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ AntDevice foundDevice = new AntDevice();
|
|
|
+ foundDevice.deviceType = deviceType;
|
|
|
+ foundDevice.deviceNumber = deviceNumber;
|
|
|
+ foundDevice.transType = transType;
|
|
|
+ foundDevice.period = 8070;
|
|
|
+ foundDevice.radiofreq = 57;
|
|
|
+ foundDevice.name = "heartrate(" + foundDevice.deviceNumber + ")";
|
|
|
+ scanResult.Add(foundDevice);
|
|
|
+ if (deviceNumber == deviceID)
|
|
|
+ {
|
|
|
+ Debug.Log($"Desired HR Sensor with id {deviceNumber} found!");
|
|
|
+ ConnectToDevice(foundDevice);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Debug.Log($"HR sensor ({deviceNumber}) found and added to ScanResult");
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void ConnectToDevice(AntDevice device)
|
|
|
+ {
|
|
|
+ AntManager.Instance.CloseBackgroundScanChannel();
|
|
|
+ byte channelID = AntManager.Instance.GetFreeChannelID();
|
|
|
+ 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);
|
|
|
+ connected = true;
|
|
|
+ deviceChannel.onReceiveData += Data;
|
|
|
+ deviceChannel.onChannelResponse += ChannelResponse;
|
|
|
+
|
|
|
+ deviceChannel.hideRXFAIL = true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //Deal with the received Data
|
|
|
+ public void Data(Byte[] data)
|
|
|
+ {
|
|
|
+ heartRate = (data[7]);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ void ChannelResponse(ANT_Response response)
|
|
|
+ {
|
|
|
+ }
|
|
|
+}
|