目前我正在使用蓝牙应用,我需要点击按钮将扫描模式从SCAN_MODE_CONNECTABLE_DISCOVERABLE
更改为SCAN_MODE_CONNECTABLE
。
我使用以下意图将其设置为可发现:
Intent discoverableIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent .putExtra( BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISOVERABLE_DURATION);
startActivityForResult(discoverableIntent, REQUEST_DISCOVERABLE_BT);
我已设置DISOVERABLE_DURATION=300
;
现在我想跳过其中的Discoverability,并希望仅将其状态更改为SCAN_MODE_CONNECTABLE
。
请给我一个合适的解决方案../
答案 0 :(得分:1)
使用SCAN_MODE_NONE启动新意图以停止扫描,然后再次使用SCAN_MODE_CONNECTABLE以可连接模式再次扫描。
答案 1 :(得分:1)
您无法使用第三方(非内核/非系统)应用程序在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.
* <p>The Bluetooth scan mode determines if the local adapter is
* connectable and/or discoverable from remote Bluetooth devices.
* <p>For privacy reasons, discoverable mode is automatically turned off
* after <code>duration</code> seconds. For example, 120 seconds should be
* enough for a remote device to initiate and complete its discovery
* process.
* <p>Valid scan mode values are:
* {@link #SCAN_MODE_NONE},
* {@link #SCAN_MODE_CONNECTABLE},
* {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}.
* <p>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.
* <p>Requires {@link android.Manifest.permission#WRITE_SECURE_SETTINGS}
* <p>Applications cannot set the scan mode. They should use
* <code>startActivityForResult(
* BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE})
* </code>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;
}
如源中所述,&#34;应用程序无法设置扫描模式&#34;。需要权限WRITE_SECURE_SETTINGS才能在API外部手动扫描模式which is not possible for third-party applications。
答案 2 :(得分:1)
这是一个很老的问题,但是你可以在没有用户许可的情况下切换蓝牙的扫描模式,并且(对于我已经测试过的)无限时间。 这需要在Android API上使用反射,因此这不是官方API。当然,你自己冒险使用它:) 下面是我用来使BT适配器可被发现的代码:
private boolean setBluetoothScanMode(int scanMode){
Method method = null;
final BluetoothAdapter btAdapter = btManager.getAdapter();
if(!btAdapter.isEnabled()){
Log.d(LCAT, "BT adapter is off, turning on");
btAdapter.enable();
}
try {
method = btAdapter.getClass().getMethod("setScanMode", int.class);
} catch (SecurityException e) {
return false;
} catch (NoSuchMethodException e) {
return false;
}
try {
method.invoke(btAdapter, scanMode);
} catch (IllegalArgumentException e) {
return false;
} catch (IllegalAccessException e) {
return false;
} catch (InvocationTargetException e) {
return false;
}
return true;
}
您还需要权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
此设备应处于扫描模式后,您可以通过传递int scanMode参数来选择。它正在使用Android API 23.