如何更改android中蓝牙适配器的扫描模式。?
目前我正在开发一款蓝牙应用程序,我需要在点击按钮后将扫描模式从如何更改android中蓝牙适配器的扫描模式。?,android,android-intent,bluetooth,discoverability,Android,Android Intent,Bluetooth,Discoverability,目前我正在开发一款蓝牙应用程序,我需要在点击按钮后将扫描模式从Scan\u Mode\u CONNECTABLE\u DISCOVERABLE更改为Scan\u Mode\u CONNECTABLE 我使用以下意图将其设置为可发现: Intent discoverableIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent .putExtra( BluetoothAdapter
Scan\u Mode\u CONNECTABLE\u DISCOVERABLE
更改为Scan\u Mode\u CONNECTABLE
我使用以下意图将其设置为可发现:
Intent discoverableIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent .putExtra( BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISOVERABLE_DURATION);
startActivityForResult(discoverableIntent, REQUEST_DISCOVERABLE_BT);
其中我设置了
不可恢复的\u持续时间=300
现在我想跳过中间的可发现性,只想将其状态更改为SCAN\u MODE\u CONNECTABLE
请为我提供一个合适的解决方案。使用扫描模式启动一个新意图\u NONE停止扫描,然后使用扫描模式\u CONNECTABLE重新启动,以便仅在可连接模式下再次扫描。使用扫描模式\u NONE启动一个新意图,停止扫描,然后使用扫描模式\u CONNECTABLE重新启动,以便在可连接模式下再次扫描仅限。您不能使用第三方(非内核/非系统)应用程序在4.2及更高版本中设置Android设备的扫描模式。您只能启用查找(
SCAN\u MODE\u CONNECTABLE\u DISCOVERABLE
),并使用EXTRA\u DISCOVERABLE\u duration
选项设置特定的持续时间。通过设置,可以有效地取消查找
有关证据,请浏览一下,重点是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之外的模式需要权限。您不能使用第三方(非内核/非系统)应用程序在4.2及更高版本中设置Android设备的扫描模式。您只能启用查找(
SCAN\u MODE\u CONNECTABLE\u DISCOVERABLE
),并使用EXTRA\u DISCOVERABLE\u duration
选项设置特定的持续时间。通过设置,可以有效地取消查找
有关证据,请浏览一下,重点是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之外的模式需要许可。这是一个很老的问题,但是您可以在没有任何用户许可的情况下(就我所测试的情况而言)无限时间地切换蓝牙的扫描模式。 这需要在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 这是一个很老的问题,但是你可以在没有任何用户许可的情况下(就我测试而言)无限时间地切换蓝牙扫描模式。 这需要在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 @Dennis..感谢您的回复..我只想知道该意图的目标是什么..因为我将设置扫描模式\u无作为其标志之一..它是否与intent discoverableIntent=new intent(BluetoothAdapter.ACTION\u REQUEST\u DISCOVERABLE)相同;我也试着把它当作。。意向意向=新意向(BluetoothAdapter.ACTION\u SCAN\u MODE\u CHANGED);intent.putExtra(BluetoothAdapter.EXTRA\u PREVIOUS\u SCAN\u MODE,BluetoothAdapter.SCAN\u MODE\u CONNECTABLE\u DISCOVERABLE);intent.putExtra(BluetoothAdapter.EXTRA\u扫描模式,BluetoothAdapter.SCAN\u模式\u无);startActivityForResult(意图、扫描模式改变);但是也不起作用。@Dennis..谢谢你的回答..我只想知道该意图的目标是什么..因为我将设置扫描模式\u无作为其标志之一..它与意图发现意图=新意图(BluetoothAdapter.ACTION\u REQUEST\u DISCOVERABLE)相同吗;我也试着把它当作。。意向意向=新意向(BluetoothAdapter.ACTION\u SCAN\u MODE\u CHANGED);intent.putExtra(BluetoothAdapter.EXTRA\u PREVIOUS\u SCAN\u MODE,BluetoothAdapter.SCAN\u MODE\u CONNECTABLE\u DISCOVERABLE);intent.putExtra(BluetoothAdapter.EXTRA\u扫描模式,BluetoothAdapter.SCAN\u模式\u无);startActivityForResult(意图、扫描模式改变);但是也没用问题依然存在。有没有办法做到这一点,还是我们应该关掉蓝牙?问题依然存在。有没有办法做到这一点,还是我们应该关掉蓝牙?