• R/O
  • HTTP
  • SSH
  • HTTPS

提交

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

development


Commit MetaInfo

修订版6314c20eebbea8eadf6bec36624dbc366da9b2dc (tree)
时间2011-01-15 06:11:34
作者Jaikumar Ganesh <jaikumar@goog...>
CommiterJaikumar Ganesh

Log Message

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

Also fix some bugs.
Bug: 3352266

Change-Id: Ie64cb243159b1f6eea5f268f02a1dd58970d1e22

更改概述

差异

--- a/samples/BluetoothChat/res/menu/option_menu.xml
+++ b/samples/BluetoothChat/res/menu/option_menu.xml
@@ -14,9 +14,12 @@
1414 limitations under the License.
1515 -->
1616 <menu xmlns:android="http://schemas.android.com/apk/res/android">
17- <item android:id="@+id/scan"
17+ <item android:id="@+id/secure_connect_scan"
1818 android:icon="@android:drawable/ic_menu_search"
19- android:title="@string/connect" />
19+ android:title="@string/secure_connect" />
20+ <item android:id="@+id/insecure_connect_scan"
21+ android:icon="@android:drawable/ic_menu_search"
22+ android:title="@string/insecure_connect" />
2023 <item android:id="@+id/discoverable"
2124 android:icon="@android:drawable/ic_menu_mylocation"
2225 android:title="@string/discoverable" />
--- a/samples/BluetoothChat/res/values/strings.xml
+++ b/samples/BluetoothChat/res/values/strings.xml
@@ -35,6 +35,7 @@
3535 <string name="button_scan">Scan for devices</string>
3636
3737 <!-- Options Menu -->
38- <string name="connect">Connect a device</string>
38+ <string name="secure_connect">Connect a device - Secure</string>
39+ <string name="insecure_connect">Connect a device - Insecure</string>
3940 <string name="discoverable">Make discoverable</string>
4041 </resources>
--- a/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChat.java
+++ b/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChat.java
@@ -59,8 +59,9 @@ public class BluetoothChat extends Activity {
5959 public static final String TOAST = "toast";
6060
6161 // Intent request codes
62- private static final int REQUEST_CONNECT_DEVICE = 1;
63- private static final int REQUEST_ENABLE_BT = 2;
62+ private static final int REQUEST_CONNECT_DEVICE_SECURE = 1;
63+ private static final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
64+ private static final int REQUEST_ENABLE_BT = 3;
6465
6566 // Layout Views
6667 private TextView mTitle;
@@ -287,16 +288,16 @@ public class BluetoothChat extends Activity {
287288 public void onActivityResult(int requestCode, int resultCode, Intent data) {
288289 if(D) Log.d(TAG, "onActivityResult " + resultCode);
289290 switch (requestCode) {
290- case REQUEST_CONNECT_DEVICE:
291+ case REQUEST_CONNECT_DEVICE_SECURE:
291292 // When DeviceListActivity returns with a device to connect
292293 if (resultCode == Activity.RESULT_OK) {
293- // Get the device MAC address
294- String address = data.getExtras()
295- .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
296- // Get the BLuetoothDevice object
297- BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
298- // Attempt to connect to the device
299- mChatService.connect(device);
294+ connectDevice(data, true);
295+ }
296+ break;
297+ case REQUEST_CONNECT_DEVICE_INSECURE:
298+ // When DeviceListActivity returns with a device to connect
299+ if (resultCode == Activity.RESULT_OK) {
300+ connectDevice(data, false);
300301 }
301302 break;
302303 case REQUEST_ENABLE_BT:
@@ -313,6 +314,16 @@ public class BluetoothChat extends Activity {
313314 }
314315 }
315316
317+ private void connectDevice(Intent data, boolean secure) {
318+ // Get the device MAC address
319+ String address = data.getExtras()
320+ .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
321+ // Get the BLuetoothDevice object
322+ BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
323+ // Attempt to connect to the device
324+ mChatService.connect(device, secure);
325+ }
326+
316327 @Override
317328 public boolean onCreateOptionsMenu(Menu menu) {
318329 MenuInflater inflater = getMenuInflater();
@@ -322,11 +333,17 @@ public class BluetoothChat extends Activity {
322333
323334 @Override
324335 public boolean onOptionsItemSelected(MenuItem item) {
336+ Intent serverIntent = null;
325337 switch (item.getItemId()) {
326- case R.id.scan:
338+ case R.id.secure_connect_scan:
339+ // Launch the DeviceListActivity to see devices and do scan
340+ serverIntent = new Intent(this, DeviceListActivity.class);
341+ startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_SECURE);
342+ return true;
343+ case R.id.insecure_connect_scan:
327344 // Launch the DeviceListActivity to see devices and do scan
328- Intent serverIntent = new Intent(this, DeviceListActivity.class);
329- startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
345+ serverIntent = new Intent(this, DeviceListActivity.class);
346+ startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE);
330347 return true;
331348 case R.id.discoverable:
332349 // Ensure this device is discoverable by others
@@ -336,4 +353,4 @@ public class BluetoothChat extends Activity {
336353 return false;
337354 }
338355
339-}
\ No newline at end of file
356+}
--- a/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java
+++ b/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java
@@ -43,15 +43,20 @@ public class BluetoothChatService {
4343 private static final boolean D = true;
4444
4545 // Name for the SDP record when creating server socket
46- private static final String NAME = "BluetoothChat";
46+ private static final String NAME_SECURE = "BluetoothChatSecure";
47+ private static final String NAME_INSECURE = "BluetoothChatInsecure";
4748
4849 // Unique UUID for this application
49- private static final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
50+ private static final UUID MY_UUID_SECURE =
51+ UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
52+ private static final UUID MY_UUID_INSECURE =
53+ UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
5054
5155 // Member fields
5256 private final BluetoothAdapter mAdapter;
5357 private final Handler mHandler;
54- private AcceptThread mAcceptThread;
58+ private AcceptThread mSecureAcceptThread;
59+ private AcceptThread mInsecureAcceptThread;
5560 private ConnectThread mConnectThread;
5661 private ConnectedThread mConnectedThread;
5762 private int mState;
@@ -103,19 +108,25 @@ public class BluetoothChatService {
103108 // Cancel any thread currently running a connection
104109 if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
105110
111+ setState(STATE_LISTEN);
112+
106113 // Start the thread to listen on a BluetoothServerSocket
107- if (mAcceptThread == null) {
108- mAcceptThread = new AcceptThread();
109- mAcceptThread.start();
114+ if (mSecureAcceptThread == null) {
115+ mSecureAcceptThread = new AcceptThread(true);
116+ mSecureAcceptThread.start();
117+ }
118+ if (mInsecureAcceptThread == null) {
119+ mInsecureAcceptThread = new AcceptThread(false);
120+ mInsecureAcceptThread.start();
110121 }
111- setState(STATE_LISTEN);
112122 }
113123
114124 /**
115125 * Start the ConnectThread to initiate a connection to a remote device.
116126 * @param device The BluetoothDevice to connect
127+ * @param secure Socket Security type - Secure (true) , Insecure (false)
117128 */
118- public synchronized void connect(BluetoothDevice device) {
129+ public synchronized void connect(BluetoothDevice device, boolean secure) {
119130 if (D) Log.d(TAG, "connect to: " + device);
120131
121132 // Cancel any thread attempting to make a connection
@@ -127,7 +138,7 @@ public class BluetoothChatService {
127138 if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
128139
129140 // Start the thread to connect with the given device
130- mConnectThread = new ConnectThread(device);
141+ mConnectThread = new ConnectThread(device, secure);
131142 mConnectThread.start();
132143 setState(STATE_CONNECTING);
133144 }
@@ -137,8 +148,9 @@ public class BluetoothChatService {
137148 * @param socket The BluetoothSocket on which the connection was made
138149 * @param device The BluetoothDevice that has been connected
139150 */
140- public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
141- if (D) Log.d(TAG, "connected");
151+ public synchronized void connected(BluetoothSocket socket, BluetoothDevice
152+ device, final String socketType) {
153+ if (D) Log.d(TAG, "connected, Socket Type:" + socketType);
142154
143155 // Cancel the thread that completed the connection
144156 if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
@@ -147,10 +159,17 @@ public class BluetoothChatService {
147159 if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
148160
149161 // Cancel the accept thread because we only want to connect to one device
150- if (mAcceptThread != null) {mAcceptThread.cancel(); mAcceptThread = null;}
162+ if (mSecureAcceptThread != null) {
163+ mSecureAcceptThread.cancel();
164+ mSecureAcceptThread = null;
165+ }
166+ if (mInsecureAcceptThread != null) {
167+ mInsecureAcceptThread.cancel();
168+ mInsecureAcceptThread = null;
169+ }
151170
152171 // Start the thread to manage the connection and perform transmissions
153- mConnectedThread = new ConnectedThread(socket);
172+ mConnectedThread = new ConnectedThread(socket, socketType);
154173 mConnectedThread.start();
155174
156175 // Send the name of the connected device back to the UI Activity
@@ -168,9 +187,26 @@ public class BluetoothChatService {
168187 */
169188 public synchronized void stop() {
170189 if (D) Log.d(TAG, "stop");
171- if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
172- if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
173- if (mAcceptThread != null) {mAcceptThread.cancel(); mAcceptThread = null;}
190+
191+ if (mConnectThread != null) {
192+ mConnectThread.cancel();
193+ mConnectThread = null;
194+ }
195+
196+ if (mConnectedThread != null) {
197+ mConnectedThread.cancel();
198+ mConnectedThread = null;
199+ }
200+
201+ if (mSecureAcceptThread != null) {
202+ mSecureAcceptThread.cancel();
203+ mSecureAcceptThread = null;
204+ }
205+
206+ if (mInsecureAcceptThread != null) {
207+ mInsecureAcceptThread.cancel();
208+ mInsecureAcceptThread = null;
209+ }
174210 setState(STATE_NONE);
175211 }
176212
@@ -195,28 +231,30 @@ public class BluetoothChatService {
195231 * Indicate that the connection attempt failed and notify the UI Activity.
196232 */
197233 private void connectionFailed() {
198- setState(STATE_LISTEN);
199-
200234 // Send a failure message back to the Activity
201235 Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST);
202236 Bundle bundle = new Bundle();
203237 bundle.putString(BluetoothChat.TOAST, "Unable to connect device");
204238 msg.setData(bundle);
205239 mHandler.sendMessage(msg);
240+
241+ // Start the service over to restart listening mode
242+ BluetoothChatService.this.start();
206243 }
207244
208245 /**
209246 * Indicate that the connection was lost and notify the UI Activity.
210247 */
211248 private void connectionLost() {
212- setState(STATE_LISTEN);
213-
214249 // Send a failure message back to the Activity
215250 Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST);
216251 Bundle bundle = new Bundle();
217252 bundle.putString(BluetoothChat.TOAST, "Device connection was lost");
218253 msg.setData(bundle);
219254 mHandler.sendMessage(msg);
255+
256+ // Start the service over to restart listening mode
257+ BluetoothChatService.this.start();
220258 }
221259
222260 /**
@@ -227,22 +265,32 @@ public class BluetoothChatService {
227265 private class AcceptThread extends Thread {
228266 // The local server socket
229267 private final BluetoothServerSocket mmServerSocket;
268+ private String mSocketType;
230269
231- public AcceptThread() {
270+ public AcceptThread(boolean secure) {
232271 BluetoothServerSocket tmp = null;
272+ mSocketType = secure ? "Secure":"Insecure";
233273
234274 // Create a new listening server socket
235275 try {
236- tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
276+ if (secure) {
277+ tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,
278+ MY_UUID_SECURE);
279+ } else {
280+ tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(
281+ NAME_INSECURE, MY_UUID_INSECURE);
282+ }
237283 } catch (IOException e) {
238- Log.e(TAG, "listen() failed", e);
284+ Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
239285 }
240286 mmServerSocket = tmp;
241287 }
242288
243289 public void run() {
244- if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);
245- setName("AcceptThread");
290+ if (D) Log.d(TAG, "Socket Type: " + mSocketType +
291+ "BEGIN mAcceptThread" + this);
292+ setName("AcceptThread" + mSocketType);
293+
246294 BluetoothSocket socket = null;
247295
248296 // Listen to the server socket if we're not connected
@@ -252,7 +300,7 @@ public class BluetoothChatService {
252300 // successful connection or an exception
253301 socket = mmServerSocket.accept();
254302 } catch (IOException e) {
255- Log.e(TAG, "accept() failed", e);
303+ Log.e(TAG, "Socket Type: " + mSocketType + "accept() failed", e);
256304 break;
257305 }
258306
@@ -263,7 +311,8 @@ public class BluetoothChatService {
263311 case STATE_LISTEN:
264312 case STATE_CONNECTING:
265313 // Situation normal. Start the connected thread.
266- connected(socket, socket.getRemoteDevice());
314+ connected(socket, socket.getRemoteDevice(),
315+ mSocketType);
267316 break;
268317 case STATE_NONE:
269318 case STATE_CONNECTED:
@@ -278,15 +327,16 @@ public class BluetoothChatService {
278327 }
279328 }
280329 }
281- if (D) Log.i(TAG, "END mAcceptThread");
330+ if (D) Log.i(TAG, "END mAcceptThread, socket Type: " + mSocketType);
331+
282332 }
283333
284334 public void cancel() {
285- if (D) Log.d(TAG, "cancel " + this);
335+ if (D) Log.d(TAG, "Socket Type" + mSocketType + "cancel " + this);
286336 try {
287337 mmServerSocket.close();
288338 } catch (IOException e) {
289- Log.e(TAG, "close() of server failed", e);
339+ Log.e(TAG, "Socket Type" + mSocketType + "close() of server failed", e);
290340 }
291341 }
292342 }
@@ -300,24 +350,32 @@ public class BluetoothChatService {
300350 private class ConnectThread extends Thread {
301351 private final BluetoothSocket mmSocket;
302352 private final BluetoothDevice mmDevice;
353+ private String mSocketType;
303354
304- public ConnectThread(BluetoothDevice device) {
355+ public ConnectThread(BluetoothDevice device, boolean secure) {
305356 mmDevice = device;
306357 BluetoothSocket tmp = null;
358+ mSocketType = secure ? "Secure" : "Insecure";
307359
308360 // Get a BluetoothSocket for a connection with the
309361 // given BluetoothDevice
310362 try {
311- tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
363+ if (secure) {
364+ tmp = device.createRfcommSocketToServiceRecord(
365+ MY_UUID_SECURE);
366+ } else {
367+ tmp = device.createInsecureRfcommSocketToServiceRecord(
368+ MY_UUID_INSECURE);
369+ }
312370 } catch (IOException e) {
313- Log.e(TAG, "create() failed", e);
371+ Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
314372 }
315373 mmSocket = tmp;
316374 }
317375
318376 public void run() {
319- Log.i(TAG, "BEGIN mConnectThread");
320- setName("ConnectThread");
377+ Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
378+ setName("ConnectThread" + mSocketType);
321379
322380 // Always cancel discovery because it will slow down a connection
323381 mAdapter.cancelDiscovery();
@@ -328,15 +386,14 @@ public class BluetoothChatService {
328386 // successful connection or an exception
329387 mmSocket.connect();
330388 } catch (IOException e) {
331- connectionFailed();
332389 // Close the socket
333390 try {
334391 mmSocket.close();
335392 } catch (IOException e2) {
336- Log.e(TAG, "unable to close() socket during connection failure", e2);
393+ Log.e(TAG, "unable to close() " + mSocketType +
394+ " socket during connection failure", e2);
337395 }
338- // Start the service over to restart listening mode
339- BluetoothChatService.this.start();
396+ connectionFailed();
340397 return;
341398 }
342399
@@ -346,14 +403,14 @@ public class BluetoothChatService {
346403 }
347404
348405 // Start the connected thread
349- connected(mmSocket, mmDevice);
406+ connected(mmSocket, mmDevice, mSocketType);
350407 }
351408
352409 public void cancel() {
353410 try {
354411 mmSocket.close();
355412 } catch (IOException e) {
356- Log.e(TAG, "close() of connect socket failed", e);
413+ Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
357414 }
358415 }
359416 }
@@ -367,8 +424,8 @@ public class BluetoothChatService {
367424 private final InputStream mmInStream;
368425 private final OutputStream mmOutStream;
369426
370- public ConnectedThread(BluetoothSocket socket) {
371- Log.d(TAG, "create ConnectedThread");
427+ public ConnectedThread(BluetoothSocket socket, String socketType) {
428+ Log.d(TAG, "create ConnectedThread: " + socketType);
372429 mmSocket = socket;
373430 InputStream tmpIn = null;
374431 OutputStream tmpOut = null;