0

我正在将我的应用程序连接到蓝牙串行连接。我想更改数据,String以便Integer我可以比较它。

阅读消息时是这种情况:

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;

在 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)

然后我将其更改为:

在案例消息中:

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;

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)

有谁知道解决方案?非常感谢..

4

2 回答 2

2

您的消息包含一个带有很多数字的字符串,在不同的行上。您无法以单个整数解析此类字符串 - 因此您会收到错误消息。

您可以尝试像这样读取整数数组:

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

此代码之后integers将包含您在消息中收到的号码。

于 2012-05-05T10:22:35.343 回答
0

当我用 try-catch 块包围它时,我遇到了同样的问题,它开始工作了。喜欢:

try 
    {
    size_of_file=Integer.parseInt(Value);
    }
    catch(Exception obj)
    {
        Toast.makeText(this, "Error :"+obj.getMessage(), Toast.LENGTH_LONG).show();
    }
于 2013-03-02T20:34:39.117 回答