diff options
-rwxr-xr-x | AndroidManifest.xml | 7 | ||||
-rwxr-xr-x | res/values/strings.xml | 3 | ||||
-rw-r--r-- | src/com/android/nfc/handover/BluetoothHeadsetHandover.java | 37 | ||||
-rw-r--r-- | src/com/android/nfc/handover/ConfirmConnectActivity.java | 48 |
4 files changed, 90 insertions, 5 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index c200e2c..3d65a2a 100755 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -42,5 +42,12 @@ android:excludeFromRecents="true" android:noHistory="true" /> + <activity android:name=".handover.ConfirmConnectActivity" + android:finishOnCloseSystemDialogs="true" + android:excludeFromRecents="true" + android:theme="@android:style/Theme.Translucent.NoTitleBar" + android:noHistory="true" + + /> </application> </manifest> diff --git a/res/values/strings.xml b/res/values/strings.xml index 21cfab8..8d25943 100755 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -32,4 +32,7 @@ <string name="pairing_headset">Pairing</string> <string name="pairing_headset_failed">Failed to pair</string> <string name="failed_to_enable_bt">Failed to enable Bluetooth</string> + <string name="confirm_pairing">Are you sure you want to pair the Bluetooth device <xliff:g id="device_name">%1$s</xliff:g>?</string> + <string name="pair_yes">Yes</string> + <string name="pair_no">No</string> </resources> diff --git a/src/com/android/nfc/handover/BluetoothHeadsetHandover.java b/src/com/android/nfc/handover/BluetoothHeadsetHandover.java index 7cb0424..7974dfa 100644 --- a/src/com/android/nfc/handover/BluetoothHeadsetHandover.java +++ b/src/com/android/nfc/handover/BluetoothHeadsetHandover.java @@ -50,14 +50,18 @@ public class BluetoothHeadsetHandover { static final String TAG = HandoverManager.TAG; static final boolean DBG = HandoverManager.DBG; + static final String ACTION_ALLOW_CONNECT = "com.android.nfc.handover.action.ALLOW_CONNECT"; + static final String ACTION_DENY_CONNECT = "com.android.nfc.handover.action.DENY_CONNECT"; + static final int TIMEOUT_MS = 20000; static final int STATE_INIT = 0; static final int STATE_TURNING_ON = 1; - static final int STATE_BONDING = 2; - static final int STATE_CONNECTING = 3; - static final int STATE_DISCONNECTING = 4; - static final int STATE_COMPLETE = 5; + static final int STATE_WAITING_FOR_BOND_CONFIRMATION = 2; + static final int STATE_BONDING = 3; + static final int STATE_CONNECTING = 4; + static final int STATE_DISCONNECTING = 5; + static final int STATE_COMPLETE = 6; static final int RESULT_PENDING = 0; static final int RESULT_CONNECTED = 1; @@ -113,6 +117,9 @@ public class BluetoothHeadsetHandover { filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); + filter.addAction(ACTION_ALLOW_CONNECT); + filter.addAction(ACTION_DENY_CONNECT); + mContext.registerReceiver(mReceiver, filter); if (mA2dp.getConnectedDevices().contains(mDevice) || @@ -189,6 +196,13 @@ public class BluetoothHeadsetHandover { // fall-through case STATE_TURNING_ON: if (mDevice.getBondState() != BluetoothDevice.BOND_BONDED) { + requestPairConfirmation(); + mState = STATE_WAITING_FOR_BOND_CONFIRMATION; + break; + } + // fall-through + case STATE_WAITING_FOR_BOND_CONFIRMATION: + if (mDevice.getBondState() != BluetoothDevice.BOND_BONDED) { startBonding(); break; } @@ -258,7 +272,11 @@ public class BluetoothHeadsetHandover { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (!mDevice.equals(device)) return; - if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action) && mState == STATE_BONDING) { + if (ACTION_ALLOW_CONNECT.equals(action)) { + nextStepConnect(); + } else if (ACTION_DENY_CONNECT.equals(action)) { + complete(false); + } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action) && mState == STATE_BONDING) { int bond = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothAdapter.ERROR); if (bond == BluetoothDevice.BOND_BONDED) { @@ -312,6 +330,15 @@ public class BluetoothHeadsetHandover { mContext.sendOrderedBroadcast(intent, null); } + void requestPairConfirmation() { + Intent dialogIntent = new Intent(mContext, ConfirmConnectActivity.class); + dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + dialogIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); + + mContext.startActivity(dialogIntent); + } + final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { diff --git a/src/com/android/nfc/handover/ConfirmConnectActivity.java b/src/com/android/nfc/handover/ConfirmConnectActivity.java new file mode 100644 index 0000000..22d518f --- /dev/null +++ b/src/com/android/nfc/handover/ConfirmConnectActivity.java @@ -0,0 +1,48 @@ +package com.android.nfc.handover; + +import android.app.Activity; +import android.app.AlertDialog; +import android.bluetooth.BluetoothDevice; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.res.Resources; +import android.os.Bundle; + +import com.android.nfc.R; + +public class ConfirmConnectActivity extends Activity { + BluetoothDevice mDevice; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_DARK); + Intent launchIntent = getIntent(); + mDevice = launchIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + if (mDevice == null) finish(); + Resources res = getResources(); + String deviceName = mDevice.getName() != null ? mDevice.getName() : ""; + String confirmString = String.format(res.getString(R.string.confirm_pairing), deviceName); + builder.setMessage(confirmString) + .setCancelable(false) + .setPositiveButton(res.getString(R.string.pair_yes), + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + Intent allowIntent = new Intent(BluetoothHeadsetHandover.ACTION_ALLOW_CONNECT); + allowIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); + sendBroadcast(allowIntent); + ConfirmConnectActivity.this.finish(); + } + }) + .setNegativeButton(res.getString(R.string.pair_no), + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + Intent denyIntent = new Intent(BluetoothHeadsetHandover.ACTION_DENY_CONNECT); + denyIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); + sendBroadcast(denyIntent); + ConfirmConnectActivity.this.finish(); + } + }); + AlertDialog alert = builder.create(); + alert.show(); + } +} |