Do Not Merge: Modify Bluetooth Chat app for new insecure rfcomm API.

Also fix some bugs.
Bug: 3352266

Change-Id: Ie64cb243159b1f6eea5f268f02a1dd58970d1e22
This commit is contained in:
Jaikumar Ganesh
2011-01-14 11:08:58 -08:00
parent daf0b895e8
commit 6314c20eeb
4 changed files with 139 additions and 61 deletions

View File

@@ -14,9 +14,12 @@
limitations under the License. limitations under the License.
--> -->
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/scan" <item android:id="@+id/secure_connect_scan"
android:icon="@android:drawable/ic_menu_search" android:icon="@android:drawable/ic_menu_search"
android:title="@string/connect" /> android:title="@string/secure_connect" />
<item android:id="@+id/insecure_connect_scan"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/insecure_connect" />
<item android:id="@+id/discoverable" <item android:id="@+id/discoverable"
android:icon="@android:drawable/ic_menu_mylocation" android:icon="@android:drawable/ic_menu_mylocation"
android:title="@string/discoverable" /> android:title="@string/discoverable" />

View File

@@ -35,6 +35,7 @@
<string name="button_scan">Scan for devices</string> <string name="button_scan">Scan for devices</string>
<!-- Options Menu --> <!-- Options Menu -->
<string name="connect">Connect a device</string> <string name="secure_connect">Connect a device - Secure</string>
<string name="insecure_connect">Connect a device - Insecure</string>
<string name="discoverable">Make discoverable</string> <string name="discoverable">Make discoverable</string>
</resources> </resources>

View File

@@ -59,8 +59,9 @@ public class BluetoothChat extends Activity {
public static final String TOAST = "toast"; public static final String TOAST = "toast";
// Intent request codes // Intent request codes
private static final int REQUEST_CONNECT_DEVICE = 1; private static final int REQUEST_CONNECT_DEVICE_SECURE = 1;
private static final int REQUEST_ENABLE_BT = 2; private static final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
private static final int REQUEST_ENABLE_BT = 3;
// Layout Views // Layout Views
private TextView mTitle; private TextView mTitle;
@@ -287,16 +288,16 @@ public class BluetoothChat extends Activity {
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(D) Log.d(TAG, "onActivityResult " + resultCode); if(D) Log.d(TAG, "onActivityResult " + resultCode);
switch (requestCode) { switch (requestCode) {
case REQUEST_CONNECT_DEVICE: case REQUEST_CONNECT_DEVICE_SECURE:
// 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 connectDevice(data, true);
String address = data.getExtras() }
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); break;
// Get the BLuetoothDevice object case REQUEST_CONNECT_DEVICE_INSECURE:
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); // When DeviceListActivity returns with a device to connect
// Attempt to connect to the device if (resultCode == Activity.RESULT_OK) {
mChatService.connect(device); connectDevice(data, false);
} }
break; break;
case REQUEST_ENABLE_BT: case REQUEST_ENABLE_BT:
@@ -313,6 +314,16 @@ public class BluetoothChat extends Activity {
} }
} }
private void connectDevice(Intent data, boolean secure) {
// Get the device MAC 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
mChatService.connect(device, secure);
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater(); MenuInflater inflater = getMenuInflater();
@@ -322,11 +333,17 @@ public class BluetoothChat extends Activity {
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
Intent serverIntent = null;
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.scan: case R.id.secure_connect_scan:
// Launch the DeviceListActivity to see devices and do scan // Launch the DeviceListActivity to see devices and do scan
Intent serverIntent = new Intent(this, DeviceListActivity.class); serverIntent = new Intent(this, DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE); startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_SECURE);
return true;
case R.id.insecure_connect_scan:
// Launch the DeviceListActivity to see devices and do scan
serverIntent = new Intent(this, DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE);
return true; return true;
case R.id.discoverable: case R.id.discoverable:
// Ensure this device is discoverable by others // Ensure this device is discoverable by others
@@ -336,4 +353,4 @@ public class BluetoothChat extends Activity {
return false; return false;
} }
} }

