您无法使用第三方(非内核/非系统)应用程序在4.2及更高版本中设置Android设备的扫描模式。您只能启用发现(SCAN_MODE_CONNECTABLE_DISCOVERABLE),并选择使用EXTRA_DISCOVERABLE_DURATION来设置特定的持续时间。您可以通过设置duration parameter to 1来有效取消发现。
证据,采取一目了然通过Android Source,重点BluetoothAdapter.java:
/**
* 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, 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},
* {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}.
*
If Bluetooth state is not {@link #STATE_ON}, this API
* will return false. After turning on Bluetooth,
* wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
* to get the updated value.
*
Requires {@link android.Manifest.permission#WRITE_SECURE_SETTINGS}
*
Applications cannot set the scan mode. They should use
* startActivityForResult(
* BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE})
* 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, int duration) {
if (getState() != STATE_ON) return false;
try {
synchronized(mManagerCallback) {
if (mService != null) return mService.setScanMode(mode, duration);
}
} catch (RemoteException e) {Log.e(TAG, "", e);}
return false;
}