Android 阻止蓝牙配对对话框 [英] Android Prevent Bluetooth Pairing Dialog

查看:52
本文介绍了Android 阻止蓝牙配对对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用蓝牙进行打印的内部应用程序.我希望在没有用户输入的情况下进行蓝牙配对.我设法通过捕获 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.

https://github.com/android/platform_packages_apps_settings/blob/master/src/com/android/settings/bluetooth/BluetoothPairingDialog.java

由于广播是无序的,我无法调用 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 操作注册您自己的活动或广播接收器.此广播接收器允许在无需用户输入的情况下继续配对(仅在不需要 pin 时):

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