Exemplo n.º 1
0
        public static int DecodeHeartMonitor(BLESubscriptionValueChangedEventArgs e)
        {
            // Get the new heart rate value
            int heartRate = e.Data[1];

            return(heartRate);
        }
        private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
        {
            //foreach (byte b in e.Data)
            //{
            //    Console.Write(b + " ");
            //}

            if (e.ServiceName == "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e")

            {
                if (e.Data[4] == 16)
                {
                    float Speed = (e.Data[9] * 256 + e.Data[8]) / 1000.00f;
                    OnSpeed?.Invoke(this, Speed);
                    //Console.WriteLine("\n\tSpeed: " + Speed + "m/s");

                    //Console.WriteLine("\telapsed time: " + e.Data[6]/4.0 + " seconds");
                    //Console.WriteLine("\telapsed distance: " + e.Data[7] + " meters\n");
                }
            }
            else if (e.ServiceName == "00002a37-0000-1000-8000-00805f9b34fb")
            {
                float HeartRate = e.Data[1];
                OnHeartRate?.Invoke(this, HeartRate);
                //Console.WriteLine($"\n\tHeartRate: {HeartRate}bpm");
            }

            //Console.WriteLine("Received from {0}: {1}, {2}", e.ServiceName,
            //    BitConverter.ToString(e.Data).Replace("-", " "),
            //    Encoding.UTF8.GetString(e.Data));
        }
Exemplo n.º 3
0
 private static void BleHeart_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
 {
     byte[] receivedDataSubset = e.Data;
     if (e.Data.Length == 6)
     {
         //Console.WriteLine($"Heartrate data received: {receivedDataSubset[0]}, {receivedDataSubset[1]}, {receivedDataSubset[2]}, {receivedDataSubset[3]}, {receivedDataSubset[4]}, {receivedDataSubset[5]}");
     }
 }
Exemplo n.º 4
0
        private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
        {
            byte[] data      = e.Data;
            byte   heartbeat = data[1];

            Console.WriteLine("Heartbeat: {0}", heartbeat);
            this.heartrateDataReceiver.ReceiveHeartrateData(heartbeat);
        }
Exemplo n.º 5
0
        private static void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
        {
            String        name = e.ServiceName;
            StringBuilder data = new StringBuilder();

            if (name == "00002a37-0000-1000-8000-00805f9b34fb")
            {
                name = "HeartRate";
                foreach (byte b in e.Data)
                {
                    data.Append(b);
                    data.Append(" ");
                }

                //Console.WriteLine("{0}: {1}", name, data.ToString());
            }
            else if (name == "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e")
            {
                name = "Avans Bike";

                byte[] bytes = e.Data;
                //starting byte should always be 0xA4
                if (bytes[0] == 0xA4)
                {
                    //length of the message
                    int length = bytes[1];
                    //type of message
                    int type = bytes[2];


                    //checking if the checksum is correct
                    int checksum           = bytes[length + 3];
                    int checksumCalculated = bytes[0];
                    for (int i = 1; i < bytes.Length - 1; i++)
                    {
                        checksumCalculated = bytes[i] ^ checksumCalculated;
                    }

                    //if it does not match the message is corrupted so we leave this message alone
                    if (checksum != checksumCalculated)
                    {
                        Console.WriteLine("Error wrong message construction");
                        return;
                    }


                    //Console.WriteLine($"length = {length} \t type = {type}");
                    for (int i = 0; i < length; i++)
                    {
                        data.Append(bytes[i + 3]);
                        data.Append(" ");
                    }

                    Console.WriteLine("{0}: {1}", name, data.ToString());
                }
            }
        }