View File

@@ -43,15 +43,20 @@ public class BluetoothChatService {
private static final boolean D = true; private static final boolean D = true;
// Name for the SDP record when creating server socket // Name for the SDP record when creating server socket
private static final String NAME = "BluetoothChat"; private static final String NAME_SECURE = "BluetoothChatSecure";
private static final String NAME_INSECURE = "BluetoothChatInsecure";
// Unique UUID for this application // Unique UUID for this application
private static final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"); private static final UUID MY_UUID_SECURE =
UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID MY_UUID_INSECURE =
UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
// Member fields // Member fields
private final BluetoothAdapter mAdapter; private final BluetoothAdapter mAdapter;
private final Handler mHandler; private final Handler mHandler;
private AcceptThread mAcceptThread; private AcceptThread mSecureAcceptThread;
private AcceptThread mInsecureAcceptThread;
private ConnectThread mConnectThread; private ConnectThread mConnectThread;
private ConnectedThread mConnectedThread; private ConnectedThread mConnectedThread;
private int mState; private int mState;
@@ -103,19 +108,25 @@ public class BluetoothChatService {
// Cancel any thread currently running a connection // Cancel any thread currently running a connection
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;} if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// Start the thread to listen on a BluetoothServerSocket
if (mAcceptThread == null) {
mAcceptThread = new AcceptThread();
mAcceptThread.start();
}
setState(STATE_LISTEN); setState(STATE_LISTEN);
// Start the thread to listen on a BluetoothServerSocket
if (mSecureAcceptThread == null) {
mSecureAcceptThread = new AcceptThread(true);
mSecureAcceptThread.start();
}
if (mInsecureAcceptThread == null) {
mInsecureAcceptThread = new AcceptThread(false);
mInsecureAcceptThread.start();
}
} }
/** /**
* Start the ConnectThread to initiate a connection to a remote device. * Start the ConnectThread to initiate a connection to a remote device.
* @param device The BluetoothDevice to connect * @param device The BluetoothDevice to connect
* @param secure Socket Security type - Secure (true) , Insecure (false)
*/ */
public synchronized void connect(BluetoothDevice device) { public synchronized void connect(BluetoothDevice device, boolean secure) {
if (D) Log.d(TAG, "connect to: " + device); if (D) Log.d(TAG, "connect to: " + device);
// Cancel any thread attempting to make a connection // Cancel any thread attempting to make a connection
@@ -127,7 +138,7 @@ public class BluetoothChatService {
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;} if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// Start the thread to connect with the given device // Start the thread to connect with the given device
mConnectThread = new ConnectThread(device); mConnectThread = new ConnectThread(device, secure);
mConnectThread.start(); mConnectThread.start();
setState(STATE_CONNECTING); setState(STATE_CONNECTING);
} }
@@ -137,8 +148,9 @@ public class BluetoothChatService {
* @param socket The BluetoothSocket on which the connection was made * @param socket The BluetoothSocket on which the connection was made
* @param device The BluetoothDevice that has been connected * @param device The BluetoothDevice that has been connected
*/ */
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) { public synchronized void connected(BluetoothSocket socket, BluetoothDevice
if (D) Log.d(TAG, "connected"); device, final String socketType) {
if (D) Log.d(TAG, "connected, Socket Type:" + socketType);
// Cancel the thread that completed the connection // Cancel the thread that completed the connection
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;} if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
@@ -147,10 +159,17 @@ public class BluetoothChatService {
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;} if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// Cancel the accept thread because we only want to connect to one device // Cancel the accept thread because we only want to connect to one device
if (mAcceptThread != null) {mAcceptThread.cancel(); mAcceptThread = null;} if (mSecureAcceptThread != null) {
mSecureAcceptThread.cancel();
mSecureAcceptThread = null;
}
if (mInsecureAcceptThread != null) {
mInsecureAcceptThread.cancel();
mInsecureAcceptThread = null;
}
// Start the thread to manage the connection and perform transmissions // Start the thread to manage the connection and perform transmissions
mConnectedThread = new ConnectedThread(socket); mConnectedThread = new ConnectedThread(socket, socketType);
mConnectedThread.start(); mConnectedThread.start();
// Send the name of the connected device back to the UI Activity // Send the name of the connected device back to the UI Activity
@@ -168,9 +187,26 @@ public class BluetoothChatService {
*/ */
public synchronized void stop() { public synchronized void stop() {
if (D) Log.d(TAG, "stop"); if (D) Log.d(TAG, "stop");
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;} if (mConnectThread != null) {
if (mAcceptThread != null) {mAcceptThread.cancel(); mAcceptThread = null;} mConnectThread.cancel();
mConnectThread = null;
}
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
if (mSecureAcceptThread != null) {
mSecureAcceptThread.cancel();
mSecureAcceptThread = null;
}
if (mInsecureAcceptThread != null) {
mInsecureAcceptThread.cancel();
mInsecureAcceptThread = null;
}
setState(STATE_NONE); setState(STATE_NONE);
} }
@@ -195,28 +231,30 @@ public class BluetoothChatService {
* Indicate that the connection attempt failed and notify the UI Activity. * Indicate that the connection attempt failed and notify the UI Activity.
*/ */
private void connectionFailed() { private void connectionFailed() {
setState(STATE_LISTEN);
// Send a failure message back to the Activity // Send a failure message back to the Activity
Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST); Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST);
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(BluetoothChat.TOAST, "Unable to connect device"); bundle.putString(BluetoothChat.TOAST, "Unable to connect device");
msg.setData(bundle); msg.setData(bundle);
mHandler.sendMessage(msg); mHandler.sendMessage(msg);
// Start the service over to restart listening mode
BluetoothChatService.this.start();
} }
/** /**
* Indicate that the connection was lost and notify the UI Activity. * Indicate that the connection was lost and notify the UI Activity.
*/ */
private void connectionLost() { private void connectionLost() {
setState(STATE_LISTEN);
// Send a failure message back to the Activity // Send a failure message back to the Activity
Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST); Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST);
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(BluetoothChat.TOAST, "Device connection was lost"); bundle.putString(BluetoothChat.TOAST, "Device connection was lost");
msg.setData(bundle); msg.setData(bundle);
mHandler.sendMessage(msg); mHandler.sendMessage(msg);
// Start the service over to restart listening mode
BluetoothChatService.this.start();
} }
/** /**
@@ -227,22 +265,32 @@ public class BluetoothChatService {
private class AcceptThread extends Thread { private class AcceptThread extends Thread {
// The local server socket // The local server socket
private final BluetoothServerSocket mmServerSocket; private final BluetoothServerSocket mmServerSocket;
private String mSocketType;
public AcceptThread() { public AcceptThread(boolean secure) {
BluetoothServerSocket tmp = null; BluetoothServerSocket tmp = null;
mSocketType = secure ? "Secure":"Insecure";
// Create a new listening server socket // Create a new listening server socket
try { try {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID); if (secure) {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,
MY_UUID_SECURE);
} else {
tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(
NAME_INSECURE, MY_UUID_INSECURE);
}
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "listen() failed", e); Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
} }
mmServerSocket = tmp; mmServerSocket = tmp;
} }
public void run() { public void run() {
if (D) Log.d(TAG, "BEGIN mAcceptThread" + this); if (D) Log.d(TAG, "Socket Type: " + mSocketType +
setName("AcceptThread"); "BEGIN mAcceptThread" + this);
setName("AcceptThread" + mSocketType);
BluetoothSocket socket = null; BluetoothSocket socket = null;
// Listen to the server socket if we're not connected // Listen to the server socket if we're not connected
@@ -252,7 +300,7 @@ public class BluetoothChatService {
// successful connection or an exception // successful connection or an exception
socket = mmServerSocket.accept(); socket = mmServerSocket.accept();
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "accept() failed", e); Log.e(TAG, "Socket Type: " + mSocketType + "accept() failed", e);
break; break;
} }
@@ -263,7 +311,8 @@ public class BluetoothChatService {
case STATE_LISTEN: case STATE_LISTEN:
case STATE_CONNECTING: case STATE_CONNECTING:
// Situation normal. Start the connected thread. // Situation normal. Start the connected thread.
connected(socket, socket.getRemoteDevice()); connected(socket, socket.getRemoteDevice(),
mSocketType);
break; break;
case STATE_NONE: case STATE_NONE:
case STATE_CONNECTED: case STATE_CONNECTED:
@@ -278,15 +327,16 @@ public class BluetoothChatService {
} }
} }
} }
if (D) Log.i(TAG, "END mAcceptThread"); if (D) Log.i(TAG, "END mAcceptThread, socket Type: " + mSocketType);
} }
public void cancel() { public void cancel() {
if (D) Log.d(TAG, "cancel " + this); if (D) Log.d(TAG, "Socket Type" + mSocketType + "cancel " + this);
try { try {
mmServerSocket.close(); mmServerSocket.close();
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "close() of server failed", e); Log.e(TAG, "Socket Type" + mSocketType + "close() of server failed", e);
} }
} }
} }
@@ -300,24 +350,32 @@ public class BluetoothChatService {
private class ConnectThread extends Thread { private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket; private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice; private final BluetoothDevice mmDevice;
private String mSocketType;
public ConnectThread(BluetoothDevice device) { public ConnectThread(BluetoothDevice device, boolean secure) {
mmDevice = device; mmDevice = device;
BluetoothSocket tmp = null; BluetoothSocket tmp = null;
mSocketType = secure ? "Secure" : "Insecure";
// Get a BluetoothSocket for a connection with the // Get a BluetoothSocket for a connection with the
// given BluetoothDevice // given BluetoothDevice
try { try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID); if (secure) {
tmp = device.createRfcommSocketToServiceRecord(
MY_UUID_SECURE);
} else {
tmp = device.createInsecureRfcommSocketToServiceRecord(
MY_UUID_INSECURE);
}
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "create() failed", e); Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
} }
mmSocket = tmp; mmSocket = tmp;
} }
public void run() { public void run() {
Log.i(TAG, "BEGIN mConnectThread"); Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
setName("ConnectThread"); setName("ConnectThread" + mSocketType);
// Always cancel discovery because it will slow down a connection // Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery(); mAdapter.cancelDiscovery();
@@ -328,15 +386,14 @@ public class BluetoothChatService {
// successful connection or an exception // successful connection or an exception
mmSocket.connect(); mmSocket.connect();
} catch (IOException e) { } catch (IOException e) {
connectionFailed();
// Close the socket // Close the socket
try { try {
mmSocket.close(); mmSocket.close();
} catch (IOException e2) { } catch (IOException e2) {
Log.e(TAG, "unable to close() socket during connection failure", e2); Log.e(TAG, "unable to close() " + mSocketType +
" socket during connection failure", e2);
} }
// Start the service over to restart listening mode connectionFailed();
BluetoothChatService.this.start();
return; return;
} }
@@ -346,14 +403,14 @@ public class BluetoothChatService {
} }
// Start the connected thread // Start the connected thread
connected(mmSocket, mmDevice); connected(mmSocket, mmDevice, mSocketType);
} }
public void cancel() { public void cancel() {
try { try {
mmSocket.close(); mmSocket.close();
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e); Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
} }
} }
} }
@@ -367,8 +424,8 @@ public class BluetoothChatService {
private final InputStream mmInStream; private final InputStream mmInStream;
private final OutputStream mmOutStream; private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) { public ConnectedThread(BluetoothSocket socket, String socketType) {
Log.d(TAG, "create ConnectedThread"); Log.d(TAG, "create ConnectedThread: " + socketType);
mmSocket = socket; mmSocket = socket;
InputStream tmpIn = null; InputStream tmpIn = null;
OutputStream tmpOut = null; OutputStream tmpOut = null;