Rosselle
Rosselle

Reputation: 3

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.

Upvotes: 0

Views: 175

Answers (2)

busybug91
busybug91

Reputation: 241

Try printing/logging/making a toast to see what is in data. This can happen only when data has characters other than digits or may be you have a double(decimal) value in data. For that you might need to use Double.parseDouble(data).

Upvotes: 0

richersoon
richersoon

Reputation: 4902

We assume that the problem is String to int and you already stepped in debug mode or make some Toast message that you are getting the string representation as expected.

You can try the following option:

Upvotes: 1

Related Questions