kgehmlich / coen390-team-d

A heartrate monitoring app for firefighters
0 stars 0 forks source link

Fix negative heart rate #7

Closed kgehmlich closed 7 years ago

kgehmlich commented 7 years ago

Heart rate is reported as a single (unsigned) byte, which is interpreted as signed in the sample code. This ends up reporting the heart rate as negative when it goes over 127.

Pierre-Lucas commented 7 years ago

static int unsignedByteToInt(byte b) { return (int) b & 0xFF; }

and use here in the "int HRate" assignment, line 55 In NewConnectedListener.java ..I believe `byte [] DataArray = msg.getBytes();

                //***************Displaying the Heart Rate********************************
                int HRate =  unsignedByteToInt( HRSpeedDistPacket.GetHeartRate(DataArray) );
                Message text1 = _aNewHandler.obtainMessage(HEART_RATE);
                Bundle b1 = new Bundle();
                b1.putString("HeartRate", String.valueOf(HRate));
                text1.setData(b1);
                _aNewHandler.sendMessage(text1);
                System.out.println("Heart Rate is "+ HRate);`

It's not a solution to the root problem... but it fixes it by painlessly converting the unsigned byte to a regular int.

Ref : http://www.rgagnon.com/javadetails/java-0026.html

kgehmlich commented 7 years ago

Same solution I found, although I didn't bother packaging it in its own method (which I like, I'll use your suggestion). Testing soon, I'll close this issue once I confirm it works.

It's not a solution to the root problem

In a way it is. Java assumes all bytes are signed, so this gets as close as possible to the issue short of changing Java itself.

kgehmlich commented 7 years ago

Fixed and tested. Closing issue.