1: public void onActivityResult(int requestCode, int resultCode, Intent data)
2: {
3: if (D)
4: Log.d(TAG, "onActivityResult " + resultCode);
5: switch (requestCode)
6: {
7: case REQUEST_CONNECT_DEVICE_SECURE:
8: // When DeviceListActivity returns with a device to connect
9: // 当用户点击设备列表上的按钮时,触发下面的代码
10: if (resultCode == Activity.RESULT_OK)
11: {
12: connectDevice(data, true);
13: }
14: break;
15: /** Other codes*/
16: }
17: }
18:
19: private void connectDevice(Intent data, boolean secure)
20: {
21: // Get the device MAC address
22: String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
23: // Get the BluetoothDevice object
24: BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
25: // Attempt to connect to the device
26: mChatService.connect(device, secure);
27: }
28:
29: // BluetoothChatService类成员函数
30: public synchronized void connect(BluetoothDevice device, boolean secure)
31: {
32: if (D){Log.d(TAG, "connect to: " + device);}
33:
34: // Cancel any thread attempting to make a connection
35: if (mState == STATE_CONNECTING)
36: {
37: if (mConnectThread != null)
38: {
39: mConnectThread.cancel();
40: mConnectThread = null;
41: }
42: }
43:
44: // Cancel any thread currently running a connection
45: if (mConnectedThread != null)
46: {
47: mConnectedThread.cancel();
48: mConnectedThread = null;
49: }
50:
51: // Start the thread to connect with the given device
52: mConnectThread = new ConnectThread(device, secure);
53: mConnectThread.start();
54: setState(STATE_CONNECTING);
55: }
Recent Comments