How to get the right values โ€‹โ€‹from a Polar heart rate monitor

My android application receives data from the Polar Heart Rate Monitor via a Bluetooth connection. My problem is that I get a line like this:

My code for getting data:

final Handler handler = new Handler(); final byte delimiter = 10; //This is the ASCII code for a newline character stopWorker = false; readBufferPosition = 0; readBuffer = new byte[1024]; workerThread = new Thread(new Runnable() { public void run() { while(!Thread.currentThread().isInterrupted() && !stopWorker) { try { int bytesAvailable = mmInputStream.available(); if(bytesAvailable > 0) { byte[] packetBytes = new byte[bytesAvailable]; mmInputStream.read(packetBytes); for(int i=0;i<bytesAvailable;i++) { byte b = packetBytes[i]; if(b == delimiter) { byte[] encodedBytes = new byte[readBufferPosition]; // System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length); final String data = new String(encodedBytes, "ASCII"); readBufferPosition = 0; handler.post(new Runnable() { public void run() { pulsText.setText(data); } }); } else { readBuffer[readBufferPosition++] = b; } } } } catch (IOException ex) { stopWorker = true; } } } }); workerThread.start(); 

I tried changing this line in several ways, but I still get the wrong data:

  final String data = new String(encodedBytes, "ASCII"); 

How can I solve this problem?

Please, help!!!

+6
source share
2 answers

The sensor does not give you printable lines (e.g. NMEA), but binary data that needs to be analyzed. You can take a look at the MyTracks Polar Sensor parser for inspiration.

You misuse available and read (but the way you use it can be a great luck).

+4
source

Units (based on the Raspberry Pi Challenge in JavaOne) Glass Heart shows how this can be parsed into the typeafe Heartbeat Unit for display or transfer to other systems.

0
source

Source: https://habr.com/ru/post/913069/


All Articles