2013-02-06 72 views
2

目前我正在开发蓝牙应用程序,我需要将扫描模式从SCAN_MODE_CONNECTABLE_DISCOVERABLE更改为SCAN_MODE_CONNECTABLE,只需点击一个按钮即可。如何在android中更改蓝牙适配器的扫描模式?

我设置可被发现使用以下意图:

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

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

+0

问题依然存在。有没有办法做到这一点,或者我们应该关闭蓝牙? –

回答

1

开始与SCAN_MODE_NONE新意图停止扫描,然后用SCAN_MODE_CONNECTABLE再次开始只在连接模式再次扫描。

+1

@ Dennis ..感谢您的回复..我只是想知道该intent目标是什么..因为我将SCAN_MODE_NONE设置为其旗帜之一..它与意图相同discoverableIntent = new Intent( \t \t \t \t \t \t \t \t \t BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); –

+1

我也试过这样做..意图intent =新的意图(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); \t \t \t \t \t \t \t intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_SCAN_MODE,BluetoothAdapter。SCAN_MODE_CONNECTABLE_DISCOVERABLE); \t \t \t \t \t \t \t intent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE,BluetoothAdapter.SCAN_MODE_NONE); \t \t \t \t \t \t \t startActivityForResult(意向,SCAN_MODE_CHANGED);但也没有工作.. –

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; 
} 

在源指出,“应用程序不能设置扫描模式”。手动扫描API which is not possible for third-party applications以外的模式需要权限WRITE_SECURE_SETTINGS

0

那么这是一个很老的问题,但是你可以切换蓝牙扫描模式,没有任何用户的许可和(就我测试过的)无限的时间。 这将需要在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.