Android:死代码?

时间:2014-06-25 11:39:07

标签: android bluetooth switch-statement headset

我正在创建一个使用蓝牙连接到NeuroSky耳机的Android应用程序。在下面的代码中,我收到了if语句的“死代码”错误,无法找出原因?

特定代码块:

(我正在尝试创建一种功能,如果耳机注册的注意力超过25,那么消息就会输出到屏幕,这就是我得到了死代码错误。)

请注意,TGDevice.MSG_ATTENTION是0-100

的int值
case TGDevice.MSG_ATTENTION:
                    tv.append("Attention: " + msg.arg1 + "\n");

                    if(TGDevice.MSG_ATTENTION>=25){
                        System.out.println("Over 25...");
                    }
                    break;

完整方法:

/**
     * Handles messages from TGDevice
     */
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
            case TGDevice.MSG_STATE_CHANGE:

                switch (msg.arg1) {
                case TGDevice.STATE_IDLE:
                    break;
                case TGDevice.STATE_CONNECTING:
                    tv.append("Connecting...\n");
                    break;
                case TGDevice.STATE_CONNECTED:
                    tv.append("Connected.\n");
                    device.start();
                    break;
                case TGDevice.STATE_NOT_FOUND:
                    tv.append("Could not connect any of the paired BT devices.  Turn them on and try again.\n");
                    break;
                case TGDevice.STATE_ERR_NO_DEVICE:
                    tv.append("No Bluetooth devices paired.  Pair your device and try again.\n");
                    break;
                case TGDevice.STATE_ERR_BT_OFF:
                    tv.append("Bluetooth is off.  Turn on Bluetooth and try again.");
                    break;

                case TGDevice.STATE_DISCONNECTED:
                    tv.append("Disconnected.\n");
                } /* end switch on msg.arg1 */

                break;

            case TGDevice.MSG_POOR_SIGNAL:
                tv.append("PoorSignal: " + msg.arg1 + "\n");
                break;

            case TGDevice.MSG_HEART_RATE:
                tv.append("Heart rate: " + msg.arg1 + "\n");
                break;

            case TGDevice.MSG_RAW_DATA:
                /* Handle raw EEG/EKG data here */
                break;

            case TGDevice.MSG_ATTENTION:
                tv.append("Attention: " + msg.arg1 + "\n");

                if(TGDevice.MSG_ATTENTION>=25){
                    System.out.println("Over 25...");
                }
                break;

            case TGDevice.MSG_MEDITATION:
                tv.append("Meditation: " + msg.arg1 + "\n");
                break;

            case TGDevice.MSG_BLINK:
                tv.append("Blink: " + msg.arg1 + "\n");
                break;

            default:
                break;

            } /* end switch on msg.what */

            sv.fullScroll(View.FOCUS_DOWN);

        } /* end handleMessage() */

    }; /* end Handler */

1 个答案:

答案 0 :(得分:0)

尝试这样..它可能有效

/**
     * Handles messages from TGDevice
     */
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            if(msg.what == TGDevice.MSG_ATTENTION) {
                //save your value in a global variable
            }

            switch (msg.what) {
            case TGDevice.MSG_STATE_CHANGE:

                switch (msg.arg1) {
                case TGDevice.STATE_IDLE:
                    break;
                case TGDevice.STATE_CONNECTING:
                    tv.append("Connecting...\n");
                    break;
                case TGDevice.STATE_CONNECTED:
                    tv.append("Connected.\n");
                    device.start();
                    break;
                case TGDevice.STATE_NOT_FOUND:
                    tv.append("Could not connect any of the paired BT devices.  Turn them on and try again.\n");
                    break;
                case TGDevice.STATE_ERR_NO_DEVICE:
                    tv.append("No Bluetooth devices paired.  Pair your device and try again.\n");
                    break;
                case TGDevice.STATE_ERR_BT_OFF:
                    tv.append("Bluetooth is off.  Turn on Bluetooth and try again.");
                    break;

                case TGDevice.STATE_DISCONNECTED:
                    tv.append("Disconnected.\n");
                } /* end switch on msg.arg1 */

                break;

            case TGDevice.MSG_POOR_SIGNAL:
                tv.append("PoorSignal: " + msg.arg1 + "\n");
                break;

            case TGDevice.MSG_HEART_RATE:
                tv.append("Heart rate: " + msg.arg1 + "\n");
                break;

            case TGDevice.MSG_RAW_DATA:
                /* Handle raw EEG/EKG data here */
                break;

            case TGDevice.MSG_ATTENTION:
                tv.append("Attention: " + msg.arg1 + "\n");

                if(that global variable value >= 25){
                    System.out.println("Over 25...");
                }
                break;

            case TGDevice.MSG_MEDITATION:
                tv.append("Meditation: " + msg.arg1 + "\n");
                break;

            case TGDevice.MSG_BLINK:
                tv.append("Blink: " + msg.arg1 + "\n");
                break;

            default:
                break;

            } /* end switch on msg.what */

            sv.fullScroll(View.FOCUS_DOWN);

        } /* end handleMessage() */

    }; /* end Handler */