繁体   English   中英

Android阻止蓝牙配对对话框

[英]Android Prevent Bluetooth Pairing Dialog

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

老实说,我无法提出一种无需修改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操作注册您自己的活动或广播接收器。 此广播接收器无需用户输入即可继续配对(仅在不需要插针的情况下):

@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以禁用该对话框。 您可能还需要作为系统应用程序运行,但我认为可能不需要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM