如何在Android中更改蓝牙适配器的扫描模式? [英] How to Change the Scan Mode of Bluetooth Adapter in android.?
问题描述
当前,我正在使用蓝牙应用程序,需要单击按钮将扫描模式从 SCAN_MODE_CONNECTABLE_DISCOVERABLE
更改为 SCAN_MODE_CONNECTABLE
.
Currently i am working on a Bluetooth app where i need to change the Scan Mode from SCAN_MODE_CONNECTABLE_DISCOVERABLE
to SCAN_MODE_CONNECTABLE
on click of a button.
我使用以下意图将其设置为可发现":
i am setting it Discoverable using following Intent:
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
.
Now i want to skip the Discoverability in between and want to change its state to SCAN_MODE_CONNECTABLE
only.
请为我提供适当的解决方案../
Kindly provide me an appropriate solution../
推荐答案
这是一个很老的问题,但是您可以在没有用户任何许可的情况下(并且就我所测试的而言)无限制地切换蓝牙的扫描模式.这将需要在Android API上使用反射,因此这不是官方的API.当然,您自己承担风险使用:)下面是我用来使BT适配器可发现的代码:
Well it is quite old question, however you can switch scan mode of bluetooth without any permission of user and (for as far as i have tested) unlimited time. This will require using reflections on Android API, so this is not an official api. Of course, you are using it on your own risk :) Below is code i am using to make BT adapter discoverable:
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上运行.
After this device should be in scan mode you choose by passing the int scanMode argument. It is working on Android API 23.
这篇关于如何在Android中更改蓝牙适配器的扫描模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!