0

I'm connecting my application to a bluetooth serial connection. I want to change the data which is a String to Integer so I can compare it.

This is the case while reading message:

case MESSAGE_READ:
    byte[] readBuf = (byte[]) msg.obj;              

    // construct a string from the valid bytes in the buffer
    String readMessage = new String(readBuf, 0, msg.arg1);
    Log.i(LOG_TAG, readMessage);

    mTextView.setText(readMessage);
    if(mTextView!=null){
        int read = Integer.parseInt(readMessage.trim());
     if (read<1 && read>28){
        alarm();
        sendSMS();
     }
    }
    /* int read = new Integer(readMessage.trim());
    if (read<1&&read>28){
        alarm();
        sendSMS();
    }*/
    /*if (readMessage.trim().equalsIgnoreCase("20"))
    {
       alarm();
       sendSMS();
    }*/
    break;

And in the LogCat:

05-05 17:12:26.329: D/BluetoothReadService(4371): connected
05-05 17:12:26.339: D/BluetoothReadService(4371): create ConnectedThread
05-05 17:12:26.339: D/BluetoothReadService(4371): setState() 2 -> 3
05-05 17:12:26.349: I/BluetoothReadService(4371): BEGIN mConnectedThread
05-05 17:12:26.369: I/Heart Beat Alarm System(4371): MESSAGE_STATE_CHANGE: 3
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 8
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 9
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 10
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 11
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 12
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 13
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 14
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 15
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 16
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 17
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 18
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 19
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 20
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 21
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 22
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 23
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 24
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 25
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 26
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 27
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 28
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 29
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 0
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 1
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 2
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 3
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 4
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 5
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 6
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 7
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 8
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 9
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 10
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 11
05-05 17:12:26.459: I/Heart Beat Alarm System(4371): 12
05-05 17:12:26.459: D/AndroidRuntime(4371): Shutting down VM
05-05 17:12:26.459: W/dalvikvm(4371): threadid=1: thread exiting with uncaught   exception (group=0x40018578)
05-05 17:12:26.459: E/AndroidRuntime(4371): FATAL EXCEPTION: main
05-05 17:12:26.459: E/AndroidRuntime(4371): java.lang.NumberFormatException: unable to parse '8

05-05 17:12:26.459: E/AndroidRuntime(4371): 9
05-05 17:12:26.459: E/AndroidRuntime(4371): 10
05-05 17:12:26.459: E/AndroidRuntime(4371): 11
05-05 17:12:26.459: E/AndroidRuntime(4371): 12
05-05 17:12:26.459: E/AndroidRuntime(4371): 13
05-05 17:12:26.459: E/AndroidRuntime(4371): 14
05-05 17:12:26.459: E/AndroidRuntime(4371): 15
05-05 17:12:26.459: E/AndroidRuntime(4371): 16
05-05 17:12:26.459: E/AndroidRuntime(4371): 17
05-05 17:12:26.459: E/AndroidRuntime(4371): 18
05-05 17:12:26.459: E/AndroidRuntime(4371): 19
05-05 17:12:26.459: E/AndroidRuntime(4371): 20
05-05 17:12:26.459: E/AndroidRuntime(4371): 21
05-05 17:12:26.459: E/AndroidRuntime(4371): 22
05-05 17:12:26.459: E/AndroidRuntime(4371): 23
05-05 17:12:26.459: E/AndroidRuntime(4371): 24
05-05 17:12:26.459: E/AndroidRuntime(4371): 25
05-05 17:12:26.459: E/AndroidRuntime(4371): 26
05-05 17:12:26.459: E/AndroidRuntime(4371): 27
05-05 17:12:26.459: E/AndroidRuntime(4371): 28
05-05 17:12:26.459: E/AndroidRuntime(4371): 29
05-05 17:12:26.459: E/AndroidRuntime(4371): 0
05-05 17:12:26.459: E/AndroidRuntime(4371): 1
05-05 17:12:26.459: E/AndroidRuntime(4371): 2
05-05 17:12:26.459: E/AndroidRuntime(4371): 3
05-05 17:12:26.459: E/AndroidRuntime(4371): 4
05-05 17:12:26.459: E/AndroidRuntime(4371): 5
05-05 17:12:26.459: E/AndroidRuntime(4371): 6
05-05 17:12:26.459: E/AndroidRuntime(4371): 7
05-05 17:12:26.459: E/AndroidRuntime(4371): 8
05-05 17:12:26.459: E/AndroidRuntime(4371): 9
05-05 17:12:26.459: E/AndroidRuntime(4371): 10
05-05 17:12:26.459: E/AndroidRuntime(4371): 11
05-05 17:12:26.459: E/AndroidRuntime(4371): 12' as integer
05-05 17:12:26.459: E/AndroidRuntime(4371):     at java.lang.Integer.parse(Integer.java:383)
05-05 17:12:26.459: E/AndroidRuntime(4371):     at java.lang.Integer.parseInt(Integer.java:372)
05-05 17:12:26.459: E/AndroidRuntime(4371):     at java.lang.Integer.parseInt(Integer.java:332)
05-05 17:12:26.459: E/AndroidRuntime(4371):     at com.android.hbas.FinalSetting$1.handleMessage(FinalSetting.java:352)
05-05 17:12:26.459: E/AndroidRuntime(4371):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 17:12:26.459: E/AndroidRuntime(4371):     at android.os.Looper.loop(Looper.java:130)
05-05 17:12:26.459: E/AndroidRuntime(4371):     at android.app.ActivityThread.main(ActivityThread.java:3687)
05-05 17:12:26.459: E/AndroidRuntime(4371):     at java.lang.reflect.Method.invokeNative(Native Method)
05-05 17:12:26.459: E/AndroidRuntime(4371):     at java.lang.reflect.Method.invoke(Method.java:507)
05-05 17:12:26.459: E/AndroidRuntime(4371):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-05 17:12:26.459: E/AndroidRuntime(4371):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-05 17:12:26.459: E/AndroidRuntime(4371):     at dalvik.system.NativeStart.main(Native Method)

