diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java index 776c923c0b2e5..3aaed385dfa76 100644 --- a/core/java/android/bluetooth/BluetoothAdapter.java +++ b/core/java/android/bluetooth/BluetoothAdapter.java @@ -412,10 +412,10 @@ public final class BluetoothAdapter { * Set the Bluetooth scan mode of the local Bluetooth adapter. *
The Bluetooth scan mode determines if the local adapter is * connectable and/or discoverable from remote Bluetooth devices. - *
For privacy reasons, it is recommended to limit the duration of time - * that the local adapter remains in a discoverable scan mode. For example, - * 2 minutes is a generous time to allow a remote Bluetooth device to - * initiate and complete its discovery process. + *
For privacy reasons, discoverable mode is automatically turned off
+ * after duration seconds. For example, 120 seconds should be
+ * enough for a remote device to initiate and complete its discovery
+ * process.
*
Valid scan mode values are: * {@link #SCAN_MODE_NONE}, * {@link #SCAN_MODE_CONNECTABLE}, @@ -427,16 +427,23 @@ public final class BluetoothAdapter { * instead. * * @param mode valid scan mode + * @param duration time in seconds to apply scan mode, only used for + * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE} * @return true if the scan mode was set, false otherwise * @hide */ - public boolean setScanMode(int mode) { + public boolean setScanMode(int mode, int duration) { try { - return mService.setScanMode(mode); + return mService.setScanMode(mode, duration); } catch (RemoteException e) {Log.e(TAG, "", e);} return false; } + /** @hide */ + public boolean setScanMode(int mode) { + return setScanMode(mode, 120); + } + /** @hide */ public int getDiscoverableTimeout() { try { diff --git a/core/java/android/bluetooth/IBluetooth.aidl b/core/java/android/bluetooth/IBluetooth.aidl index 203a61d8a1f68..1bc2f968b3838 100644 --- a/core/java/android/bluetooth/IBluetooth.aidl +++ b/core/java/android/bluetooth/IBluetooth.aidl @@ -35,7 +35,7 @@ interface IBluetooth boolean setName(in String name); int getScanMode(); - boolean setScanMode(int mode); + boolean setScanMode(int mode, int duration); int getDiscoverableTimeout(); boolean setDiscoverableTimeout(int timeout); diff --git a/core/java/android/server/BluetoothService.java b/core/java/android/server/BluetoothService.java index de14b5b4e0bb5..8b9ba84cac5bd 100644 --- a/core/java/android/server/BluetoothService.java +++ b/core/java/android/server/BluetoothService.java @@ -78,6 +78,7 @@ public class BluetoothService extends IBluetooth.Stub { private static final int MESSAGE_REGISTER_SDP_RECORDS = 1; private static final int MESSAGE_FINISH_DISABLE = 2; private static final int MESSAGE_UUID_INTENT = 3; + private static final int MESSAGE_DISCOVERABLE_TIMEOUT = 4; // The timeout used to sent the UUIDs Intent // This timeout should be greater than the page timeout @@ -308,6 +309,15 @@ public class BluetoothService extends IBluetooth.Stub { if (address != null) sendUuidIntent(address); break; + case MESSAGE_DISCOVERABLE_TIMEOUT: + int mode = msg.arg1; + if (isEnabled()) { + // TODO: Switch back to the previous scan mode + // This is ok for now, because we only use + // CONNECTABLE and CONNECTABLE_DISCOVERABLE + setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE, -1); + } + break; } } }; @@ -679,23 +689,30 @@ public class BluetoothService extends IBluetooth.Stub { return setPropertyInteger("DiscoverableTimeout", timeout); } - public synchronized boolean setScanMode(int mode) { + public synchronized boolean setScanMode(int mode, int duration) { mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS, "Need WRITE_SECURE_SETTINGS permission"); boolean pairable = false; boolean discoverable = false; + switch (mode) { case BluetoothAdapter.SCAN_MODE_NONE: + mHandler.removeMessages(MESSAGE_DISCOVERABLE_TIMEOUT); pairable = false; discoverable = false; break; case BluetoothAdapter.SCAN_MODE_CONNECTABLE: + mHandler.removeMessages(MESSAGE_DISCOVERABLE_TIMEOUT); pairable = true; discoverable = false; break; case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE: + mHandler.removeMessages(MESSAGE_DISCOVERABLE_TIMEOUT); pairable = true; discoverable = true; + Message msg = mHandler.obtainMessage(MESSAGE_DISCOVERABLE_TIMEOUT); + mHandler.sendMessageDelayed(msg, duration * 1000); + if (DBG) Log.d(TAG, "BT Discoverable for " + duration + " seconds"); break; default: Log.w(TAG, "Requested invalid scan mode " + mode);