vendredi 27 février 2015

Converting a String from Byte array to int



I've been googling all night for this problem since it is my first time to use arduino and android. My question is, how can I convert the variable data *String to int? I've been getting NumberFormatException whenever I do: int pulse = Integer.ParseInt(data);


My objective here is to be able to get the data coming from the arduino and have it as an integer, for me to be able to compare it.


ADDITIONAL: The variable "data" is a pulse rate. I need to have it converted to int so I can compare the value if the pulse rate is still normal or not. After hours of searching I found out that what I am trying to convert to int is not purely a string since it came from the arduino, now my problem is how can I make the variable "data" an integer.


This is my code:



public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
final int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
final byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;

handler.post(new Runnable()
{
public void run()
{

Intent i = new Intent(Bluetooth.this, Home.class);
i.putExtra("theBPM",data);
startActivity(i);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});

workerThread.start();


Thank you.




Aucun commentaire:

Enregistrer un commentaire