Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy TextView can't show any data from the serial bluetooth?
    primarykey
    data
    text
    <p>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 dont know what's wrong.</p> <p>this is the code when application listens the inputstream while connected:</p> <pre><code>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; } } } </code></pre> <p>and <code>TextView mTextView = (TextView) findViewById(R.id.dataTerm);</code> and on the layout:</p> <pre><code>&lt;TextView android:id="@+id/dataTerm" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; </code></pre> <p>So does anyone know what went wrong? Any answers are so helpful,thanks..</p> <p><strong>The code that finally work</strong> 1. On the main file, in my case named FinalSetting: On the Activity method, declare:</p> <pre><code>//Layout View private static TextView mTextView; </code></pre> <p>On the onCreate(Bundle savedInstanceState) method declare the textview:</p> <pre><code> mTextView = (TextView) findViewById(R.id.dataTerm); </code></pre> <p>On the Handler method:</p> <pre><code>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; </code></pre> <ol> <li><p>On the BluetoothService.java file: Let's just straight to the method </p> <p>//This thread runs during a connection with a remote device. //It handles all incoming and outgoing transmissions.</p> <p>private class ConnectedThread extends Thread { private final BluetoothSocket mmSocket; private final InputStream mmInStream; private final OutputStream mmOutStream;</p> <p>public ConnectedThread(BluetoothSocket socket) { Log.d(TAG, "create ConnectedThread"); mmSocket = socket; InputStream tmpIn = null; OutputStream tmpOut = null;</p> <pre><code> // 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); } } } </code></pre></li> </ol> <p>After you connect to the serial bluetooth device, the data will show up on the TextView. Hope this help :D</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload