1

我需要TextView从串行显示数据Bluetooth。但是当我将我的应用程序与串行设备连接时,它确实连接但突然被强制关闭。logcat 什么也没显示,所以我不知道出了什么问题。

这是应用程序inputstream在连接时侦听的代码:

public void run() {
    Log.i(TAG, "BEGIN mConnectedThread");
    byte[] buffer = new byte[1024];
    int bytes;

    while (true) {
        try {

            // Read from the InputStream
            bytes = mmInStream.read(buffer);

            //mEmulatorView.write(buffer, bytes);

            mTextView.append(new String(buffer));
            // Send the obtained bytes to the UI Activity
            //mHandler.obtainMessage(BlueTerm.MESSAGE_READ, bytes, -1, buffer).sendToTarget();

            String a = buffer.toString();
            mTextView.setText(a);
            a = "";
        } catch (IOException e) {
            Log.e(TAG, "disconnected", e);
            connectionLost();
            break;
        }
    }
}

并且TextView mTextView = (TextView) findViewById(R.id.dataTerm);
在布局上:

<TextView
    android:id="@+id/dataTerm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

那么有人知道出了什么问题吗?任何答案都非常有帮助,谢谢..

最终工作的代码

  1. 在主文件上,在我的例子中名为 FinalSetting:
    在 Activity 方法上,声明:

    //Layout View   
    private static TextView mTextView;
    

    onCreate(Bundle savedInstanceState)方法上声明textview

    mTextView = (TextView) findViewById(R.id.dataTerm);
    

    关于Handler方法:

    case MESSAGE_READ:
        byte[] readBuf = (byte[]) msg.obj;              
        //mEmulatorView.write(readBuf, msg.arg1);
        // construct a string from the valid bytes in the buffer
        String readMessage = new String(readBuf, 0, msg.arg1);
        //mConversationArrayAdapter.add(mConnectedDeviceName+":  " + readMessage);
        mTextView.setText(readMessage);
        break;
    
  2. 关于BluetoothService.java文件:
    让我们直接进入方法

    //This thread runs during a connection with a remote device.
    //It handles all incoming and outgoing transmissions.
    
    private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;
    
        public ConnectedThread(BluetoothSocket socket) {
            Log.d(TAG, "create ConnectedThread");
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;
    
            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }
            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }
    
        public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[1024];
            //final byte[] buffer = new byte[1024];
            int bytes;
    
            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);
    
                    //Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(FinalSetting.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }
            }
        }
    
        /**
        * Write to the connected OutStream.
        * @param buffer  The bytes to write
        */
        public void write(byte[] buffer) {
            try {
                mmOutStream.write(buffer);
    
                // Share the sent message back to the UI Activity
                mHandler.obtainMessage(FinalSetting.MESSAGE_WRITE, buffer.length, -1, buffer).sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "Exception during write", e);
            }
        }
    
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }    
    }
    

连接到串行Bluetooth设备后,数据将显示在TextView. 希望这有帮助:D

4

1 回答 1

0

似乎您正在尝试在非 UI 线程中修改mTextView ,这是非法的,可能是 FC 的原因(如果不是任何其他问题)。但是,您可以按如下方式实现:

改变这个:

mTextView.append(new String(buffer));

对此:

mTextView.post(new Runnable() {
    @Override
    public void run() {
        mTextView.append(new String(buffer));
    }
});
于 2012-04-24T08:48:58.797 回答