1e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan/* 2e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * Copyright (C) 2009 The Android Open Source Project 3e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * 4e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * Licensed under the Apache License, Version 2.0 (the "License"); 5e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * you may not use this file except in compliance with the License. 6e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * You may obtain a copy of the License at 7e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * 8e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * http://www.apache.org/licenses/LICENSE-2.0 9e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * 10e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * Unless required by applicable law or agreed to in writing, software 11e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * distributed under the License is distributed on an "AS IS" BASIS, 12e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * See the License for the specific language governing permissions and 14e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * limitations under the License. 15e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan */ 16e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 17e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanpackage com.android.settings.bluetooth; 18e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 19834e5993e4f2f34d5aceb3196601b30231d00b07Michael Chanimport com.android.settings.R; 20834e5993e4f2f34d5aceb3196601b30231d00b07Michael Chan 21834e5993e4f2f34d5aceb3196601b30231d00b07Michael Chanimport android.app.Activity; 22a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chanimport android.app.AlertDialog; 23e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.bluetooth.BluetoothAdapter; 24e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.bluetooth.BluetoothDevice; 25e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.content.BroadcastReceiver; 26e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.content.Context; 27e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.content.DialogInterface; 28e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.content.Intent; 29e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.content.IntentFilter; 30e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.os.Bundle; 31e1089ad33896f4fe5b75e773813367d700773b6fMichael Chanimport android.util.Log; 32e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 33e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan/** 34e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * RequestPermissionActivity asks the user whether to enable discovery. This is 35e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * usually started by an application wanted to start bluetooth and or discovery 36e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan */ 37a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chanpublic class RequestPermissionActivity extends Activity implements 38e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan DialogInterface.OnClickListener { 39e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // Command line to test this 40e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // adb shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE 41e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // adb shell am start -a android.bluetooth.adapter.action.REQUEST_DISCOVERABLE 42e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 43e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan private static final String TAG = "RequestPermissionActivity"; 44e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 45d955af8461b845a1ce4f9beba9decfc2cffb0966Jaikumar Ganesh private static final int MAX_DISCOVERABLE_TIMEOUT = 3600; // 1 hr 46e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 47e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // Non-error return code: BT is starting or has started successfully. Used 48e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // by this Activity and RequestPermissionHelperActivity 49e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan /* package */ static final int RESULT_BT_STARTING_OR_STARTED = -1000; 50e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 51e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan private static final int REQUEST_CODE_START_BT = 1; 52e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 53436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby private LocalBluetoothAdapter mLocalAdapter; 54e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 55e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan private int mTimeout = BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT; 56e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 57e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan /* 58e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * True if bluetooth wasn't enabled and RequestPermissionHelperActivity was 59e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * started to ask the user and start bt. 60e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * 61e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * If/when that activity returns successfully, display please wait msg then 62e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * go away when bt has started and discovery mode has been enabled. 63e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan */ 64e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan private boolean mNeededToEnableBluetooth; 65e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 66e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // True if requesting BT to be turned on 67e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // False if requesting BT to be turned on + discoverable mode 68436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby private boolean mEnableOnly; 69e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 70436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby private boolean mUserConfirmed; 71e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 72436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby private AlertDialog mDialog; 73a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan 7439ef225e7c44a48aa9cfdf5c56ecd4ddfb95ae89Jake Hamby private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 75e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 76e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan @Override 77e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan public void onReceive(Context context, Intent intent) { 78436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby if (intent == null) { 79e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return; 80436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby } 81e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (mNeededToEnableBluetooth 82e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan && BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) { 83e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR); 84e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (state == BluetoothAdapter.STATE_ON) { 85e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (mUserConfirmed) { 86a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan proceedAndFinish(); 87e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 88e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 89e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 90e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 91e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan }; 92e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 93e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan @Override 94e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan protected void onCreate(Bundle savedInstanceState) { 95e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan super.onCreate(savedInstanceState); 96e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 97436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby // Note: initializes mLocalAdapter and returns true on error 98e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (parseIntent()) { 99e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan finish(); 100e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return; 101e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 102e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 103436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby int btState = mLocalAdapter.getState(); 104e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 105e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan switch (btState) { 106e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan case BluetoothAdapter.STATE_OFF: 107e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan case BluetoothAdapter.STATE_TURNING_OFF: 108e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan case BluetoothAdapter.STATE_TURNING_ON: 109e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan /* 110e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * Strictly speaking STATE_TURNING_ON belong with STATE_ON; 111e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * however, BT may not be ready when the user clicks yes and we 112e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * would fail to turn on discovery mode. By kicking this to the 113e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * RequestPermissionHelperActivity, this class will handle that 114e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * case via the broadcast receiver. 115e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan */ 116e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 117e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan /* 118e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * Start the helper activity to: 119e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * 1) ask the user about enabling bt AND discovery 120e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan * 2) enable BT upon confirmation 121e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan */ 122e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan registerReceiver(mReceiver, 123e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); 124436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby Intent intent = new Intent(); 125436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby intent.setClass(this, RequestPermissionHelperActivity.class); 126e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (mEnableOnly) { 127436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby intent.setAction(RequestPermissionHelperActivity.ACTION_INTERNAL_REQUEST_BT_ON); 128e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else { 129436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby intent.setAction(RequestPermissionHelperActivity. 130e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE); 131436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, mTimeout); 132e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 133436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby startActivityForResult(intent, REQUEST_CODE_START_BT); 134e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan mNeededToEnableBluetooth = true; 135e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan break; 136e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan case BluetoothAdapter.STATE_ON: 137e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (mEnableOnly) { 138e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // Nothing to do. Already enabled. 139a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan proceedAndFinish(); 140e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else { 141e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // Ask the user about enabling discovery mode 142e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan createDialog(); 143e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 144436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby break; 145436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby default: 146436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby Log.e(TAG, "Unknown adapter state: " + btState); 147e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 148e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 149e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 150e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan private void createDialog() { 151a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan AlertDialog.Builder builder = new AlertDialog.Builder(this); 152e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 153e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (mNeededToEnableBluetooth) { 154e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // RequestPermissionHelperActivity has gotten confirmation from user 155e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // to turn on BT 156a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan builder.setMessage(getString(R.string.bluetooth_turning_on)); 157a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan builder.setCancelable(false); 158e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else { 159e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // Ask the user whether to turn on discovery mode or not 1601b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren // For lasting discoverable mode there is a different message 1611b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) { 1621b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren builder.setMessage( 163d955af8461b845a1ce4f9beba9decfc2cffb0966Jaikumar Ganesh getString(R.string.bluetooth_ask_lasting_discovery)); 1641b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren } else { 1651b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren builder.setMessage( 166d955af8461b845a1ce4f9beba9decfc2cffb0966Jaikumar Ganesh getString(R.string.bluetooth_ask_discovery, mTimeout)); 1671b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren } 16895c1003f1c6ea957065fa752d89c9f64b599461cJake Hamby builder.setPositiveButton(getString(R.string.allow), this); 16995c1003f1c6ea957065fa752d89c9f64b599461cJake Hamby builder.setNegativeButton(getString(R.string.deny), this); 170e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 171e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 172a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan mDialog = builder.create(); 173a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan mDialog.show(); 17431c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen 17531c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog) == true) { 17631c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen // dismiss dialog immediately if settings say so 17731c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen onClick(null, DialogInterface.BUTTON_POSITIVE); 17831c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen } 179e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 180e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 181e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan @Override 182e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan protected void onActivityResult(int requestCode, int resultCode, Intent data) { 183e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (requestCode != REQUEST_CODE_START_BT) { 184436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby Log.e(TAG, "Unexpected onActivityResult " + requestCode + ' ' + resultCode); 185436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby setResult(RESULT_CANCELED); 186e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan finish(); 187e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return; 188e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 189e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (resultCode != RESULT_BT_STARTING_OR_STARTED) { 190e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan setResult(resultCode); 191e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan finish(); 192a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan return; 193e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 194e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 195e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // Back from RequestPermissionHelperActivity. User confirmed to enable 196e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // BT and discoverable mode. 197e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan mUserConfirmed = true; 198e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 199436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) { 200a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan proceedAndFinish(); 201e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else { 202e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // If BT is not up yet, show "Turning on Bluetooth..." 203e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan createDialog(); 204e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 205e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 206e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 207e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan public void onClick(DialogInterface dialog, int which) { 208e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan switch (which) { 209e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan case DialogInterface.BUTTON_POSITIVE: 210a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan proceedAndFinish(); 211e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan break; 212e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 213e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan case DialogInterface.BUTTON_NEGATIVE: 214436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby setResult(RESULT_CANCELED); 215a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan finish(); 216e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan break; 217e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 218e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 219e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 220a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan private void proceedAndFinish() { 221e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan int returnCode; 222e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 223e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (mEnableOnly) { 224e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // BT enabled. Done 225436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby returnCode = RESULT_OK; 226436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby } else if (mLocalAdapter.setScanMode( 227e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) { 228e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // If already in discoverable mode, this will extend the timeout. 2294bb010a67f80f5224b0777b48abdb08403b87616Srikanth Uppala long endTime = System.currentTimeMillis() + (long) mTimeout * 1000; 230436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby LocalBluetoothPreferences.persistDiscoverableEndTimestamp( 2314bb010a67f80f5224b0777b48abdb08403b87616Srikanth Uppala this, endTime); 2324bb010a67f80f5224b0777b48abdb08403b87616Srikanth Uppala if (0 < mTimeout) { 2334bb010a67f80f5224b0777b48abdb08403b87616Srikanth Uppala BluetoothDiscoverableTimeoutReceiver.setDiscoverableAlarm(this, endTime); 2344bb010a67f80f5224b0777b48abdb08403b87616Srikanth Uppala } 235e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan returnCode = mTimeout; 236834e5993e4f2f34d5aceb3196601b30231d00b07Michael Chan // Activity.RESULT_FIRST_USER should be 1 237436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby if (returnCode < RESULT_FIRST_USER) { 238436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby returnCode = RESULT_FIRST_USER; 239834e5993e4f2f34d5aceb3196601b30231d00b07Michael Chan } 240e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else { 241436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby returnCode = RESULT_CANCELED; 242e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 243e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 244a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan if (mDialog != null) { 245a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan mDialog.dismiss(); 246e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 247a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan 248a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan setResult(returnCode); 249a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan finish(); 250e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 251e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 252436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby /** 253436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby * Parse the received Intent and initialize mLocalBluetoothAdapter. 254436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby * @return true if an error occurred; false otherwise 255436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby */ 256e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan private boolean parseIntent() { 257e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan Intent intent = getIntent(); 258e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (intent != null && intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_ENABLE)) { 259e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan mEnableOnly = true; 260e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else if (intent != null 261e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan && intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)) { 262e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 263e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT); 264e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 265d955af8461b845a1ce4f9beba9decfc2cffb0966Jaikumar Ganesh Log.d(TAG, "Setting Bluetooth Discoverable Timeout = " + mTimeout); 266e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 267d955af8461b845a1ce4f9beba9decfc2cffb0966Jaikumar Ganesh if (mTimeout < 0 || mTimeout > MAX_DISCOVERABLE_TIMEOUT) { 268e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan mTimeout = BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT; 269e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 270e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else { 271e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan Log.e(TAG, "Error: this activity may be started only with intent " 272e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan + BluetoothAdapter.ACTION_REQUEST_ENABLE + " or " 273e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan + BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 274436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby setResult(RESULT_CANCELED); 275e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return true; 276e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 277e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 278436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby LocalBluetoothManager manager = LocalBluetoothManager.getInstance(this); 279436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby if (manager == null) { 280436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby Log.e(TAG, "Error: there's a problem starting Bluetooth"); 281436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby setResult(RESULT_CANCELED); 282e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return true; 283e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 284436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby mLocalAdapter = manager.getBluetoothAdapter(); 285e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 286e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return false; 287e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 288e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 289e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan @Override 290e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan protected void onDestroy() { 291e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan super.onDestroy(); 292436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby if (mNeededToEnableBluetooth) { 293436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby unregisterReceiver(mReceiver); 294436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby } 295e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 296e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 297e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan @Override 298e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan public void onBackPressed() { 299436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby setResult(RESULT_CANCELED); 300e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan super.onBackPressed(); 301e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 302e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan} 303