English 中文(简体)
案文为什么意见无法显示序列蓝色线的任何数据?
原标题:why TextView can t show any data from the serial bluetooth?

I need to show data to a TextView from a serial Bluetooth. But when i connect my application with the serial device, it did connected but suddenly went forced closed. The logcat shows nothing so I don t know what s wrong.

这是在申请聆听<条码>输入<>/代码>时使用的代码。

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;
        }
    }
}

and TextView mTextView = (TextView) findViewById(R.id.dataTerm);
and on the layout:

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

So does anyone know what went wrong? Any answers are so helpful, thanks..

www.un.org/spanish/ecosoc 最后运作的法典

  1. On the main file, in my case named FinalSetting:
    On the Activity method, declare:

    //Layout View   
    private static TextView mTextView;
    

    On the onCreate(Bundle savedInstanceState) method declare the 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. On the BluetoothService.java file:
    Let s just straight to the method

    //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);
            }
        }    
    }
    

After you connect to the serial Bluetooth device, the data will show up on the TextView. Hope this help :D

问题回答

你们喜欢的种子试图在非倡议的版面上修改m Text ,这种读物是非法的,并且可能是促进委员会的原因(如果不是任何其他问题)。 然而,你可以做到如下:

Change this:

mTextView.append(new String(buffer));

为此:

mTextView.post(new Runnable() {
    @Override
    public void run() {
        mTextView.append(new String(buffer));
    }
});




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签