Languages

Menu
Sites
Language
[Web applicaiton]Get Hrm data from Gear S3

Good day developers.


I'm trying to Hrm detecting Web application for this vacation.

It's based on open source from Samsung HelloAccesary and HeartRateMonitor sample.

My purpose is detecting Heartreate, also it passes data to my Phone.

So, Phone(OS is Android)'s apk will compare rate, if the data over '120', then it make events.

But problem is apk can't distinguish the Rate. It always distinguish 'message' is bigger than 'heart_attack_rate'.

However, the value is low.

 

If you can help me, please give som advice 

Thnak you.

ps. at the below, there's code.

 

public int cmp_number(String num1, String num2)

 

{ int store_num = num1.compareTo(num2); return store_num;} //Function, which compare two String values

 

 

// 180806 public void onReceive(int channelId, byte[] data)

{ final String message = new String(data);

String heart_attack_rate="120"; //heart attack rate

int cmnumber=cmp_number(heart_attack_rate,message); //compare to values

if (cmnumber<0)

{ addMessage("Detected Hrm Rate: ", message);

addMessage("Detected number: ", Integer.toString(cmnumber)); }//normal state

else

{ updateTextView("Heart attack!!");

addMessage("Heart attack happened: ", message);

addMessage("Detected number: ", Integer.toString(cmnumber));// for looking compareto() value

} }//heart attak happen

Edited by: 현철 조 on 07 Aug, 2018

Responses

11 Replies
Armaan-Ul- Islam

String.compareTo() compares lexicographically (based on difference of character value), This is not what you are looking for.

To compare two numbers, Parse both Strings into Integer value first, then take decision.

Try this Code Snippet:

public static int cmp_number(String num1, String num2) {

    int num1Int = Integer.parseInt(num1);
    int num2Int = Integer.parseInt(num2);

    if(num1Int > num2Int)
        return -1; //greater than range
    else
        return 1;  //less than range
}

 

현철 조

Hi, thank you for your help.

 

I took your advise, and tried to fix it, but it occured error and stop running.

Maybe it is related with DynamicArray.

 

Samsung's HelloAccesary source uses DynamicArray for saving data which comes from Gear S3.

They use Collections.synchronizedList for String.

 

Have a good day

Armaan-Ul- Islam

I'm assuming you getting HRM data as byteArray (byte []).

In that case, Create a new String sending the byte array to Constructor. Code Sample:

public static int cmp_number(String num1, String num2) {

    int num1Int = Integer.parseInt(num1);
    int num2Int = Integer.parseInt(num2);

    if(num1Int > num2Int)
        return -1; //greater than range
    else
        return 1;  //less than range
}


// 180806 public void onReceive(int channelId, byte[] data)
{ 
    final String message = new String(data,StandardCharsets.UTF_8);
    String heart_attack_rate="120"; //heart attack rate

    int cmnumber=cmp_number(heart_attack_rate,message); //compare to values
......}

 

A Relevant Post on Stack Overflow.

Armaan-Ul- Islam

Hello 현철 조 , did setting the encoding as 'StandardCharsets.UTF_8' worked out for you ? You might print the byte [] data for further understanding.

Please share current status here.

현철 조

Sorry ofr answering so late. I tripped foreign country.

That's why I can't answer.

Also, thank you for keep intersting my project.

I try to 'StandardCharsets.UTF_8'.

But it doesn't work. Maybe Dynamic array and cahing it to String causes problem .

And i will add my code.

 

<code>

public void onReceive(int channelId, byte[] data) {
    int heart_attack_rate=1147499621;
    final String message = new String(data);//build.gradle(Module: app)
    int changed_value=byteArrayToInt(data);

    //int cmpnumber=cmp_number(heart_attack_rate,message);
    if (changed_value<heart_attack_rate)
    { addMessage("Detected Hrm Rate: ", message);
      addMessage("Detected Rate: ", Integer.toString(changed_value));
    }//normal state
    else
    { updateTextView("Heart attack!!");
        addMessage("Heart attack happened: ", message);
        addMessage("Detected Rate: ", Integer.toString(changed_value));
    } }

</code>

Armaan-Ul- Islam

Please Share what data/format do you receive when you print/log 'message' & 'changed_value'.

현철 조

Thank you for come again

For printing 'message',  i use 'addmessage'function.

Also, message's format is  String, but when first it comes from Gear S3 is  byte type array.

Byte type array's name is 'data'. It is transfered from S3, by bluetooth Socket communication.

<code>

private void addMessage(final String prefix, final String data) {
    final String strToUI = prefix.concat(data);
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            ConsumerActivity.addMessage(strToUI);
        }
    });
}

</code>

 

Also, 'changed_value' is Integer type.

I make that variable to check what had in the 'data'

And 'byteArraytoInt()' function's source is undered.

 

<code>

public double byteArrayToInt(byte [] b) {
    return (b[0]<< 56) + ((b[1] & 0xFF) << 48) + ((b[2] & 0xFF) << 40) + ((b[3] & 0xFF)<<32)
            + ((b[4] & 0xFF) << 24)+ ((b[5] & 0xFF) << 16)+ ((b[6] & 0xFF) << 8)+ (b[7] & 0xFF);
}

</code>

 

Armaan-Ul- Islam

Thanks for the details.

But I was asking for the Output of 'message' & 'changed_value'.

현철 조

It's sure thing.

Unfortunately, it occurs errors, before my device prints output.

So, I can't check how it shows.

Armaan-Ul- Islam

I was aware of the HRM data (Integer) comparison problem, Not about the Error.

 

Please share the Error message thrown.

현철 조

Sorry for answering late.

New semester was started, so i was too busy to check it.

Renting Gear S3 duration was over, so i have to return it.

That's why i can't afford more information about Error message.

But, i will buy Galaxy Watch on end of September.

If you don't mind, please mail to  "chhyunchul@naver.com".

Then I will share the information about source and screenshot, after buy a Galaxy watch.