2

目前我正在开发一个蓝牙应用程序,我需要在单击按钮时将扫描模式从更改SCAN_MODE_CONNECTABLE_DISCOVERABLESCAN_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

现在我想跳过两者之间的可发现性,并将其状态更改为SCAN_MODE_CONNECTABLE仅。

请给我一个合适的解决方案../

4

3 回答 3

2

好吧,这是一个很老的问题,但是您可以在未经用户许可的情况下切换蓝牙的扫描模式,并且(据我测试)无限时间。这将需要在 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。

于 2016-01-27T09:30:21.477 回答
1

您无法使用第三方(非内核/非系统)应用程序设置 4.2 及更高版本的 Android 设备的扫描模式。您只能启用发现 ( SCAN_MODE_CONNECTABLE_DISCOVERABLE),并可选择使用 设置特定持续时间EXTRA_DISCOVERABLE_DURATION您可以通过将持续时间参数设置为 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;
}

如源代码中所述,“应用程序无法设置扫描模式”。需要WRITE_SECURE_SETTINGS权限才能在 API 之外手动扫描模式,第三方应用程序无法做到这一点

于 2014-04-23T15:39:07.680 回答
1

使用 SCAN_MODE_NONE 启动新意图以停止扫描,然后使用 SCAN_MODE_CONNECTABLE 再次开始以仅在可连接模式下再次扫描。

于 2013-02-06T18:19:03.870 回答