Android防止蓝牙配对对话框 [英] Android Prevent Bluetooth Pairing Dialog
问题描述
我正在开发一个使用蓝牙进行打印的内部应用程序。我想要蓝牙配对发生没有用户输入。我已经设法通过捕获 android.bluetooth.device.action.PAIRING_REQUEST
广播来获得这项工作。
I'm developing an internal application which uses Bluetooth for printing. I want the Bluetooth pairing to occur without user input. I have managed to get that working by trapping the android.bluetooth.device.action.PAIRING_REQUEST
broadcast.
我的广播接收器我调用setPin方法,配对工作正常,但一个 BluetoothPairingDialog
显示一秒钟或两秒,然后它消失 - 见下面的链接。
In my broadcast receiver I call the setPin method, and pairing works ok, but a BluetoothPairingDialog
is displayed for a second or two, then it disappears - see link below.
由于广播是无序的,所以我无法调用 abortBroadcast()
,并且想知道是否有其他方法可以防止配对对话框出现。我可以以某种方式挂接到窗口管理器中吗?
Since the broadcast is non-ordered, I can't call abortBroadcast()
, and was wondering if there was any other way to prevent the pairing dialog from appearing. Can I hook into the window manager in some way?
推荐答案
我真的无法想出一个方法这样做而不修改sdk。如果你是OEM,很容易(我在4.3):
I honestly haven't been able to come up with a way to do this without modifying the sdk. If you're the OEM, it's easy (I'm on 4.3):
在packages / apps / Settings / AndroidManifest.xml中,为配对注释意图过滤器对话框:
In packages/apps/Settings/AndroidManifest.xml, comment the intent filter for the pairing dialog:
<activity android:name=".bluetooth.BluetoothPairingDialog"
android:label="@string/bluetooth_pairing_request"
android:excludeFromRecents="true"
android:theme="@*android:style/Theme.Holo.Dialog.Alert">
<!-- <intent-filter>
<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter> -->
</activity>
在frameworks / base / core / java / android / bluetooth / BluetoothDevice.java中,删除@hide javadoc $这个常数的注释
In frameworks/base/core/java/android/bluetooth/BluetoothDevice.java remove the @hide javadoc annotation from this constant
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_PAIRING_REQUEST =
"android.bluetooth.device.action.PAIRING_REQUEST";
此方法
public boolean setPairingConfirmation(boolean confirm)
然后注册自己的活动或广播接收器对于BluetoothDevice.PAIRING_REQUEST操作。此广播接收器允许配对继续,无需用户输入(仅当不需要引脚时):
Then register your own activity or broadcast receiver for the BluetoothDevice.PAIRING_REQUEST action. This broadcast receiver allows pairing to continue without user input (only if no pin is required):
@Override
public void onReceive(Context context, Intent intent) {
if( intent.getAction().equals(BluetoothDevice.ACTION_PAIRING_REQUEST) ) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
device.setPairingConfirmation( true );
}
}
您需要重建sdk并编译对新版本的代码来访问常量和方法,并替换/ system分区上的Settings.apk来禁用对话框。您可能还需要作为系统应用程序运行,但我可能不会。
You'll need to rebuild the sdk and compile your code against the new version to get access to the constant and methods, and replace Settings.apk on the /system partition to disable the dialog. You may possibly also need to be running as a system app but I think likely not.
这篇关于Android防止蓝牙配对对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!