am 441b80ab: am 6b4bd260: Merge change I1cd5c828 into eclair

Merge commit '441b80ab3795cb9f02afa7a7397dccd7c17c0cdd' into eclair-mr2-plus-aosp

* commit '441b80ab3795cb9f02afa7a7397dccd7c17c0cdd':
  change the way bytes are read from InputStream.
This commit is contained in:
Scott Main
2009-12-09 15:55:47 -08:00
committed by Android Git Automerger
2 changed files with 31 additions and 27 deletions

View File

@@ -50,14 +50,13 @@ public class BluetoothChat extends Activity {
// Message types sent from the BluetoothChatService Handler // Message types sent from the BluetoothChatService Handler
public static final int MESSAGE_STATE_CHANGE = 1; public static final int MESSAGE_STATE_CHANGE = 1;
public static final int MESSAGE_READ = 2; 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_DEVICE_NAME = 4;
public static final int MESSAGE_TOAST = 5; public static final int MESSAGE_TOAST = 5;
// Key names received from the BluetoothChatService Handler // Key names received from the BluetoothChatService Handler
public static final String DEVICE_NAME = "device_name"; public static final String DEVICE_NAME = "device_name";
public static final String TOAST = "toast"; public static final String TOAST = "toast";
public static final String MESSAGE_WRITE = "message_write";
// Intent request codes // Intent request codes
private static final int REQUEST_CONNECT_DEVICE = 1; private static final int REQUEST_CONNECT_DEVICE = 1;
@@ -192,9 +191,10 @@ public class BluetoothChat extends Activity {
private void ensureDiscoverable() { private void ensureDiscoverable() {
if(D) Log.d(TAG, "ensure discoverable"); 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); 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); startActivity(discoverableIntent);
} }
} }
@@ -223,7 +223,8 @@ public class BluetoothChat extends Activity {
} }
// The action listener for the EditText widget, to listen for the return key // 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) { 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 the action is a key-up event on the return key, send the message
if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) { if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
@@ -257,20 +258,27 @@ public class BluetoothChat extends Activity {
break; break;
} }
break; break;
case MESSAGE_OUTGOING_STRING: case MESSAGE_WRITE:
mConversationArrayAdapter.add("Me: " + new String(msg.getData().getByteArray(MESSAGE_WRITE)).trim()); byte[] writeBuf = (byte[]) msg.obj;
mOutEditText.setText(mOutStringBuffer); // construct a string from the buffer
String writeMessage = new String(writeBuf);
mConversationArrayAdapter.add("Me: " + writeMessage);
break; break;
case MESSAGE_READ: case MESSAGE_READ:
byte[] buf = (byte[]) msg.obj; byte[] readBuf = (byte[]) msg.obj;
mConversationArrayAdapter.add(mConnectedDeviceName+": " + new String(buf).trim()); // construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
mConversationArrayAdapter.add(mConnectedDeviceName+": " + readMessage);
break; break;
case MESSAGE_DEVICE_NAME: case MESSAGE_DEVICE_NAME:
mConnectedDeviceName = msg.getData().getString(DEVICE_NAME); // save the connected device's name // save the connected device's name
Toast.makeText(getApplicationContext(), "Connected to " + mConnectedDeviceName, Toast.LENGTH_SHORT).show(); mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
Toast.makeText(getApplicationContext(), "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
break; break;
case MESSAGE_TOAST: 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; break;
} }
} }
@@ -283,7 +291,8 @@ public class BluetoothChat extends Activity {
// When DeviceListActivity returns with a device to connect // When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
// Get the device MAC address // 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 // Get the BLuetoothDevice object
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
// Attempt to connect to the device // Attempt to connect to the device

View File

@@ -57,7 +57,7 @@ public class BluetoothChatService {
private int mState; private int mState;
// Constants that indicate the current connection state // 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_LISTEN = 1; // now listening for incoming connections
public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection 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 public static final int STATE_CONNECTED = 3; // now connected to a remote device
@@ -397,10 +397,8 @@ public class BluetoothChatService {
bytes = mmInStream.read(buffer); bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity // Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer).sendToTarget(); mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
// Reload the buffer to clear extra bytes from the previous read
buffer = new byte[1024];
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "disconnected", e); Log.e(TAG, "disconnected", e);
connectionLost(); connectionLost();
@@ -411,18 +409,15 @@ public class BluetoothChatService {
/** /**
* Write to the connected OutStream. * 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 { try {
mmOutStream.write(b); mmOutStream.write(buffer);
// Share the sent message back to the UI Activity // Share the sent message back to the UI Activity
Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_OUTGOING_STRING); mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
Bundle bundle = new Bundle(); .sendToTarget();
bundle.putByteArray(BluetoothChat.MESSAGE_WRITE, b);
msg.setData(bundle);
mHandler.sendMessage(msg);
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "Exception during write", e); Log.e(TAG, "Exception during write", e);
} }