【问题标题】:Android Prevent Bluetooth Pairing DialogAndroid 阻止蓝牙配对对话框
【发布时间】:2013-07-31 13:31:25
【问题描述】:

我正在开发一个使用蓝牙进行打印的内部应用程序。我希望在没有用户输入的情况下进行蓝牙配对。我已经设法通过捕获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(),并且想知道是否有任何其他方法可以防止出现配对对话框。我可以通过某种方式连接到窗口管理器吗?

【问题讨论】:

  • 我遇到了同样的问题。我可以通过调用 device.cancelPairingUserInput(); 在没有用户输入的情况下关闭对话框。 device.setPairingConfirmation(true);但只有在接收到状态为 BluetoothDevice.BOND_BONDING 的 BluetoothDevice.ACTION_BOND_STATE_CHANGED 操作之后,对话框才会短暂出现然后被关闭。
  • 这有什么更新吗?我需要在科尔多瓦防止这种情况发生
  • 抱歉,我已经有几年没有从事这个项目了。我从来没有解决过这个问题。

标签: android dialog bluetooth


【解决方案1】:

老实说,我无法在不修改 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>

在 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";

还有这个方法

public boolean setPairingConfirmation(boolean confirm) 

然后为 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 );
   }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 2013-08-14
    相关资源
    最近更新 更多