Exemplo n.º 6
0
        private void HR_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
        {
            this.bLESimulator.SaveBytesHeartRate(e.Data);
            this.bLESimulator.WriteData(WriteOption.Heartrate);
            BLEDecoderHR.Decrypt(e.Data, this.dataHandler);
            string toSendHeartRateData = this.dataHandler.ReadLastData();

            this.iClient.Write(toSendHeartRateData);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Callback for the Ergometer. Contains data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void Ergo_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
        {
            this.bLESimulator.SaveBytesErgo(e.Data);
            this.bLESimulator.WriteData(WriteOption.Ergo);
            BLEDecoderErgo.Decrypt(e.Data, this.dataHandler);
            string toSendErgoData = this.dataHandler.ReadLastData();

            this.iClient.Write(toSendErgoData);
        }
Exemplo n.º 8
0
        private static void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
        {
            byte[] receivedDataSubset = e.Data.SubArray(4, e.Data.Length - 2 - 4);
            //      pageConversion.RegisterData(receivedDataSubset);

            //Console.WriteLine("Received from {0}: {1}, {2}", e.ServiceName,
            //BitConverter.ToString(SubArray<byte>(e.Data, 4, e.Data.Length - 2 - 4)).Replace("-", " "),
            //Encoding.UTF8.GetString(e.Data));
        }
Exemplo n.º 9
0
 private void BleHeartValueReceived(object sender, BLESubscriptionValueChangedEventArgs e)
 {
     if (e.Data.Length == 2) // if false: not a ushort, so data corrupted
     {
         this.BikeDataReceived?.Invoke(this, new BikeDataReceivedEventArgs(BikeDataType.HeartBeat, new DBikeHeartBeat()
         {
             HeartBeat = BitConverter.ToUInt16(e.Data.Reverse().ToArray(), 0)
         }));
     }
 }
Exemplo n.º 10
0
        private static void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
        {
            //Console.WriteLine("Received from {0}: {1}, {2}", e.ServiceName,
            //    BitConverter.ToString(e.Data).Replace("-", " "),
            //    Encoding.UTF8.GetString(e.Data));

            string[] bytes = BitConverter.ToString(e.Data).Split('-');
            string[] ANT   = new string[5];
            if (e.ServiceName == "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e")
            {
                //Console.WriteLine("SYNC     : " + bytes[0]);
                //ANT[0] = bytes[0];
                //Console.WriteLine("LENGTH   : " + bytes[1]);

                int length = Convert.ToInt32(bytes[1], 16);
                //ANT[1] = length.ToString();
                //Console.WriteLine("MSG ID   : " + bytes[2]);
                //ANT[2] = bytes[2];
                //string msg = string.Empty;
                //for (int i = 3; i < 3 + length; i++)
                //{
                //    msg += bytes[i];
                //}
                //ANT[3] = msg;

                byte[] message = new byte[length - 1];

                Array.Copy(e.Data, 4, message, 0, length - 1);

                DoCrazyShitWithMsg(message);

                //Console.WriteLine("MSG      : " + msg);
                //string checksum = bytes[3 + length];
                //ANT[4] = checksum;
                //Console.WriteLine("CHECKSUM : " + checksum);

                //byte calcChecksum = 0;

                //for (int i = 0; i < e.Data.Length - 1; i++)
                //{
                //    calcChecksum ^= e.Data[i];
                //}

                //Console.WriteLine("Calculated checksum : " + Convert.ToString(calcChecksum,16).ToUpper());


                //Console.WriteLine(BitConverter.ToString(e.Data));
            }
            else
            {
                //Console.WriteLine("BPM:     " + Convert.ToInt32(bytes[1], 16));
            }
            Console.WriteLine();
        }
Exemplo n.º 11
0
 private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
 {
     if (e.Data[0] == 0x00) // if first byte 0x00 we got the heart rate package
     {
         SetNewHeartRate(Decoder.DecodeHeartMonitor(e));
     }
     else if (e.Data[4] == 0x10) // if fifth byte 0x10 we got the bike package
     {
         SetNewSpeed(decoder.DecodeBike(e));
     }
 }
Exemplo n.º 12
0
        public int getHeartrate(BLESubscriptionValueChangedEventArgs e)
        {
            int    heartRate  = -1;
            String stringData = BitConverter.ToString(e.Data);

            if (stringData.Substring(0, 2).Equals("16"))
            {
                heartRate = Convert.ToInt32(stringData.Substring(3, 2), 16);
            }

            return(heartRate);
        }
Exemplo n.º 13
0
        public float DecodeBike(BLESubscriptionValueChangedEventArgs e)
        {
            // Loop through all the received bytes except for the last one
            sumByte = e.Data[0];
            for (var i = 1; i < e.Data.Length - 1; i++)
            {
                // XOR all the bytes
                sumByte ^= e.Data[i];
            }
            // Check if the received sum is the same as our calculated one
            if (sumByte != e.Data[^ 1])
            {
                return(0xFFFF);
            }

            // Set the new speed value
            var speed = (e.Data[8] + (e.Data[9] << 8)) / 100f;

            return(speed);
        }
Exemplo n.º 14
0
        private void BleBikeValueReceived(object sender, BLESubscriptionValueChangedEventArgs e)
        {
            if (e.Data.Length > 3)
            {
                byte syncByte   = e.Data[0];
                byte dataLength = e.Data[1];
                byte type       = e.Data[2];
                byte channel    = e.Data[3];
                byte checksum   = e.Data[e.Data.Length - 1];

                byte calculatedChecksum = this.GenerateChecksum(e.Data.Take(e.Data.Length - 1));

                if (syncByte == EspBikeTrainer.ANT_SYNC_BYTE &&
                    checksum == calculatedChecksum &&
                    dataLength == 9 &&                      // dataLength is always 9 for FE-C
                    e.Data.Length == 3 + dataLength + 1)    // syncByte + lengthByte + typeByte + dataLength (includes the channel byte) + checksumByte
                {
                    // skip sync-, length-, type- and channelbyte. remove 1 from datalength because we skipped channelByte
                    byte[] data = e.Data.Skip(4).Take(dataLength - 1).ToArray();

                    byte pageNumber = data[0];

                    if (pageNumber == EspBikeTrainer.GENERAL_FE_DATA_PAGE)
                    {
                        DBikeGeneralFEData result = new DBikeGeneralFEData()
                        {
                            EquipmentTypeField = data[1],
                            ElapsedTime        = data[2],
                            DistanceTraveled   = data[3],
                            Speed             = BitConverter.ToUInt16(Utility.ReverseIfBigEndian(data.Skip(4).Take(2)).ToArray(), 0),
                            HeartRate         = data[6],
                            CapabilitiesField = (byte)(data[7] & 0b1111),       // Capabilities field is the first nibble of the last byte
                            FEStateField      = (byte)((data[7] >> 4) & 0b1111) // FE State field is the last nibble of the last byte
                        };

                        this.BikeDataReceived?.Invoke(this, new BikeDataReceivedEventArgs(BikeDataType.GeneralFEData, result));
                    }
                    if (pageNumber == EspBikeTrainer.SPECIFIC_BIKE_DATA_PAGE)
                    {
                        byte[] powerBytes = new byte[] { data[5], (byte)(data[6] & 0b1111) }; // Length of power is 1.5 bytes
Exemplo n.º 15
0
 /// <summary>
 /// Callback for when the subscription value of the ble bike has changed
 /// </summary>
 /// <param name="sender"> the sender object</param>
 /// <param name="e">the value changed event</param>
 private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
 {
     if (e.ServiceName == "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e")
     {
         byte[] payload = new byte[8];
         Array.Copy(e.Data, 4, payload, 0, 8);
         foreach (IDataReceiver dataReceiver in this.dataReceivers)
         {
             dataReceiver.Bike(payload);
         }
     }
     else if (e.ServiceName == "00002a37-0000-1000-8000-00805f9b34fb")
     {
         foreach (IDataReceiver dataReceiver in this.dataReceivers)
         {
             dataReceiver.BPM(e.Data);
         }
     }
     else
     {
         Console.WriteLine("received data from unknown source {0}", e.ServiceName);
     }
 }
Exemplo n.º 16
0
 private static void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
 {
     SimpleLog.Log("FietsData.txt", $"{e.Data[0]}, {e.Data[1]}, {e.Data[2]}, {e.Data[3]}, {e.Data[4]}, {e.Data[5]}, {e.Data[6]}, {e.Data[7]}, {e.Data[8]}, {e.Data[9]}, {e.Data[10]}, {e.Data[11]}, {e.Data[12]}");
 }
 private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
 {
     ReceivedData(e.Data);
 }
Exemplo n.º 18
0
 private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
 {
     HandlePayload(e.Data);
 }
Exemplo n.º 19
0
        /// <summary>
        /// Callback for the Ergometer. Contains data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void Ergo_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
        {
            bLESimulator.SaveBytesErgo(e.Data);
            bLESimulator.WriteData(WriteOption.Ergo);
            BLEDecoderErgo.Decrypt(e.Data, this.dataHandler);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Callback for the Heart Rate sensor. Contains data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void HR_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
        {
            bLESimulator.SaveBytesHeartRate(e.Data);
            bLESimulator.WriteData(WriteOption.Heartrate);
            BLEDecoderHR.Decrypt(e.Data, this.dataHandler);
        }