Then I change it to:

In the Case Message:

case MESSAGE_READ:
                int i;
                byte[] readBuf = (byte[]) msg.obj;              

             // construct a string from the valid bytes in the buffer
                String readMessage = new String(readBuf, 0, msg.arg1);
                Log.i(LOG_TAG, readMessage);

                mTextView.setText(readMessage);

                List<Integer> integers = new ArrayList<Integer>();
                String [] numbers = readMessage.split("\n");
                for (String number : numbers) {
                    integers.add(Integer.valueOf(number.trim()));
                }


               /* int read = new Integer(readMessage.trim());
                if (read<1&&read>28){
                    alarm();
                    sendSMS();
                }*/
                /*if (readMessage.trim().equalsIgnoreCase("20"))
                   {
                       alarm();
                       sendSMS();
                   }*/
                break;

The LogCatShows:

    05-05 23:14:39.519: D/BluetoothReadService(9614): connected
05-05 23:14:39.529: D/BluetoothReadService(9614): create ConnectedThread
05-05 23:14:39.529: D/BluetoothReadService(9614): setState() 2 -> 3
05-05 23:14:39.539: I/BluetoothReadService(9614): BEGIN mConnectedThread
05-05 23:14:39.549: I/Heart Beat Alarm System(9614): MESSAGE_STATE_CHANGE: 3
05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 4

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 5

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 6

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 7

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 8

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 9

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 10

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 11

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 12

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 13

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 14

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 15

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 16

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 17

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 18

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 19

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 20

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 21

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 22

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 23

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 24

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 25

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 26

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 27

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 28

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 29

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 0

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 1

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 2

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 3

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 4

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 5

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 6

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 7

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 8

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 9

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 10

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 11

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 12

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 13

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 14

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 15

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 16

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 17

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 18

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 19

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 20

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 21

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 22

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 23

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 24

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 25

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 26

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 27

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 28

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 29

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 0

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 1

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 2

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 3

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 4

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 5

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 6

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 7

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 8

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 9

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 10

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 11

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 12

05-05 23:14:39.649: I/Heart Beat Alarm System(9614): 13
05-05 23:14:39.659: D/AndroidRuntime(9614): Shutting down VM
05-05 23:14:39.659: W/dalvikvm(9614): threadid=1: thread exiting with uncaught exception (group=0x40018578)
05-05 23:14:39.659: E/AndroidRuntime(9614): FATAL EXCEPTION: main
05-05 23:14:39.659: E/AndroidRuntime(9614): java.lang.NumberFormatException: unable to parse '' as integer
05-05 23:14:39.659: E/AndroidRuntime(9614):     at java.lang.Integer.parseInt(Integer.java:362)
05-05 23:14:39.659: E/AndroidRuntime(9614):     at java.lang.Integer.parseInt(Integer.java:332)
05-05 23:14:39.659: E/AndroidRuntime(9614):     at java.lang.Integer.valueOf(Integer.java:506)
05-05 23:14:39.659: E/AndroidRuntime(9614):     at com.android.hbas.FinalSetting$1.handleMessage(FinalSetting.java:355)
05-05 23:14:39.659: E/AndroidRuntime(9614):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 23:14:39.659: E/AndroidRuntime(9614):     at android.os.Looper.loop(Looper.java:130)
05-05 23:14:39.659: E/AndroidRuntime(9614):     at android.app.ActivityThread.main(ActivityThread.java:3687)
05-05 23:14:39.659: E/AndroidRuntime(9614):     at java.lang.reflect.Method.invokeNative(Native Method)
05-05 23:14:39.659: E/AndroidRuntime(9614):     at java.lang.reflect.Method.invoke(Method.java:507)
05-05 23:14:39.659: E/AndroidRuntime(9614):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-05 23:14:39.659: E/AndroidRuntime(9614):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-05 23:14:39.659: E/AndroidRuntime(9614):     at dalvik.system.NativeStart.main(Native Method)

Does anyone knows the solution? Thanks so much..

1
  • Please share line of code FinalSetting.java:352, Its look like you are trying to parse invalid no Commented May 5, 2012 at 10:19

2 Answers 2

2

Your message contains a string with a lot of numbers, on separate lines. You can not parse such string in single integer - thus you get your error.

You can try reading the integer array like that:

List<Integer> integers = new ArrayList<Integer>();
String [] numbers = readMessage.split("\n");
for (String number : numbers) {
    integers.add(Integer.valueOf(number.trim()));
}

After this code integers will contain the numbers you receive in the message.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, i follow ur instructions but still has error. For complete info, look at my new edited question please :D
0

I had the same problem when i surrounded it with try-catch block, it started work. like:

try 
    {
    size_of_file=Integer.parseInt(Value);
    }
    catch(Exception obj)
    {
        Toast.makeText(this, "Error :"+obj.getMessage(), Toast.LENGTH_LONG).show();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.