私のAndroidアプリケーションは、Bluetooth接続を介してPolar Heart Rate Monitorからデータを取得しています。 私の問題は、私はそのような文字列を取得していることです: Polar心拍数モニタから適切な値を得るには
データを取得するための私のコード:
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();
私はいくつかの方法で、この行を変更しようとしましたが、私はまだ間違ったデータを取得しています:
final String data = new String(encodedBytes, "ASCII");
どうすればこの問題を解決できますか?
助けてください!
レンダリングされた文字列ではなく、生のバイト値を調べましたか? –
あなたの文字が印刷可能範囲を超えているようです。あなたは文字ではなく整数として見て、それがもう意味をなさないかどうかを見なければなりません。 – twain249
@erniどのように問題を解決しましたか? – Hunt