問題描述
我正在開發一個使用藍牙進行打印的內部應用程序.我希望在沒有用戶輸入的情況下進行藍牙配對.我設法通過捕獲 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 阻止藍牙配對對話框的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!