Android防止蓝牙配对对话框

Kei*_*agh 14 android dialog bluetooth

我正在开发一个使用蓝牙进行打印的内部应用程序.我想在没有用户输入的情况下进行蓝牙配对.我设法通过捕获android.bluetooth.device.action.PAIRING_REQUEST广播来实现这一目标.

在我的广播接收器中,我调用了setPin方法,并且配对工作正常,但是BluetoothPairingDialog显示一秒或两秒,然后消失 - 请参阅下面的链接.

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

由于广播是非订购的,我无法打电话abortBroadcast(),并且想知道是否有任何其他方法可以阻止配对对话框出现.我能以某种方式挂钩窗口管理器吗?

Joh*_*y C 3

老实说,我还没有想出一种方法可以在不修改 sdk 的情况下做到这一点。如果您是 OEM,这很简单(我使用的是 4.3):

在packages/apps/Settings/AndroidManifest.xml中,注释配对对话框的意图过滤器:

<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>
Run Code Online (Sandbox Code Playgroud)

在frameworks/base/core/java/android/bluetooth/BluetoothDevice.java中从此常量中删除@hide javadoc注释

@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_PAIRING_REQUEST =
        "android.bluetooth.device.action.PAIRING_REQUEST";
Run Code Online (Sandbox Code Playgroud)

和这个方法

public boolean setPairingConfirmation(boolean confirm) 
Run Code Online (Sandbox Code Playgroud)

然后为 BluetoothDevice.PAIRING_REQUEST 操作注册您自己的活动或广播接收器。该广播接收器允许在无需用户输入的情况下继续配对(仅当不需要 pin 时):

@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 );
   }
}
Run Code Online (Sandbox Code Playgroud)

您需要重建 sdk 并根据新版本编译代码才能访问常量和方法,并替换 /system 分区上的 Settings.apk 以禁用该对话框。您可能还需要作为系统应用程序运行,但我认为可能不需要。