diff --git a/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChat.java b/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChat.java index e4b9d5289..d05bbd611 100644 --- a/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChat.java +++ b/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChat.java @@ -50,14 +50,13 @@ public class BluetoothChat extends Activity { // Message types sent from the BluetoothChatService Handler public static final int MESSAGE_STATE_CHANGE = 1; public static final int MESSAGE_READ = 2; - public static final int MESSAGE_OUTGOING_STRING = 3; + public static final int MESSAGE_WRITE = 3; public static final int MESSAGE_DEVICE_NAME = 4; public static final int MESSAGE_TOAST = 5; // Key names received from the BluetoothChatService Handler public static final String DEVICE_NAME = "device_name"; public static final String TOAST = "toast"; - public static final String MESSAGE_WRITE = "message_write"; // Intent request codes private static final int REQUEST_CONNECT_DEVICE = 1; @@ -192,9 +191,10 @@ public class BluetoothChat extends Activity { private void ensureDiscoverable() { if(D) Log.d(TAG, "ensure discoverable"); - if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { + if (mBluetoothAdapter.getScanMode() != + BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); - discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); // set max duration + discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(discoverableIntent); } } @@ -223,7 +223,8 @@ public class BluetoothChat extends Activity { } // The action listener for the EditText widget, to listen for the return key - private TextView.OnEditorActionListener mWriteListener = new TextView.OnEditorActionListener() { + private TextView.OnEditorActionListener mWriteListener = + new TextView.OnEditorActionListener() { public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { // If the action is a key-up event on the return key, send the message if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) { @@ -257,20 +258,27 @@ public class BluetoothChat extends Activity { break; } break; - case MESSAGE_OUTGOING_STRING: - mConversationArrayAdapter.add("Me: " + new String(msg.getData().getByteArray(MESSAGE_WRITE)).trim()); - mOutEditText.setText(mOutStringBuffer); + case MESSAGE_WRITE: + byte[] writeBuf = (byte[]) msg.obj; + // construct a string from the buffer + String writeMessage = new String(writeBuf); + mConversationArrayAdapter.add("Me: " + writeMessage); break; case MESSAGE_READ: - byte[] buf = (byte[]) msg.obj; - mConversationArrayAdapter.add(mConnectedDeviceName+": " + new String(buf).trim()); + byte[] readBuf = (byte[]) msg.obj; + // construct a string from the valid bytes in the buffer + String readMessage = new String(readBuf, 0, msg.arg1); + mConversationArrayAdapter.add(mConnectedDeviceName+": " + readMessage); break; case MESSAGE_DEVICE_NAME: - mConnectedDeviceName = msg.getData().getString(DEVICE_NAME); // save the connected device's name - Toast.makeText(getApplicationContext(), "Connected to " + mConnectedDeviceName, Toast.LENGTH_SHORT).show(); + // save the connected device's name + mConnectedDeviceName = msg.getData().getString(DEVICE_NAME); + Toast.makeText(getApplicationContext(), "Connected to " + + mConnectedDeviceName, Toast.LENGTH_SHORT).show(); break; case MESSAGE_TOAST: - Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST), + Toast.LENGTH_SHORT).show(); break; } } @@ -283,7 +291,8 @@ public class BluetoothChat extends Activity { // When DeviceListActivity returns with a device to connect if (resultCode == Activity.RESULT_OK) { // Get the device MAC address - String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); + String address = data.getExtras() + .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); // Get the BLuetoothDevice object BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); // Attempt to connect to the device diff --git a/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java b/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java index 8d29c6a99..d0c1d43cd 100644 --- a/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java +++ b/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java @@ -57,7 +57,7 @@ public class BluetoothChatService { private int mState; // Constants that indicate the current connection state - public static final int STATE_NONE = 0; // we're doing nothing. only valid during setup/shutdown + public static final int STATE_NONE = 0; // we're doing nothing public static final int STATE_LISTEN = 1; // now listening for incoming connections public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection public static final int STATE_CONNECTED = 3; // now connected to a remote device @@ -397,10 +397,8 @@ public class BluetoothChatService { bytes = mmInStream.read(buffer); // Send the obtained bytes to the UI Activity - mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer).sendToTarget(); - - // Reload the buffer to clear extra bytes from the previous read - buffer = new byte[1024]; + mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer) + .sendToTarget(); } catch (IOException e) { Log.e(TAG, "disconnected", e); connectionLost(); @@ -411,18 +409,15 @@ public class BluetoothChatService { /** * Write to the connected OutStream. - * @param b The bytes to write + * @param buffer The bytes to write */ - public void write(byte[] b) { + public void write(byte[] buffer) { try { - mmOutStream.write(b); + mmOutStream.write(buffer); // Share the sent message back to the UI Activity - Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_OUTGOING_STRING); - Bundle bundle = new Bundle(); - bundle.putByteArray(BluetoothChat.MESSAGE_WRITE, b); - msg.setData(bundle); - mHandler.sendMessage(msg); + mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer) + .sendToTarget(); } catch (IOException e) { Log.e(TAG, "Exception during write", e); }