带有handleMessage的ClassCastException

时间:2013-03-20 13:46:45

标签: bluetooth handle classcastexception

调试蓝牙连接的代码位接收字符串等但是,似乎在代码行中获得ClassCastException

String read_Message = (String) msg.obj;

但是,如果我要使用我之前的代码并收集字节并放入字符串运行但不收集一个字符串中的所有数据。

我没有投出某些东西或遗漏某些东西,因为我知道我错过了一些但却看不到的东西。

如果需要更多代码,我会放置,其他一切正常工作

感谢您的帮助

     // The Handler that gets information back from the BluetoothService
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) 
        {
            switch (msg.what)
            {
            case MESSAGE_STATE_CHANGE:
//              if (D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
                switch (msg.arg1) 
                {
                case BluetoothService.STATE_CONNECTED:
                    break;
                case BluetoothService.STATE_CONNECTING:
                    //mTitle.setText(R.string.title_connecting);
                    break;
                case BluetoothService.STATE_LISTEN:
                case BluetoothService.STATE_NONE:
                    //mTitle.setText(R.string.title_not_connected);
                    break;
                }
                break;
            case MESSAGE_WRITE:
                //code to be entered here
                break;
            case MESSAGE_READ:
                //Previous code
                //byte[] read_Buf = (byte[]) msg.obj;
                // construct a string from the valid bytes in the buffer
                //String read_Message = new String(read_Buf, 0, msg.arg1);

                String read_Message = (String) msg.obj;

                if (mSmokeReadingArrayAdapter.isEmpty())
                    mSmokeReadingArrayAdapter.add("");

                mAdapter_Text.set(0, mAdapter_Text.get(0).toString() + read_Message);  

                mSmokeReadingArrayAdapter.notifyDataSetChanged();

                organiseString(read_Message);
                break;
            case MESSAGE_DEVICE_NAME:
                //code to be entered here
                break;
            case MESSAGE_TOAST:
                Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST), Toast.LENGTH_SHORT).show();     // shows 'alert' messages
                break;
            }
        }
    };

1 个答案:

答案 0 :(得分:0)

问题解决了。它与已定义为字节数组而非字符串

的蓝牙服务代码有关