[英]Android Prevent Bluetooth Pairing Dialog
我正在開發使用藍牙進行打印的內部應用程序。 我希望在沒有用戶輸入的情況下進行藍牙配對。 我設法通過捕獲android.bluetooth.device.action.PAIRING_REQUEST
廣播來使其正常工作。
在我的廣播接收器中,我調用setPin方法,配對成功,但是顯示BluetoothPairingDialog
時間為一兩秒鍾,然后消失了-請參見下面的鏈接。
由於廣播是無序的,因此我無法調用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.