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); 152a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan builder.setIcon(android.R.drawable.ic_dialog_info); 153a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan builder.setTitle(getString(R.string.bluetooth_permission_request)); 154e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 155e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (mNeededToEnableBluetooth) { 156e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // RequestPermissionHelperActivity has gotten confirmation from user 157e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // to turn on BT 158a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan builder.setMessage(getString(R.string.bluetooth_turning_on)); 159a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan builder.setCancelable(false); 160e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else { 161e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // Ask the user whether to turn on discovery mode or not 1621b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren // For lasting discoverable mode there is a different message 1631b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) { 1641b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren builder.setMessage( 165d955af8461b845a1ce4f9beba9decfc2cffb0966Jaikumar Ganesh getString(R.string.bluetooth_ask_lasting_discovery)); 1661b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren } else { 1671b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren builder.setMessage( 168d955af8461b845a1ce4f9beba9decfc2cffb0966Jaikumar Ganesh getString(R.string.bluetooth_ask_discovery, mTimeout)); 1691b0489cb3eb051b2cf18aa2cb3110ff904c9ebe8Chris Wren } 170a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan builder.setPositiveButton(getString(R.string.yes), this); 171a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan builder.setNegativeButton(getString(R.string.no), this); 172e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 173e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 174a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan mDialog = builder.create(); 175a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan mDialog.show(); 17631c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen 17731c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog) == true) { 17831c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen // dismiss dialog immediately if settings say so 17931c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen onClick(null, DialogInterface.BUTTON_POSITIVE); 18031c5ec82021271f2e1745a2de37c543e67dcec17Mike J. Chen } 181e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 182e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 183e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan @Override 184e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan protected void onActivityResult(int requestCode, int resultCode, Intent data) { 185e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (requestCode != REQUEST_CODE_START_BT) { 186436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby Log.e(TAG, "Unexpected onActivityResult " + requestCode + ' ' + resultCode); 187436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby setResult(RESULT_CANCELED); 188e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan finish(); 189e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return; 190e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 191e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (resultCode != RESULT_BT_STARTING_OR_STARTED) { 192e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan setResult(resultCode); 193e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan finish(); 194a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan return; 195e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 196e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 197e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // Back from RequestPermissionHelperActivity. User confirmed to enable 198e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // BT and discoverable mode. 199e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan mUserConfirmed = true; 200e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 201436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) { 202a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan proceedAndFinish(); 203e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else { 204e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // If BT is not up yet, show "Turning on Bluetooth..." 205e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan createDialog(); 206e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 207e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 208e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 209e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan public void onClick(DialogInterface dialog, int which) { 210e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan switch (which) { 211e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan case DialogInterface.BUTTON_POSITIVE: 212a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan proceedAndFinish(); 213e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan break; 214e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 215e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan case DialogInterface.BUTTON_NEGATIVE: 216436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby setResult(RESULT_CANCELED); 217a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan finish(); 218e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan break; 219e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 220e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 221e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 222a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan private void proceedAndFinish() { 223e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan int returnCode; 224e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 225e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (mEnableOnly) { 226e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // BT enabled. Done 227436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby returnCode = RESULT_OK; 228436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby } else if (mLocalAdapter.setScanMode( 229e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) { 230e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan // If already in discoverable mode, this will extend the timeout. 231436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby LocalBluetoothPreferences.persistDiscoverableEndTimestamp( 232436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby this, System.currentTimeMillis() + (long) mTimeout * 1000); 233e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan returnCode = mTimeout; 234834e5993e4f2f34d5aceb3196601b30231d00b07Michael Chan // Activity.RESULT_FIRST_USER should be 1 235436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby if (returnCode < RESULT_FIRST_USER) { 236436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby returnCode = RESULT_FIRST_USER; 237834e5993e4f2f34d5aceb3196601b30231d00b07Michael Chan } 238e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else { 239436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby returnCode = RESULT_CANCELED; 240e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 241e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 242a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan if (mDialog != null) { 243a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan mDialog.dismiss(); 244e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 245a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan 246a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan setResult(returnCode); 247a0de1dcd1960181d9d8fd2e44f19dead16021e2bMichael Chan finish(); 248e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 249e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 250436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby /** 251436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby * Parse the received Intent and initialize mLocalBluetoothAdapter. 252436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby * @return true if an error occurred; false otherwise 253436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby */ 254e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan private boolean parseIntent() { 255e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan Intent intent = getIntent(); 256e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan if (intent != null && intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_ENABLE)) { 257e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan mEnableOnly = true; 258e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else if (intent != null 259e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan && intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)) { 260e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 261e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT); 262e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 263d955af8461b845a1ce4f9beba9decfc2cffb0966Jaikumar Ganesh Log.d(TAG, "Setting Bluetooth Discoverable Timeout = " + mTimeout); 264e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 265d955af8461b845a1ce4f9beba9decfc2cffb0966Jaikumar Ganesh if (mTimeout < 0 || mTimeout > MAX_DISCOVERABLE_TIMEOUT) { 266e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan mTimeout = BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT; 267e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 268e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } else { 269e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan Log.e(TAG, "Error: this activity may be started only with intent " 270e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan + BluetoothAdapter.ACTION_REQUEST_ENABLE + " or " 271e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan + BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 272436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby setResult(RESULT_CANCELED); 273e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return true; 274e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 275e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 276436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby LocalBluetoothManager manager = LocalBluetoothManager.getInstance(this); 277436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby if (manager == null) { 278436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby Log.e(TAG, "Error: there's a problem starting Bluetooth"); 279436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby setResult(RESULT_CANCELED); 280e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return true; 281e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 282436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby mLocalAdapter = manager.getBluetoothAdapter(); 283e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 284e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan return false; 285e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 286e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 287e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan @Override 288e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan protected void onDestroy() { 289e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan super.onDestroy(); 290436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby if (mNeededToEnableBluetooth) { 291436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby unregisterReceiver(mReceiver); 292436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby } 293e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 294e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan 295e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan @Override 296e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan public void onBackPressed() { 297436b29e68e6608bed9e8e7d54385b8f62d89208eJake Hamby setResult(RESULT_CANCELED); 298e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan super.onBackPressed(); 299e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan } 300e1089ad33896f4fe5b75e773813367d700773b6fMichael Chan} 301